From 153fb2d20f8bcae46ef1c171b6b85668d5ac4d42 Mon Sep 17 00:00:00 2001 From: Mikhail Sukharev Date: Wed, 25 Sep 2019 14:53:30 +0300 Subject: [PATCH] fix test_query_timeout --- Cargo.toml | 12 ++++++------ README.md | 3 +++ src/lib.rs | 3 +++ src/pool/mod.rs | 3 +-- src/types/cmd.rs | 2 +- src/types/options.rs | 9 +++++---- src/types/query_result/mod.rs | 13 ++++++++----- 7 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 54bc093f..18b38e4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clickhouse-rs" -version = "0.1.15" +version = "0.1.16" authors = ["Mikhail Sukharev "] license = "MIT" homepage = "https://github.com/suharev7/clickhouse-rs" @@ -14,8 +14,8 @@ edition = "2018" exclude = ["test/*"] [dependencies] -log = "0.4.6" -futures = "0.1.28" +log = "0.4.8" +futures = "0.1.29" tokio = "0.1.22" tokio-timer = "0.2.11" hostname = "^0.1" @@ -30,8 +30,8 @@ byteorder = "1.3.1" failure = "0.1" failure_derive = "0.1" url="^2" -lazy_static = "1.3.0" +lazy_static = "1.4.0" [dev-dependencies] -env_logger = "0.6.2" -rand = "0.7.0" \ No newline at end of file +env_logger = "^0.7" +rand = "^0.7" \ No newline at end of file diff --git a/README.md b/README.md index a2732703..18127921 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,9 @@ parameters: - `retry_timeout` - Amount of time to wait before next retry. (defaults to `5 sec`). - `ping_timeout` - Timeout for ping (defaults to `500 ms`). +- `query_timeout` - Timeout for queries (defaults to `180 sec`). +- `query_block_timeout` - Timeout for each block in a query (defaults to `180 sec`). + example: ```url tcp://user:password@host:9000/clicks?compression=lz4&ping_timeout=42ms diff --git a/src/lib.rs b/src/lib.rs index faad8894..60e2a16e 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,9 @@ //! - `retry_timeout` - Amount of time to wait before next retry. (defaults to `5 sec`). //! - `ping_timeout` - Timeout for ping (defaults to `500 ms`). //! +//! - `query_timeout` - Timeout for queries (defaults to `180 sec`). +//! - `query_block_timeout` - Timeout for each block in a query (defaults to `180 sec`). +//! //! example: //! ```url //! tcp://user:password@host:9000/clicks?compression=lz4&ping_timeout=42ms diff --git a/src/pool/mod.rs b/src/pool/mod.rs index 306d7c19..1b3d59f8 100644 --- a/src/pool/mod.rs +++ b/src/pool/mod.rs @@ -470,13 +470,12 @@ mod test { #[test] fn test_query_timeout() { - let test_db_url = format!("{}{}", DATABASE_URL.as_str(), "&query_timeout=5ms"); + let test_db_url = format!("{}{}", DATABASE_URL.as_str(), "&query_timeout=10ms"); let pool = Pool::new(test_db_url.to_string()); let done = pool.get_handle() .and_then(|c| c.query("SELECT sleep(10)").fetch_all()); - run(done).unwrap_err(); let info = pool.info(); diff --git a/src/types/cmd.rs b/src/types/cmd.rs index c729f72f..86ac838d 100644 --- a/src/types/cmd.rs +++ b/src/types/cmd.rs @@ -1,5 +1,5 @@ use crate::{ - binary::{protocol, Encoder}, + binary::{Encoder, protocol}, client_info, types::{Block, ClickhouseResult, Context, Query}, }; diff --git a/src/types/options.rs b/src/types/options.rs index 6d841840..bd977bb9 100644 --- a/src/types/options.rs +++ b/src/types/options.rs @@ -19,6 +19,7 @@ const DEFAULT_MIN_CONNS: usize = 10; const DEFAULT_MAX_CONNS: usize = 20; #[derive(Debug)] +#[allow(clippy::large_enum_variant)] enum State { Raw(Options), Url(String), @@ -168,10 +169,10 @@ pub struct Options { /// Timeout for connection (defaults to `500 ms`) pub(crate) connection_timeout: Duration, - /// Timeout for queries (defaults to `180,000 ms`) + /// Timeout for queries (defaults to `180 sec`) pub(crate) query_timeout: Duration, - /// Timeout for each block in a query (defaults to `180,000 ms`) + /// Timeout for each block in a query (defaults to `180 sec`) pub(crate) query_block_timeout: Duration, } @@ -192,8 +193,8 @@ impl Default for Options { retry_timeout: Duration::from_secs(5), ping_timeout: Duration::from_millis(500), connection_timeout: Duration::from_millis(500), - query_timeout: Duration::from_millis(180_000), - query_block_timeout: Duration::from_millis(180_000), + query_timeout: Duration::from_secs(180), + query_block_timeout: Duration::from_secs(180), } } } diff --git a/src/types/query_result/mod.rs b/src/types/query_result/mod.rs index 1e5e60de..54a6e1cc 100644 --- a/src/types/query_result/mod.rs +++ b/src/types/query_result/mod.rs @@ -76,8 +76,6 @@ impl QueryResult { /// Fetch data from table. It returns a block that contains all rows. pub fn fetch_all(self) -> BoxFuture<(ClientHandle, Block)> { - let timeout = try_opt!(self.client.context.options.get()).query_timeout; - wrap_future( self.fold_blocks(Vec::new(), |mut blocks, block| { if !block.is_empty() { @@ -85,7 +83,6 @@ impl QueryResult { } Ok(blocks) }) - .timeout(timeout) .map_err(Error::from) .map(|(h, blocks)| (h, Block::concat(blocks.as_slice()))) ) @@ -99,6 +96,7 @@ impl QueryResult { Fut::Future: Send, T: Send + 'static, { + let timeout = try_opt!(self.client.context.options.get()).query_timeout; let context = self.client.context.clone(); let pool = self.client.pool.clone(); let release_pool = self.client.pool.clone(); @@ -122,9 +120,10 @@ impl QueryResult { _ => Either::Right(future::err(Error::Driver(DriverError::UnexpectedPacket))), }) .map(|(c, t)| (c.unwrap(), t)) + .timeout(timeout) .map_err(move |err| { release_pool.release_conn(); - err + err.into() }), ) } @@ -187,6 +186,7 @@ impl QueryResult { let context = c.context.clone(); let pool = c.pool.clone(); + let release_pool = c.pool.clone(); BlockStream::new( c.inner @@ -197,7 +197,10 @@ impl QueryResult { pool, ) .timeout(timeout) - .map_err(Error::from) + .map_err(move |err| { + release_pool.clone().release_conn(); + err.into() + }) }) }