-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathconfig.rs
More file actions
120 lines (116 loc) · 4.03 KB
/
config.rs
File metadata and controls
120 lines (116 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use rocket::serde::{Deserialize, Serialize};
/// Base configuration for all database drivers.
///
/// A dictionary matching this structure is extracted from the active
/// [`Figment`](crate::figment::Figment), scoped to `databases.name`, where
/// `name` is the name of the database, by the
/// [`Initializer`](crate::Initializer) fairing on ignition and used to
/// configure the relevant database and database pool.
///
/// With the default provider, these parameters are typically configured in a
/// `Rocket.toml` file:
///
/// ```toml
/// [default.databases.db_name]
/// url = "/path/to/db.sqlite"
///
/// # Only `url` is required. These have sane defaults and are optional.
/// min_connections = 64
/// max_connections = 1024
/// connect_timeout = 5
/// idle_timeout = 120
///
/// # This option is only supported by the `sqlx_sqlite` driver.
/// extensions = ["memvfs", "rot13"]
/// ```
///
/// Alternatively, a custom provider can be used. For example, a custom `Figment`
/// with a global `databases.name` configuration:
///
/// ```rust
/// # use rocket::launch;
/// #[launch]
/// fn rocket() -> _ {
/// let figment = rocket::Config::figment()
/// .merge(("databases.name", rocket_db_pools::Config {
/// url: "db:specific@config&url".into(),
/// min_connections: None,
/// max_connections: 1024,
/// connect_timeout: 3,
/// idle_timeout: None,
/// extensions: None,
/// }));
///
/// rocket::custom(figment)
/// }
/// ```
///
/// For general information on configuration in Rocket, see [`rocket::config`].
/// For higher-level details on configuring a database, see the [crate-level
/// docs](crate#configuration).
// NOTE: Defaults provided by the figment created in the `Initializer` fairing.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(crate = "rocket::serde")]
pub struct Config {
/// Database-specific connection and configuration URL.
///
/// The format of the URL is database specific; consult your database's
/// documentation.
pub url: String,
/// Minimum number of connections to maintain in the pool.
///
/// **Note:** `deadpool` drivers do not support and thus ignore this value.
///
/// _Default:_ `None`.
pub min_connections: Option<u32>,
/// Maximum number of connections to maintain in the pool.
///
/// _Default:_ `workers * 4`.
pub max_connections: usize,
/// Number of seconds to wait for a connection before timing out.
///
/// If the timeout elapses before a connection can be made or retrieved from
/// a pool, an error is returned.
///
/// _Default:_ `5`.
pub connect_timeout: u64,
/// Maximum number of seconds to keep a connection alive for.
///
/// After a connection is established, it is maintained in a pool for
/// efficient connection retrieval. When an `idle_timeout` is set, that
/// connection will be closed after the timeout elapses. If an
/// `idle_timeout` is not specified, the behavior is driver specific but
/// typically defaults to keeping a connection active indefinitely.
///
/// _Default:_ `None`.
pub idle_timeout: Option<u64>,
/// A list of database extensions to load at run-time.
///
/// **Note:** Only the `sqlx_sqlite` driver supports this option (for SQLite
/// extensions) at this time. All other drivers ignore this option.
///
/// _Default:_ `None`.
pub extensions: Option<Vec<String>>,
}
impl Default for Config {
fn default() -> Self {
Self {
url: Default::default(),
min_connections: Default::default(),
max_connections: rocket::Config::default().workers * 4,
connect_timeout: 5,
idle_timeout: Default::default(),
extensions: Default::default(),
}
}
}
#[cfg(test)]
mod tests {
use super::Config;
#[test]
fn default_values_sane() {
let config = Config::default();
assert_ne!(config.max_connections, 0);
assert_eq!(config.connect_timeout, 5);
}
}