-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
configs.rs
272 lines (243 loc) · 8.62 KB
/
configs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use common_utils::ext_traits::AsyncExt;
use diesel_models::configs::ConfigUpdateInternal;
use error_stack::{IntoReport, ResultExt};
use router_env::{instrument, tracing};
use storage_impl::redis::{
cache::{CacheKind, CONFIG_CACHE},
kv_store::RedisConnInterface,
pub_sub::PubSubInterface,
};
use super::{cache, MockDb, Store};
use crate::{
connection, consts,
core::errors::{self, CustomResult},
types::storage,
};
#[async_trait::async_trait]
pub trait ConfigInterface {
async fn insert_config(
&self,
config: storage::ConfigNew,
) -> CustomResult<storage::Config, errors::StorageError>;
async fn find_config_by_key(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError>;
async fn find_config_by_key_unwrap_or(
&self,
key: &str,
// If the config is not found it will be created with the default value.
default_config: Option<String>,
) -> CustomResult<storage::Config, errors::StorageError>;
async fn find_config_by_key_from_db(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError>;
async fn update_config_by_key(
&self,
key: &str,
config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError>;
async fn update_config_in_database(
&self,
key: &str,
config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError>;
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError>;
}
#[async_trait::async_trait]
impl ConfigInterface for Store {
#[instrument(skip_all)]
async fn insert_config(
&self,
config: storage::ConfigNew,
) -> CustomResult<storage::Config, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
config.insert(&conn).await.map_err(Into::into).into_report()
}
async fn update_config_in_database(
&self,
key: &str,
config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
storage::Config::update_by_key(&conn, key, config_update)
.await
.map_err(Into::into)
.into_report()
}
//update in DB and remove in redis and cache
async fn update_config_by_key(
&self,
key: &str,
config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError> {
cache::publish_and_redact(self, CacheKind::Config(key.into()), || {
self.update_config_in_database(key, config_update)
})
.await
}
async fn find_config_by_key_from_db(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
storage::Config::find_by_key(&conn, key)
.await
.map_err(Into::into)
.into_report()
}
//check in cache, then redis then finally DB, and on the way back populate redis and cache
async fn find_config_by_key(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
let find_config_by_key_from_db = || async {
let conn = connection::pg_connection_write(self).await?;
storage::Config::find_by_key(&conn, key)
.await
.map_err(Into::into)
.into_report()
};
cache::get_or_populate_in_memory(self, key, find_config_by_key_from_db, &CONFIG_CACHE).await
}
async fn find_config_by_key_unwrap_or(
&self,
key: &str,
// If the config is not found it will be created with the default value.
default_config: Option<String>,
) -> CustomResult<storage::Config, errors::StorageError> {
let find_else_unwrap_or = || async {
let conn = connection::pg_connection_write(self).await?;
match storage::Config::find_by_key(&conn, key)
.await
.map_err(Into::<errors::StorageError>::into)
.into_report()
{
Ok(a) => Ok(a),
Err(err) => {
if err.current_context().is_db_not_found() {
default_config
.ok_or(err)
.async_and_then(|c| async {
storage::ConfigNew {
key: key.to_string(),
config: c,
}
.insert(&conn)
.await
.map_err(Into::into)
.into_report()
})
.await
} else {
Err(err)
}
}
}
};
cache::get_or_populate_in_memory(self, key, find_else_unwrap_or, &CONFIG_CACHE).await
}
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
let deleted = storage::Config::delete_by_key(&conn, key)
.await
.map_err(Into::into)
.into_report()?;
self.get_redis_conn()
.map_err(Into::<errors::StorageError>::into)?
.publish(consts::PUB_SUB_CHANNEL, CacheKind::Config(key.into()))
.await
.map_err(Into::<errors::StorageError>::into)?;
Ok(deleted)
}
}
#[async_trait::async_trait]
impl ConfigInterface for MockDb {
#[instrument(skip_all)]
async fn insert_config(
&self,
config: storage::ConfigNew,
) -> CustomResult<storage::Config, errors::StorageError> {
let mut configs = self.configs.lock().await;
let config_new = storage::Config {
id: configs
.len()
.try_into()
.into_report()
.change_context(errors::StorageError::MockDbError)?,
key: config.key,
config: config.config,
};
configs.push(config_new.clone());
Ok(config_new)
}
async fn update_config_in_database(
&self,
key: &str,
config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError> {
self.update_config_by_key(key, config_update).await
}
async fn update_config_by_key(
&self,
key: &str,
config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError> {
let result = self
.configs
.lock()
.await
.iter_mut()
.find(|c| c.key == key)
.ok_or_else(|| {
errors::StorageError::ValueNotFound("cannot find config to update".to_string())
.into()
})
.map(|c| {
let config_updated =
ConfigUpdateInternal::from(config_update).create_config(c.clone());
*c = config_updated.clone();
config_updated
});
result
}
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {
let mut configs = self.configs.lock().await;
let result = configs
.iter()
.position(|c| c.key == key)
.map(|index| {
configs.remove(index);
true
})
.ok_or_else(|| {
errors::StorageError::ValueNotFound("cannot find config to delete".to_string())
.into()
});
result
}
async fn find_config_by_key(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
let configs = self.configs.lock().await;
let config = configs.iter().find(|c| c.key == key).cloned();
config.ok_or_else(|| {
errors::StorageError::ValueNotFound("cannot find config".to_string()).into()
})
}
async fn find_config_by_key_unwrap_or(
&self,
key: &str,
_default_config: Option<String>,
) -> CustomResult<storage::Config, errors::StorageError> {
self.find_config_by_key(key).await
}
async fn find_config_by_key_from_db(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
self.find_config_by_key(key).await
}
}