Skip to content

Commit a61e590

Browse files
committed
new config option: database_connection_acquire_timeout_seconds
1 parent 729bf58 commit a61e590

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ on a [JSON](https://en.wikipedia.org/wiki/JSON) file placed in `sqlpage/sqlpage.
1212
| `database_connection_idle_timeout_seconds` | depends on the database | Automatically close database connections after this period of inactivity |
1313
| `database_connection_max_lifetime_seconds` | depends on the database | Always close database connections after this amount of time |
1414
| `database_connection_retries` | 6 | Database connection attempts before giving up. Retries will happen every 5 seconds. |
15+
| `database_connection_acquire_timeout_seconds` | 10 | How long to wait when acquiring a database connection from the pool before giving up and returning an error. |
1516
| `sqlite_extensions` | | An array of SQLite extensions to load, such as `mod_spatialite` |
1617

1718
You can find an example configuration file in [`sqlpage/sqlpage.json`](./sqlpage/sqlpage.json).

src/app_config.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ pub struct AppConfig {
2727
/// will wait up to 30 seconds for the database to become available.
2828
#[serde(default = "default_database_connection_retries")]
2929
pub database_connection_retries: u32,
30+
31+
/// Maximum number of seconds to wait before giving up when acquiring a database connection from the
32+
/// pool. The default is 10 seconds.
33+
#[serde(default = "default_database_connection_acquire_timeout_seconds")]
34+
pub database_connection_acquire_timeout_seconds: f64,
3035
}
3136

3237
pub fn load() -> anyhow::Result<AppConfig> {
3338
let mut conf = Config::builder()
3439
.set_default("listen_on", "0.0.0.0:8080")?
3540
.add_source(config::File::with_name("sqlpage/sqlpage").required(false))
3641
.add_source(env_config())
37-
.add_source(env_config().prefix("SQLPAGE_"))
42+
.add_source(env_config().prefix("SQLPAGE"))
3843
.build()?
3944
.try_deserialize::<AppConfig>()
4045
.with_context(|| "Unable to load configuration")?;
@@ -93,6 +98,10 @@ fn default_database_connection_retries() -> u32 {
9398
6
9499
}
95100

101+
fn default_database_connection_acquire_timeout_seconds() -> f64 {
102+
10.
103+
}
104+
96105
#[cfg(test)]
97106
pub(crate) mod tests {
98107
use super::AppConfig;

src/webserver/database/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ impl Database {
277277
_ => Some(Duration::from_secs(60 * 60)),
278278
}),
279279
)
280+
.acquire_timeout(Duration::from_secs_f64(
281+
config.database_connection_acquire_timeout_seconds,
282+
))
280283
}
281284
}
282285

0 commit comments

Comments
 (0)