|
| 1 | +use crate::models::initial::{AppSettings, MokaSettings, PgSettings}; |
1 | 2 | use deadpool_postgres::{Manager, RecyclingMethod, Pool as PgPool}; |
2 | 3 | use crate::utils::{process_channel, AppCache}; |
3 | 4 | use deadpool::{managed::Timeouts, Runtime}; |
4 | 5 | use actix_web::web::Data as webData; |
5 | 6 | use tokio_postgres::{Config, NoTls}; |
6 | | -use std::env::var as env_var; |
7 | 7 | use std::sync::mpsc::Sender; |
8 | 8 | use std::time::Duration; |
9 | 9 | use log::{info, warn}; |
10 | 10 |
|
11 | 11 |
|
12 | | -struct PgSettings { |
13 | | - url: String, |
14 | | - conn_timeout: u64, |
15 | | - max_pool_size: usize, |
16 | | - wait_timeout: u64, |
17 | | - new_connection_timeout: u64, |
18 | | - recycle_timeout: u64, |
19 | | - warm_pool: bool, |
20 | | - warm_pool_size: usize, |
21 | | -} |
22 | | - |
23 | | - |
24 | | -struct MokaSettings { |
25 | | - cache_size: u64, |
26 | | - expiration_time: Duration, |
27 | | -} |
28 | | - |
29 | | - |
30 | | -struct AppSettings { |
31 | | - pg_settings: PgSettings, |
32 | | - cache_settings: MokaSettings, |
33 | | - enable_logging: bool, |
34 | | -} |
35 | | - |
36 | | - |
37 | | -trait FromEnv { |
38 | | - fn from_env() -> Self; |
39 | | -} |
40 | | - |
41 | | - |
42 | | -impl FromEnv for PgSettings { |
43 | | - fn from_env() -> Self { |
44 | | - let url = env_var("POSTGRES_DB_URL").expect("POSTGRES_DB_URL must be set"); |
45 | | - let conn_timeout = env_var("PG_CONN_TIMEOUT") |
46 | | - .ok() |
47 | | - .and_then(|s| s.parse().ok()) |
48 | | - .expect("PG_CONN_TIMEOUT must be a positive integer of type u64"); |
49 | | - let max_pool_size = env_var("PG_POOL_MAX_SIZE") |
50 | | - .ok() |
51 | | - .and_then(|s| s.parse().ok()) |
52 | | - .expect("PG_POOL_MAX_SIZE must be a positive integer of type usize"); |
53 | | - let wait_timeout = env_var("PG_POOL_WAIT_TIMEOUT") |
54 | | - .ok() |
55 | | - .and_then(|s| s.parse().ok()) |
56 | | - .expect("PG_POOL_WAIT_TIMEOUT must be a positive integer of type u64"); |
57 | | - let new_connection_timeout = env_var("PG_POOL_NEW_CONNECTION_TIMEOUT") |
58 | | - .ok() |
59 | | - .and_then(|s| s.parse().ok()) |
60 | | - .expect("PG_POOL_NEW_CONNECTION_TIMEOUT must be a positive integer of type u64"); |
61 | | - let recycle_timeout = env_var("PG_POOL_RECYCLE_TIMEOUT") |
62 | | - .ok() |
63 | | - .and_then(|s| s.parse().ok()) |
64 | | - .expect("PG_POOL_RECYCLE_TIMEOUT must be a positive integer of type u64"); |
65 | | - let warm_pool = env_var("PG_POOL_WARM_POOL").expect("PG_POOL_WARM_POOL must be set as true or false"); |
66 | | - let warm_pool = match warm_pool.to_lowercase().as_str() { |
67 | | - "true" => true, |
68 | | - "false" => false, |
69 | | - _ => panic!("PG_POOL_WARM_POOL must be set as true or false"), |
70 | | - }; |
71 | | - let warm_pool_size = env_var("PG_POOL_WARM_POOL_SIZE") |
72 | | - .ok() |
73 | | - .and_then(|s| s.parse().ok()) |
74 | | - .expect("PG_POOL_WARM_POOL_SIZE must be a positive integer of type usize"); |
75 | | - |
76 | | - // Warm pool size can not go above 128 (if warm pool is enabled) |
77 | | - if warm_pool_size > max_pool_size { |
78 | | - panic!("PG_POOL_WARM_POOL_SIZE must be at most PG_POOL_MAX_SIZE, it can not go more than {}", max_pool_size); |
79 | | - } |
80 | | - if warm_pool && warm_pool_size > 128 { |
81 | | - panic!("PG_POOL_WARM_POOL_SIZE must be at most 128, and the optimal size is 64"); |
82 | | - } |
83 | | - |
84 | | - PgSettings { |
85 | | - url, |
86 | | - conn_timeout, |
87 | | - max_pool_size, |
88 | | - wait_timeout, |
89 | | - new_connection_timeout, |
90 | | - recycle_timeout, |
91 | | - warm_pool, |
92 | | - warm_pool_size, |
93 | | - } |
94 | | - } |
95 | | -} |
96 | | - |
97 | | - |
98 | | -impl FromEnv for MokaSettings { |
99 | | - fn from_env() -> Self { |
100 | | - let cache_size = env_var("CACHE_SIZE") |
101 | | - .ok() |
102 | | - .and_then(|s| s.parse().ok()) |
103 | | - .expect("CACHE_SIZE must be a positive integer of type u64"); |
104 | | - let expiration_time = env_var("CACHE_EXPIRATION_TIME") |
105 | | - .ok() |
106 | | - .and_then(|s| s.parse().ok()) |
107 | | - .expect("CACHE_EXPIRATION_TIME must be a positive integer of type u64"); |
108 | | - |
109 | | - MokaSettings { |
110 | | - cache_size, |
111 | | - expiration_time: Duration::from_secs(expiration_time), |
112 | | - } |
113 | | - } |
114 | | -} |
115 | | - |
116 | | - |
117 | | -impl FromEnv for AppSettings { |
118 | | - fn from_env() -> Self { |
119 | | - let enable_logging = env_var("ENABLE_LOGGING").expect("ENABLE_LOGGING must be set as true or false"); |
120 | | - let enable_logging = match enable_logging.to_lowercase().as_str() { |
121 | | - "true" => true, |
122 | | - "false" => false, |
123 | | - _ => panic!("ENABLE_LOGGING must be set as true or false"), |
124 | | - }; |
125 | | - |
126 | | - AppSettings { |
127 | | - pg_settings: PgSettings::from_env(), |
128 | | - cache_settings: MokaSettings::from_env(), |
129 | | - enable_logging, |
130 | | - } |
131 | | - } |
132 | | -} |
133 | | - |
134 | 12 |
|
135 | 13 | async fn warm_pool(pool: &PgPool, pg: &PgSettings) { |
136 | 14 | // Warm pool to avoid first-hit latency |
|
0 commit comments