Skip to content

Commit 5cce992

Browse files
chore: Add statement timeout to created users (#25)
* chore: Add statement timeout to created users * Fix default timeout and update version * Rename query_timeout to statement_timeout * Rename query_timeout to statement_timeout on README * Change match pattern to unwrap_or
1 parent d5e4660 commit 5cce992

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

operator/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

operator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ext-cardano-dbsync"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2021"
55
default-run = "controller"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

operator/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This project is a Kubernetes custom controller to create users on dbsync's Postg
2424
| DB_MAX_CONNECTIONS | 2 |
2525
| DCU_PER_SECOND | preview=5,preprod=5,mainnet=5 |
2626
| METRICS_DELAY | 30 |
27+
| STATEMENT_TIMEOUT | 12000 |
2728

2829

2930
## Commands

operator/src/config.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct Config {
1717
pub dcu_per_second: HashMap<String, f64>,
1818

1919
pub metrics_delay: Duration,
20+
pub statement_timeout: u64,
2021
}
2122

2223
impl Config {
@@ -63,12 +64,60 @@ impl Config {
6364
.expect("METRICS_DELAY must be a number"),
6465
);
6566

67+
let statement_timeout = env::var("STATEMENT_TIMEOUT")
68+
.unwrap_or("120000".to_string())
69+
.parse::<u64>()
70+
.expect("STATEMENT_TIMEOUT must be a number");
71+
6672
Self {
6773
db_urls,
6874
db_names,
6975
db_max_connections,
7076
dcu_per_second,
7177
metrics_delay,
78+
statement_timeout,
7279
}
7380
}
7481
}
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
87+
#[test]
88+
fn test_from_env() {
89+
env::set_var("DB_URLS", "url1,url2");
90+
env::set_var(
91+
"DB_NAMES",
92+
"preview=dbsync-preview,preprod=dbsync-preprod,mainnet=dbsync-mainnet",
93+
);
94+
env::set_var("DCU_PER_SECOND", "preview=5,preprod=5,mainnet=5");
95+
env::set_var("METRICS_DELAY", "100");
96+
env::set_var("STATEMENT_TIMEOUT", "100");
97+
98+
let config = Config::from_env();
99+
assert_eq!(config.db_urls, vec!["url1".to_owned(), "url2".to_owned()]);
100+
assert_eq!(
101+
config.db_names,
102+
HashMap::from([
103+
("preview".to_owned(), "dbsync-preview".to_owned()),
104+
("preprod".to_owned(), "dbsync-preprod".to_owned()),
105+
("mainnet".to_owned(), "dbsync-mainnet".to_owned())
106+
])
107+
);
108+
assert_eq!(
109+
config.dcu_per_second,
110+
HashMap::from([
111+
("preview".to_owned(), 5.0),
112+
("preprod".to_owned(), 5.0),
113+
("mainnet".to_owned(), 5.0)
114+
])
115+
);
116+
assert_eq!(config.statement_timeout, 100);
117+
118+
// Check default query timeout
119+
env::remove_var("STATEMENT_TIMEOUT");
120+
let config = Config::from_env();
121+
assert_eq!(config.statement_timeout, 120000);
122+
}
123+
}

operator/src/postgres.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::str::FromStr;
33
use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod};
44
use tokio_postgres::{NoTls, Row};
55

6-
use crate::Error;
6+
use crate::{get_config, Error};
77

88
#[derive(Clone)]
99
pub struct Postgres {
@@ -32,6 +32,10 @@ impl Postgres {
3232
let query_create_user = format!("create user \"{username}\" with password '{password}';");
3333
let query_grant = format!("grant select on all tables in schema public to \"{username}\";");
3434

35+
let timeout = get_config().statement_timeout;
36+
let query_set_timeout =
37+
format!("alter role \"{username}\" set statement_timeout = '{timeout}';");
38+
3539
let mut client = self.pool.get().await?;
3640
let tx = client.transaction().await?;
3741

@@ -49,6 +53,13 @@ impl Postgres {
4953
return Err(Error::PgError(err.to_string()));
5054
}
5155

56+
let set_timeout_stmt = tx.prepare(&query_set_timeout).await?;
57+
let set_timeout_result = tx.execute(&set_timeout_stmt, &[]).await;
58+
if let Err(err) = set_timeout_result {
59+
tx.rollback().await?;
60+
return Err(Error::PgError(err.to_string()));
61+
}
62+
5263
tx.commit().await?;
5364
Ok(())
5465
}

0 commit comments

Comments
 (0)