diff --git a/src/connector/mysql.rs b/src/connector/mysql.rs index be120bb3a..d707b7aba 100644 --- a/src/connector/mysql.rs +++ b/src/connector/mysql.rs @@ -110,7 +110,7 @@ impl MysqlUrl { self.url.port().unwrap_or(3306) } - /// The connection timeout. + /// The connect timeout. pub fn connect_timeout(&self) -> Option { self.query_params.connect_timeout } @@ -151,32 +151,22 @@ impl MysqlUrl { fn parse_query_params(url: &Url) -> Result { let mut ssl_opts = my::SslOpts::default(); ssl_opts = ssl_opts.with_danger_accept_invalid_certs(true); + let mut use_ssl = false; + let mut connect_timeout = Some(Duration::from_secs(5)); let mut connection_limit = None; - let mut use_ssl = false; + let mut pool_timeout = Some(Duration::from_secs(10)); + let mut socket = None; let mut socket_timeout = None; - let mut connect_timeout = Some(Duration::from_secs(5)); - let mut pool_timeout = Some(Duration::from_secs(10)); - let mut max_connection_lifetime = None; - let mut max_idle_connection_lifetime = Some(Duration::from_secs(300)); let mut prefer_socket = None; + let mut statement_cache_size = 1000; + let mut max_connection_lifetime = None; + let mut max_idle_connection_lifetime = Some(Duration::from_secs(300)); for (k, v) in url.query_pairs() { match k.as_ref() { - "connection_limit" => { - let as_int: usize = v - .parse() - .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; - - connection_limit = Some(as_int); - } - "statement_cache_size" => { - statement_cache_size = v - .parse() - .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; - } "sslcert" => { use_ssl = true; ssl_opts = ssl_opts.with_root_cert_path(Some(Path::new(&*v).to_path_buf())); @@ -189,20 +179,19 @@ impl MysqlUrl { use_ssl = true; ssl_opts = ssl_opts.with_password(Some(v.to_string())); } - "socket" => { - socket = Some(v.replace("(", "").replace(")", "")); - } - "socket_timeout" => { - let as_int = v - .parse() - .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; - socket_timeout = Some(Duration::from_secs(as_int)); - } - "prefer_socket" => { - let as_bool = v - .parse::() - .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; - prefer_socket = Some(as_bool) + "sslaccept" => { + match v.as_ref() { + "strict" => { + ssl_opts = ssl_opts.with_danger_accept_invalid_certs(false); + } + "accept_invalid_certs" => {} + _ => { + tracing::debug!( + message = "Unsupported SSL accept mode, defaulting to `accept_invalid_certs`", + mode = &*v + ); + } + }; } "connect_timeout" => { let as_int = v @@ -214,6 +203,13 @@ impl MysqlUrl { _ => Some(Duration::from_secs(as_int)), }; } + "connection_limit" => { + let as_int: usize = v + .parse() + .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; + + connection_limit = Some(as_int); + } "pool_timeout" => { let as_int = v .parse::() @@ -224,19 +220,25 @@ impl MysqlUrl { _ => Some(Duration::from_secs(as_int)), }; } - "sslaccept" => { - match v.as_ref() { - "strict" => { - ssl_opts = ssl_opts.with_danger_accept_invalid_certs(false); - } - "accept_invalid_certs" => {} - _ => { - tracing::debug!( - message = "Unsupported SSL accept mode, defaulting to `accept_invalid_certs`", - mode = &*v - ); - } - }; + "socket" => { + socket = Some(v.replace("(", "").replace(")", "")); + } + "socket_timeout" => { + let as_int = v + .parse() + .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; + socket_timeout = Some(Duration::from_secs(as_int)); + } + "prefer_socket" => { + let as_bool = v + .parse::() + .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; + prefer_socket = Some(as_bool) + } + "statement_cache_size" => { + statement_cache_size = v + .parse() + .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; } "max_connection_lifetime" => { let as_int = v @@ -267,17 +269,17 @@ impl MysqlUrl { } Ok(MysqlUrlQueryParams { + use_ssl, ssl_opts, + connect_timeout, connection_limit, - use_ssl, + pool_timeout, socket, socket_timeout, - connect_timeout, - pool_timeout, - max_connection_lifetime, - max_idle_connection_lifetime, prefer_socket, statement_cache_size, + max_connection_lifetime, + max_idle_connection_lifetime, }) } @@ -318,17 +320,17 @@ impl MysqlUrl { #[derive(Debug, Clone)] pub(crate) struct MysqlUrlQueryParams { + use_ssl: bool, ssl_opts: my::SslOpts, + connect_timeout: Option, connection_limit: Option, - use_ssl: bool, + pool_timeout: Option, socket: Option, socket_timeout: Option, - connect_timeout: Option, - pool_timeout: Option, - max_connection_lifetime: Option, - max_idle_connection_lifetime: Option, prefer_socket: Option, statement_cache_size: usize, + max_connection_lifetime: Option, + max_idle_connection_lifetime: Option, } impl Mysql { diff --git a/src/connector/postgres.rs b/src/connector/postgres.rs index 74f4f4cda..b7b032692 100644 --- a/src/connector/postgres.rs +++ b/src/connector/postgres.rs @@ -260,24 +260,31 @@ impl PostgresUrl { } fn parse_query_params(url: &Url) -> Result { - let mut connection_limit = None; let mut schema = None; + let mut pg_bouncer = false; + + let mut ssl_mode = SslMode::Prefer; let mut certificate_file = None; let mut identity_file = None; let mut identity_password = None; let mut ssl_accept_mode = SslAcceptMode::AcceptInvalidCerts; - let mut ssl_mode = SslMode::Prefer; - let mut host = None; - let mut socket_timeout = None; + let mut connect_timeout = Some(Duration::from_secs(5)); + let mut connection_limit = None; let mut pool_timeout = Some(Duration::from_secs(10)); - let mut pg_bouncer = false; + + let mut host = None; + let mut socket_timeout = None; + let mut statement_cache_size = 500; let mut max_connection_lifetime = None; let mut max_idle_connection_lifetime = Some(Duration::from_secs(300)); for (k, v) in url.query_pairs() { match k.as_ref() { + "schema" => { + schema = Some(v.to_string()); + } "pgbouncer" => { pg_bouncer = v .parse() @@ -302,11 +309,6 @@ impl PostgresUrl { "sslpassword" => { identity_password = Some(v.to_string()); } - "statement_cache_size" => { - statement_cache_size = v - .parse() - .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; - } "sslaccept" => { match v.as_ref() { "strict" => { @@ -325,24 +327,6 @@ impl PostgresUrl { } }; } - "schema" => { - schema = Some(v.to_string()); - } - "connection_limit" => { - let as_int: usize = v - .parse() - .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; - connection_limit = Some(as_int); - } - "host" => { - host = Some(v.to_string()); - } - "socket_timeout" => { - let as_int = v - .parse() - .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; - socket_timeout = Some(Duration::from_secs(as_int)); - } "connect_timeout" => { let as_int = v .parse() @@ -354,6 +338,12 @@ impl PostgresUrl { connect_timeout = Some(Duration::from_secs(as_int)); } } + "connection_limit" => { + let as_int: usize = v + .parse() + .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; + connection_limit = Some(as_int); + } "pool_timeout" => { let as_int = v .parse() @@ -365,6 +355,20 @@ impl PostgresUrl { pool_timeout = Some(Duration::from_secs(as_int)); } } + "host" => { + host = Some(v.to_string()); + } + "socket_timeout" => { + let as_int = v + .parse() + .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; + socket_timeout = Some(Duration::from_secs(as_int)); + } + "statement_cache_size" => { + statement_cache_size = v + .parse() + .map_err(|_| Error::builder(ErrorKind::InvalidConnectionArguments).build())?; + } "max_connection_lifetime" => { let as_int = v .parse() @@ -394,20 +398,20 @@ impl PostgresUrl { } Ok(PostgresUrlQueryParams { + schema, + pg_bouncer, + ssl_mode, ssl_params: SslParams { certificate_file, identity_file, ssl_accept_mode, identity_password: Hidden(identity_password), }, - connection_limit, - schema, - ssl_mode, - host, connect_timeout, + connection_limit, pool_timeout, + host, socket_timeout, - pg_bouncer, statement_cache_size, max_connection_lifetime, max_idle_connection_lifetime, @@ -445,15 +449,15 @@ impl PostgresUrl { #[derive(Debug, Clone)] pub(crate) struct PostgresUrlQueryParams { - ssl_params: SslParams, - connection_limit: Option, schema: Option, - ssl_mode: SslMode, pg_bouncer: bool, - host: Option, - socket_timeout: Option, + ssl_mode: SslMode, + ssl_params: SslParams, connect_timeout: Option, + connection_limit: Option, pool_timeout: Option, + host: Option, + socket_timeout: Option, statement_cache_size: usize, max_connection_lifetime: Option, max_idle_connection_lifetime: Option, diff --git a/src/pooled.rs b/src/pooled.rs index 8e878f511..c65a5b383 100644 --- a/src/pooled.rs +++ b/src/pooled.rs @@ -25,6 +25,8 @@ //! //! - `connection_limit` defines the maximum number of connections opened to the //! database. +//! - `max_connection_lifetime`, TODO +//! - `max_idle_connection_lifetime`, TODO //! //! ## SQLite //! @@ -38,6 +40,14 @@ //! //! ## PostgreSQL //! +//! - `schema` the default search path. +//! - `pgbouncer` either `true` or `false`. If set, allows usage with the +//! pgBouncer connection pool in transaction mode. Additionally a transaction +//! is required for every query for the mode to work. When starting a new +//! transaction, a deallocation query `DEALLOCATE ALL` is executed right after +//! `BEGIN` to avoid possible collisions with statements created in other +//! sessions. +//! //! - `sslmode` either `disable`, `prefer` or `require`. [Read more](https://docs.rs/tokio-postgres/0.5.0-alpha.1/tokio_postgres/config/enum.SslMode.html) //! - `sslcert` should point to a PEM certificate file. //! - `sslidentity` should point to a PKCS12 certificate database. @@ -46,27 +56,23 @@ //! certificate needs to be valid and in the CA certificates. //! `accept_invalid_certs` accepts any certificate from the server and can //! lead to weakened security. Defaults to `accept_invalid_certs`. -//! - `schema` the default search path. +//! +//! - `connect_timeout` defined in seconds. Connecting to a +//! database will return a `ConnectTimeout` error if taking more than the +//! defined value. Defaults to 5 seconds, if set to 0, no timeout. +//! //! - `host` additionally the host can be given as a parameter, typically in //! cases when connectiong to the database through a unix socket to //! separate the database name from the database path, such as //! `postgresql:///dbname?host=/var/run/postgresql`. //! - `socket_timeout` defined in seconds. If set, a query will return a //! `Timeout` error if it fails to resolve before given time. -//! - `connect_timeout` defined in seconds. Connecting to a -//! database will return a `ConnectTimeout` error if taking more than the -//! defined value. Defaults to 5 seconds, if set to 0, no timeout. +//! //! - `pool_timeout` defined in seconds. If all connections are in use, the //! database will return a `PoolTimeout` error after waiting for the given time. -//! If set to zero, no timeout. -//! - `pgbouncer` either `true` or `false`. If set, allows usage with the -//! pgBouncer connection pool in transaction mode. Additionally a transaction -//! is required for every query for the mode to work. When starting a new -//! transaction, a deallocation query `DEALLOCATE ALL` is executed right after -//! `BEGIN` to avoid possible collisions with statements created in other -//! sessions. //! - `statement_cache_size`, number of prepared statements kept cached. -//! Defaults to 500. If `pgbouncer` mode is enabled, caching is always off. +//! Defaults to 500, which means caching is off. If `pgbouncer` mode is enabled, +//! caching is always off. //! //! ## MySQL //! @@ -76,14 +82,17 @@ //! - `sslaccept` either `strict` or `accept_invalid_certs`. If strict, the //! certificate needs to be valid and in the CA certificates. //! `accept_invalid_certs` accepts any certificate from the server and can -//! lead to weakened security. Defaults to `strict`. +//! lead to weakened security. Defaults to `accept_invalid_certs`. +//! +//! - `connect_timeout` defined in seconds. Connecting to a +//! database will return a `ConnectTimeout` error if taking more than the +//! defined value. Defaults to 5 seconds, if set to 0, no timeout. +//! //! - `socket` needed when connecting to MySQL database through a unix //! socket. When set, the host parameter is dismissed. //! - `socket_timeout` defined in seconds. If set, a query will return a //! `Timeout` error if it fails to resolve before given time. -//! - `connect_timeout` defined in seconds. Connecting to a -//! database will return a `ConnectTimeout` error if taking more than the -//! defined value. Defaults to 5 seconds, if set to 0, no timeout. +//! //! - `pool_timeout` defined in seconds. If all connections are in use, the //! database will return a `PoolTimeout` error after waiting for the given time. //! If set to zero, no timeout. @@ -92,12 +101,14 @@ //! //! ## Microsoft SQL Server //! -//! - `encrypt` if set to `true` encrypts all traffic over TLS. If `false`, only -//! the login details are encrypted. A special value `DANGER_PLAINTEXT` will -//! disable TLS completely, including sending login credentials as plaintext. //! - `user` sets the login name. //! - `password` sets the login password. //! - `database` sets the database to connect to. +//! - `schema` the name of the lookup schema. Only stored to the connection, +//! must be used in every query to be effective. +//! - `encrypt` if set to `true` encrypts all traffic over TLS. If `false`, only +//! the login details are encrypted. A special value `DANGER_PLAINTEXT` will +//! disable TLS completely, including sending login credentials as plaintext. //! - `trustServerCertificate` if set to `true`, accepts any kind of certificate //! from the server. //! - `socketTimeout` defined in seconds. If set, a query will return a @@ -110,8 +121,6 @@ //! If set to zero, no timeout. //! - `connectionLimit` defines the maximum number of connections opened to the //! database. -//! - `schema` the name of the lookup schema. Only stored to the connection, -//! must be used in every query to be effective. //! - `isolationLevel` the transaction isolation level. Possible values: //! `READ UNCOMMITTED`, `READ COMMITTED`, `REPEATABLE READ`, `SNAPSHOT`, //! `SERIALIZABLE`. diff --git a/src/single.rs b/src/single.rs index 177cc250c..ef59d44bc 100644 --- a/src/single.rs +++ b/src/single.rs @@ -59,6 +59,14 @@ impl Quaint { /// /// PostgreSQL: /// + /// - `schema` the default search path. + /// - `pgbouncer` either `true` or `false`. If set, allows usage with the + /// pgBouncer connection pool in transaction mode. Additionally a transaction + /// is required for every query for the mode to work. When starting a new + /// transaction, a deallocation query `DEALLOCATE ALL` is executed right after + /// `BEGIN` to avoid possible collisions with statements created in other + /// sessions. + /// /// - `sslmode` either `disable`, `prefer` or `require`. [Read more](https://docs.rs/tokio-postgres/0.5.0-alpha.1/tokio_postgres/config/enum.SslMode.html) /// - `sslcert` should point to a PEM certificate file. /// - `sslidentity` should point to a PKCS12 certificate database. @@ -67,22 +75,18 @@ impl Quaint { /// certificate needs to be valid and in the CA certificates. /// `accept_invalid_certs` accepts any certificate from the server and can /// lead to weakened security. Defaults to `strict`. - /// - `schema` the default search path. + /// + /// - `connect_timeout` defined in seconds (default: 5). Connecting to a + /// database will return a `ConnectTimeout` error if taking more than the + /// defined value. + /// /// - `host` additionally the host can be given as a parameter, typically in /// cases when connectiong to the database through a unix socket to /// separate the database name from the database path, such as /// `postgresql:///dbname?host=/var/run/postgresql`. /// - `socket_timeout` defined in seconds. If set, a query will return a /// `Timeout` error if it fails to resolve before given time. - /// - `connect_timeout` defined in seconds (default: 5). Connecting to a - /// database will return a `ConnectTimeout` error if taking more than the - /// defined value. - /// - `pgbouncer` either `true` or `false`. If set, allows usage with the - /// pgBouncer connection pool in transaction mode. Additionally a transaction - /// is required for every query for the mode to work. When starting a new - /// transaction, a deallocation query `DEALLOCATE ALL` is executed right after - /// `BEGIN` to avoid possible collisions with statements created in other - /// sessions. + /// /// - `statement_cache_size`, number of prepared statements kept cached. /// Defaults to 500, which means caching is off. If `pgbouncer` mode is enabled, /// caching is always off. @@ -95,23 +99,31 @@ impl Quaint { /// - `sslaccept` either `strict` or `accept_invalid_certs`. If strict, the /// certificate needs to be valid and in the CA certificates. /// `accept_invalid_certs` accepts any certificate from the server and can - /// lead to weakened security. Defaults to `strict`. + /// lead to weakened security. Defaults to `accept_invalid_certs`. + /// + /// - `connect_timeout` defined in seconds (default: 5). Connecting to a + /// database will return a `ConnectTimeout` error if taking more than the + /// defined value. + /// /// - `socket` needed when connecting to MySQL database through a unix /// socket. When set, the host parameter is dismissed. /// - `socket_timeout` defined in seconds. If set, a query will return a /// `Timeout` error if it fails to resolve before given time. - /// - `connect_timeout` defined in seconds (default: 5). Connecting to a - /// database will return a `ConnectTimeout` error if taking more than the - /// defined value. + /// + /// - `statement_cache_size`, number of prepared statements kept cached. + /// Defaults to 500, which means caching is off. If `pgbouncer` mode is enabled, + /// caching is always off. /// /// Microsoft SQL Server: /// - /// - `encrypt` if set to `true` encrypts all traffic over TLS. If `false`, only - /// the login details are encrypted. A special value `DANGER_PLAINTEXT` will - /// disable TLS completely, including sending login credentials as plaintext. /// - `user` sets the login name. /// - `password` sets the login password. /// - `database` sets the database to connect to. + /// - `schema` the name of the lookup schema. Only stored to the connection, + /// must be used in every query to be effective. + /// - `encrypt` if set to `true` encrypts all traffic over TLS. If `false`, only + /// the login details are encrypted. A special value `DANGER_PLAINTEXT` will + /// disable TLS completely, including sending login credentials as plaintext. /// - `trustServerCertificate` if set to `true`, accepts any kind of certificate /// from the server. /// - `socketTimeout` defined in seconds. If set, a query will return a @@ -119,10 +131,6 @@ impl Quaint { /// - `connectTimeout` defined in seconds (default: 5). Connecting to a /// database will return a `ConnectTimeout` error if taking more than the /// defined value. - /// - `connectionLimit` defines the maximum number of connections opened to the - /// database. - /// - `schema` the name of the lookup schema. Only stored to the connection, - /// must be used in every query to be effective. /// - `isolationLevel` the transaction isolation level. Possible values: /// `READ UNCOMMITTED`, `READ COMMITTED`, `REPEATABLE READ`, `SNAPSHOT`, /// `SERIALIZABLE`.