From 187f337f7e73e077f68a43166353e822b782800b Mon Sep 17 00:00:00 2001 From: Jeb Bearer Date: Fri, 15 Nov 2024 08:02:08 -0800 Subject: [PATCH] Make connection pool options configurable (#2285) Also changes the default connection pool size from 10 to 25 --- sequencer/api/public-env-vars.toml | 4 +++ sequencer/src/persistence/sql.rs | 39 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/sequencer/api/public-env-vars.toml b/sequencer/api/public-env-vars.toml index 0ee5aca64..74fbe2495 100644 --- a/sequencer/api/public-env-vars.toml +++ b/sequencer/api/public-env-vars.toml @@ -73,8 +73,12 @@ variables = [ "ESPRESSO_SEQUENCER_LIBP2P_BIND_ADDRESS", "ESPRESSO_SEQUENCER_MAX_CONNECTIONS", "ESPRESSO_SEQUENCER_ORCHESTRATOR_URL", + "ESPRESSO_SEQUENCER_POSTGRES_CONNECTION_TIMEOUT", "ESPRESSO_SEQUENCER_POSTGRES_DATABASE", "ESPRESSO_SEQUENCER_POSTGRES_HOST", + "ESPRESSO_SEQUENCER_POSTGRES_IDLE_CONNECTION_TIMEOUT", + "ESPRESSO_SEQUENCER_POSTGRES_MAX_CONNECTIONS", + "ESPRESSO_SEQUENCER_POSTGRES_MIN_CONNECTIONS", "ESPRESSO_SEQUENCER_POSTGRES_PORT", "ESPRESSO_SEQUENCER_POSTGRES_PRUNE", "ESPRESSO_SEQUENCER_POSTGRES_USE_TLS", diff --git a/sequencer/src/persistence/sql.rs b/sequencer/src/persistence/sql.rs index 24e4a5db5..4cd370438 100644 --- a/sequencer/src/persistence/sql.rs +++ b/sequencer/src/persistence/sql.rs @@ -118,6 +118,45 @@ pub struct Options { /// fetching from peers. #[clap(long, env = "ESPRESSO_SEQUENCER_ARCHIVE", conflicts_with = "prune")] pub(crate) archive: bool, + + /// The maximum idle time of a database connection. + /// + /// Any connection which has been open and unused longer than this duration will be + /// automatically closed to reduce load on the server. + #[clap(long, env = "ESPRESSO_SEQUENCER_POSTGRES_IDLE_CONNECTION_TIMEOUT", value_parser = parse_duration, default_value = "10m")] + pub(crate) idle_connection_timeout: Duration, + + /// The maximum lifetime of a database connection. + /// + /// Any connection which has been open longer than this duration will be automatically closed + /// (and, if needed, replaced), even if it is otherwise healthy. It is good practice to refresh + /// even healthy connections once in a while (e.g. daily) in case of resource leaks in the + /// server implementation. + #[clap(long, env = "ESPRESSO_SEQUENCER_POSTGRES_CONNECTION_TIMEOUT", value_parser = parse_duration, default_value = "30m")] + pub(crate) connection_timeout: Duration, + + /// The minimum number of database connections to maintain at any time. + /// + /// The database client will, to the best of its ability, maintain at least `min` open + /// connections at all times. This can be used to reduce the latency hit of opening new + /// connections when at least this many simultaneous connections are frequently needed. + #[clap( + long, + env = "ESPRESSO_SEQUENCER_POSTGRES_MIN_CONNECTIONS", + default_value = "0" + )] + pub(crate) min_connections: u32, + + /// The maximum number of database connections to maintain at any time. + /// + /// Once `max` connections are in use simultaneously, further attempts to acquire a connection + /// (or begin a transaction) will block until one of the existing connections is released. + #[clap( + long, + env = "ESPRESSO_SEQUENCER_POSTGRES_MAX_CONNECTIONS", + default_value = "25" + )] + pub(crate) max_connections: u32, } impl TryFrom for Config {