Made database connection pool size configurable. Fixes #11
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -27,6 +27,10 @@ The server reads `config.toml` from the working directory, or from the path give
|
||||
|
||||
The file contains the database and SMTP passwords and the session key. Keep it outside the site directory (`target/site`): everything in there is served as static files. The server refuses to start when the config file is inside it.
|
||||
|
||||
## Database connections
|
||||
|
||||
Each instance opens database connections on demand, up to `max_connections` in the `[database]` section of the config (10 by default), and closes the ones that stay idle. When many instances share one PostgreSQL server, the sum of their limits must stay below the server's own `max_connections` (100 by default): lower the limit per instance, raise it on the server, or put PgBouncer in front of PostgreSQL.
|
||||
|
||||
## First admin account
|
||||
|
||||
On the first start, when the database has no admin account, the server creates the user `admin`. Its password must be given, at least 8 characters, in one of these ways:
|
||||
|
||||
@@ -11,6 +11,9 @@ password = 'password'
|
||||
db_name = 'rezervator'
|
||||
host = 'localhost'
|
||||
port = 5432
|
||||
# Upper limit of open database connections, default is 10. Lower it when many instances
|
||||
# share one PostgreSQL server.
|
||||
#max_connections = 10
|
||||
|
||||
[mailing]
|
||||
transport = "File" #or Smtp
|
||||
|
||||
+34
-1
@@ -43,11 +43,18 @@ pub struct Database {
|
||||
password: String,
|
||||
db_name: String,
|
||||
host: String,
|
||||
port: Option<u16>
|
||||
port: Option<u16>,
|
||||
max_connections: Option<u32>
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
impl Database {
|
||||
/// Upper limit of open database connections, 10 by default. With many instances on one
|
||||
/// PostgreSQL server the sum of the limits must fit into its `max_connections`.
|
||||
pub fn max_connections(&self) -> u32 {
|
||||
self.max_connections.unwrap_or(10).max(1)
|
||||
}
|
||||
|
||||
pub fn con_string(&self) -> String {
|
||||
format!("postgres://{}:{}@{}:{}/{}", self.user, self.password, self.host, self.port.unwrap_or(5432), self.db_name)
|
||||
}
|
||||
@@ -358,6 +365,32 @@ mod tests {
|
||||
assert_eq!(database.con_string(), "postgres://u:p@db.example.com:6543/db");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_connections_defaults_to_ten() {
|
||||
let database: Database = toml::from_str(
|
||||
"user = 'u'\npassword = 'p'\ndb_name = 'db'\nhost = 'localhost'").unwrap();
|
||||
|
||||
assert_eq!(database.max_connections(), 10);
|
||||
// The sample config has the key commented out
|
||||
assert_eq!(sample().database().max_connections(), 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_connections_can_be_configured() {
|
||||
let database: Database = toml::from_str(
|
||||
"user = 'u'\npassword = 'p'\ndb_name = 'db'\nhost = 'localhost'\nmax_connections = 3").unwrap();
|
||||
|
||||
assert_eq!(database.max_connections(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_connections_is_at_least_one() {
|
||||
let database: Database = toml::from_str(
|
||||
"user = 'u'\npassword = 'p'\ndb_name = 'db'\nhost = 'localhost'\nmax_connections = 0").unwrap();
|
||||
|
||||
assert_eq!(database.max_connections(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smtp_mailing_config() {
|
||||
let mailing: Mailing = toml::from_str(
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ async fn main() -> std::io::Result<()> {
|
||||
let key = Key::from(srv_conf.session().key());
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(10)
|
||||
.max_connections(srv_conf.database().max_connections())
|
||||
.connect(&srv_conf.database().con_string()).await.unwrap();
|
||||
migrate!().run(&pool).await.expect("could not run SQLx migrations");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user