Skip to content

Commit a3dd8b7

Browse files
authored
fix: eliminate recursive call while updating config in database (#2128)
1 parent ca9fb0c commit a3dd8b7

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

crates/router/src/db/configs.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ pub trait ConfigInterface {
3636
config_update: storage::ConfigUpdate,
3737
) -> CustomResult<storage::Config, errors::StorageError>;
3838

39+
async fn update_config_in_database(
40+
&self,
41+
key: &str,
42+
config_update: storage::ConfigUpdate,
43+
) -> CustomResult<storage::Config, errors::StorageError>;
44+
3945
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError>;
4046
}
4147

@@ -49,14 +55,26 @@ impl ConfigInterface for Store {
4955
config.insert(&conn).await.map_err(Into::into).into_report()
5056
}
5157

58+
async fn update_config_in_database(
59+
&self,
60+
key: &str,
61+
config_update: storage::ConfigUpdate,
62+
) -> CustomResult<storage::Config, errors::StorageError> {
63+
let conn = connection::pg_connection_write(self).await?;
64+
storage::Config::update_by_key(&conn, key, config_update)
65+
.await
66+
.map_err(Into::into)
67+
.into_report()
68+
}
69+
5270
//update in DB and remove in redis and cache
5371
async fn update_config_by_key(
5472
&self,
5573
key: &str,
5674
config_update: storage::ConfigUpdate,
5775
) -> CustomResult<storage::Config, errors::StorageError> {
5876
cache::publish_and_redact(self, CacheKind::Config(key.into()), || {
59-
self.update_config_by_key(key, config_update)
77+
self.update_config_in_database(key, config_update)
6078
})
6179
.await
6280
}
@@ -124,6 +142,14 @@ impl ConfigInterface for MockDb {
124142
Ok(config_new)
125143
}
126144

145+
async fn update_config_in_database(
146+
&self,
147+
key: &str,
148+
config_update: storage::ConfigUpdate,
149+
) -> CustomResult<storage::Config, errors::StorageError> {
150+
self.update_config_by_key(key, config_update).await
151+
}
152+
127153
async fn update_config_by_key(
128154
&self,
129155
key: &str,
@@ -166,7 +192,7 @@ impl ConfigInterface for MockDb {
166192
result
167193
}
168194

169-
async fn find_config_by_key_from_db(
195+
async fn find_config_by_key(
170196
&self,
171197
key: &str,
172198
) -> CustomResult<storage::Config, errors::StorageError> {
@@ -178,15 +204,10 @@ impl ConfigInterface for MockDb {
178204
})
179205
}
180206

181-
async fn find_config_by_key(
207+
async fn find_config_by_key_from_db(
182208
&self,
183209
key: &str,
184210
) -> CustomResult<storage::Config, errors::StorageError> {
185-
let configs = self.configs.lock().await;
186-
let config = configs.iter().find(|c| c.key == key).cloned();
187-
188-
config.ok_or_else(|| {
189-
errors::StorageError::ValueNotFound("cannot find config".to_string()).into()
190-
})
211+
self.find_config_by_key(key).await
191212
}
192213
}

0 commit comments

Comments
 (0)