mirror of https://github.com/rwf2/Rocket.git
Default 'pool_size' to 'workers * 4'.
The previous default was 'workers * 2'.
This commit is contained in:
parent
33cff5938c
commit
7d5f7db9f4
|
@ -1,3 +1,8 @@
|
||||||
|
use rocket::{Rocket, Build};
|
||||||
|
use rocket::figment::{self, Figment, providers::Serialized};
|
||||||
|
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
/// A base `Config` for any `Poolable` type.
|
/// A base `Config` for any `Poolable` type.
|
||||||
///
|
///
|
||||||
/// For the following configuration:
|
/// For the following configuration:
|
||||||
|
@ -28,7 +33,7 @@
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Connection URL specified in the Rocket configuration.
|
/// Connection URL specified in the Rocket configuration.
|
||||||
pub url: String,
|
pub url: String,
|
||||||
/// Initial pool size. Defaults to the number of Rocket workers * 2.
|
/// Initial pool size. Defaults to the number of Rocket workers * 4.
|
||||||
pub pool_size: u32,
|
pub pool_size: u32,
|
||||||
/// How long to wait, in seconds, for a new connection before timing out.
|
/// How long to wait, in seconds, for a new connection before timing out.
|
||||||
/// Defaults to `5`.
|
/// Defaults to `5`.
|
||||||
|
@ -36,9 +41,6 @@ pub struct Config {
|
||||||
pub timeout: u8,
|
pub timeout: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
use rocket::figment::{self, Figment, providers::Serialized};
|
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
/// Retrieves the database configuration for the database named `name`.
|
/// Retrieves the database configuration for the database named `name`.
|
||||||
///
|
///
|
||||||
|
@ -66,7 +68,9 @@ impl Config {
|
||||||
///
|
///
|
||||||
/// let config = Config::from("my_other_db", rocket).unwrap();
|
/// let config = Config::from("my_other_db", rocket).unwrap();
|
||||||
/// assert_eq!(config.url, "mysql://root:root@localhost/database");
|
/// assert_eq!(config.url, "mysql://root:root@localhost/database");
|
||||||
/// assert_eq!(config.pool_size, (rocket.config().workers * 2) as u32);
|
///
|
||||||
|
/// let workers = rocket.figment().extract_inner::<u32>(rocket::Config::WORKERS);
|
||||||
|
/// assert_eq!(config.pool_size, (workers.unwrap() * 4));
|
||||||
///
|
///
|
||||||
/// let config = Config::from("unknown_db", rocket);
|
/// let config = Config::from("unknown_db", rocket);
|
||||||
/// assert!(config.is_err())
|
/// assert!(config.is_err())
|
||||||
|
@ -97,10 +101,18 @@ impl Config {
|
||||||
/// ```
|
/// ```
|
||||||
pub fn figment(db_name: &str, rocket: &rocket::Rocket) -> Figment {
|
pub fn figment(db_name: &str, rocket: &rocket::Rocket) -> Figment {
|
||||||
let db_key = format!("databases.{}", db_name);
|
let db_key = format!("databases.{}", db_name);
|
||||||
let key = |name: &str| format!("{}.{}", db_key, name);
|
let default_pool_size = rocket.figment()
|
||||||
Figment::from(rocket.figment())
|
.extract_inner::<u32>(rocket::Config::WORKERS)
|
||||||
.join(Serialized::default(&key("pool_size"), rocket.config().workers * 2))
|
.map(|workers| workers * 4)
|
||||||
.join(Serialized::default(&key("timeout"), 5))
|
.ok();
|
||||||
|
|
||||||
|
let figment = Figment::from(rocket.figment())
|
||||||
.focus(&db_key)
|
.focus(&db_key)
|
||||||
|
.join(Serialized::default("timeout", 5));
|
||||||
|
|
||||||
|
match default_pool_size {
|
||||||
|
Some(pool_size) => figment.join(Serialized::default("pool_size", pool_size)),
|
||||||
|
None => figment
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,7 +131,7 @@
|
||||||
//! Additionally, all configurations accept the following _optional_ keys:
|
//! Additionally, all configurations accept the following _optional_ keys:
|
||||||
//!
|
//!
|
||||||
//! * `pool_size` - the size of the pool, i.e., the number of connections to
|
//! * `pool_size` - the size of the pool, i.e., the number of connections to
|
||||||
//! pool (defaults to the configured number of workers * 2)
|
//! pool (defaults to the configured number of workers * 4)
|
||||||
//!
|
//!
|
||||||
//! Additional options may be required or supported by other adapters.
|
//! Additional options may be required or supported by other adapters.
|
||||||
//!
|
//!
|
||||||
|
|
Loading…
Reference in New Issue