From 54cbb65c1e5e680162bdceba3bd0bfdc6780740a Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Wed, 2 Sep 2020 13:12:52 +0200 Subject: [PATCH 01/10] runtime: add custom keep_alive functionality Adds a keep_alive attribute in the builder that can be customized only for the BlockingPool Fixes: #2585 --- tokio/src/runtime/blocking/pool.rs | 6 +++++- tokio/src/runtime/builder.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/tokio/src/runtime/blocking/pool.rs b/tokio/src/runtime/blocking/pool.rs index 47895fcf477..bec3dba17f2 100644 --- a/tokio/src/runtime/blocking/pool.rs +++ b/tokio/src/runtime/blocking/pool.rs @@ -47,6 +47,9 @@ struct Inner { // Maximum number of threads thread_cap: usize, + + // Customizable wait timeout + keep_alive: Duration, } struct Shared { @@ -110,6 +113,7 @@ impl BlockingPool { after_start: builder.after_start.clone(), before_stop: builder.before_stop.clone(), thread_cap, + keep_alive: builder.keep_alive.unwrap_or(KEEP_ALIVE), }), }, shutdown_rx, @@ -258,7 +262,7 @@ impl Inner { shared.num_idle += 1; while !shared.shutdown { - let lock_result = self.condvar.wait_timeout(shared, KEEP_ALIVE).unwrap(); + let lock_result = self.condvar.wait_timeout(shared, self.keep_alive).unwrap(); shared = lock_result.0; let timeout_result = lock_result.1; diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index db01cf5871e..2df7bf725a0 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -4,6 +4,7 @@ use crate::runtime::shell::Shell; use crate::runtime::{blocking, io, time, Callback, Runtime, Spawner}; use std::fmt; +use std::time::Duration; /// Builds Tokio Runtime with custom configuration values. /// @@ -65,6 +66,9 @@ pub struct Builder { /// To run before each worker thread stops pub(super) before_stop: Option, + + /// Customizable keep alive for BlockingPool + pub(super) keep_alive: Option, } pub(crate) type ThreadNameFn = std::sync::Arc String + Send + Sync + 'static>; @@ -108,6 +112,8 @@ impl Builder { // No worker thread callbacks after_start: None, before_stop: None, + + keep_alive: None, } } @@ -375,6 +381,26 @@ impl Builder { blocking_pool, }) } + + /// Sets a custom timeout for a thread in the `BlockingPool`. + /// + /// + /// # Example + /// + /// ``` + /// # use tokio::runtime; + /// # use std::time::Duration; + /// + /// # pub fn main() { + /// let rt = runtime::Builder::new() + /// .keep_alive(Duration::from_millis(100)) + /// .build(); + /// # } + /// ``` + pub fn keep_alive(&mut self, duration: Duration) -> &mut Self { + self.keep_alive = Some(duration); + self + } } cfg_io_driver! { From 7e1109ff7e34281cdaecc51cad4705e980d58078 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Wed, 2 Sep 2020 20:03:49 +0200 Subject: [PATCH 02/10] Update tokio/src/runtime/builder.rs Co-authored-by: Eliza Weisman --- tokio/src/runtime/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 2df7bf725a0..fc3ad662edc 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -397,7 +397,7 @@ impl Builder { /// .build(); /// # } /// ``` - pub fn keep_alive(&mut self, duration: Duration) -> &mut Self { + pub fn blocking_keep_alive(&mut self, duration: Duration) -> &mut Self { self.keep_alive = Some(duration); self } From c465cfb6b49ba943f2d2ed7860381a4c9e3a3daa Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Wed, 2 Sep 2020 21:49:32 +0200 Subject: [PATCH 03/10] rename method and add blocking feature --- tokio/src/runtime/builder.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index fc3ad662edc..1194c7044b5 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -4,6 +4,7 @@ use crate::runtime::shell::Shell; use crate::runtime::{blocking, io, time, Callback, Runtime, Spawner}; use std::fmt; +#[cfg(feature = "blocking")] use std::time::Duration; /// Builds Tokio Runtime with custom configuration values. @@ -67,6 +68,8 @@ pub struct Builder { /// To run before each worker thread stops pub(super) before_stop: Option, + #[cfg(feature = "blocking")] + #[cfg_attr(docsrs, doc(cfg(feature = "blocking")))] /// Customizable keep alive for BlockingPool pub(super) keep_alive: Option, } @@ -113,6 +116,7 @@ impl Builder { after_start: None, before_stop: None, + #[cfg(feature = "blocking")] keep_alive: None, } } @@ -382,8 +386,12 @@ impl Builder { }) } - /// Sets a custom timeout for a thread in the `BlockingPool`. - /// + #[cfg(feature = "blocking")] + #[cfg_attr(docsrs, doc(cfg(feature = "blocking")))] + /// Sets a custom timeout for a thread in the blocking pool. + /// By default, the timeout for a thread is constant and defined + /// in KEEP_ALIVE. In case the user wants to customize this + /// value, then they can use blocking_keep_alive() for it. /// /// # Example /// @@ -393,7 +401,7 @@ impl Builder { /// /// # pub fn main() { /// let rt = runtime::Builder::new() - /// .keep_alive(Duration::from_millis(100)) + /// .blocking_keep_alive(Duration::from_millis(100)) /// .build(); /// # } /// ``` From d582a5ecced2475ca0902d4ff66848b7c18e9b89 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Wed, 2 Sep 2020 22:53:23 +0200 Subject: [PATCH 04/10] fix compile error + remove KEEP_ALIVE ref from docs --- tokio/src/runtime/blocking/pool.rs | 6 +++++- tokio/src/runtime/builder.rs | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tokio/src/runtime/blocking/pool.rs b/tokio/src/runtime/blocking/pool.rs index bec3dba17f2..633021ededf 100644 --- a/tokio/src/runtime/blocking/pool.rs +++ b/tokio/src/runtime/blocking/pool.rs @@ -94,6 +94,10 @@ where impl BlockingPool { pub(crate) fn new(builder: &Builder, thread_cap: usize) -> BlockingPool { let (shutdown_tx, shutdown_rx) = shutdown::channel(); + #[cfg(feature = "blocking")] + let keep_alive = builder.keep_alive.unwrap_or(KEEP_ALIVE); + #[cfg(not(feature = "blocking"))] + let keep_alive = KEEP_ALIVE; BlockingPool { spawner: Spawner { @@ -113,7 +117,7 @@ impl BlockingPool { after_start: builder.after_start.clone(), before_stop: builder.before_stop.clone(), thread_cap, - keep_alive: builder.keep_alive.unwrap_or(KEEP_ALIVE), + keep_alive, }), }, shutdown_rx, diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 1194c7044b5..f6c9d4ec468 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -389,9 +389,9 @@ impl Builder { #[cfg(feature = "blocking")] #[cfg_attr(docsrs, doc(cfg(feature = "blocking")))] /// Sets a custom timeout for a thread in the blocking pool. - /// By default, the timeout for a thread is constant and defined - /// in KEEP_ALIVE. In case the user wants to customize this - /// value, then they can use blocking_keep_alive() for it. + /// + /// By default, the timeout for a thread is set to 10 seconds. This can + /// be overriden usinng .blocking_keep_alive(). /// /// # Example /// From c3b9bc410534ebcb4315bec8f68d4a99d5a254c2 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Wed, 2 Sep 2020 23:05:58 +0200 Subject: [PATCH 05/10] fix typo --- tokio/src/runtime/builder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index f6c9d4ec468..fb2b9f05833 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -70,7 +70,7 @@ pub struct Builder { #[cfg(feature = "blocking")] #[cfg_attr(docsrs, doc(cfg(feature = "blocking")))] - /// Customizable keep alive for BlockingPool + /// Customizable keep alive timeout for BlockingPool pub(super) keep_alive: Option, } @@ -391,7 +391,7 @@ impl Builder { /// Sets a custom timeout for a thread in the blocking pool. /// /// By default, the timeout for a thread is set to 10 seconds. This can - /// be overriden usinng .blocking_keep_alive(). + /// be overriden using .blocking_keep_alive(). /// /// # Example /// From f03aa82e2be149a107086d6a68117fa22b534f39 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Thu, 3 Sep 2020 09:38:00 +0200 Subject: [PATCH 06/10] simplify feature flagging --- tokio/src/runtime/blocking/pool.rs | 3 --- tokio/src/runtime/builder.rs | 3 --- 2 files changed, 6 deletions(-) diff --git a/tokio/src/runtime/blocking/pool.rs b/tokio/src/runtime/blocking/pool.rs index 633021ededf..71886322b9f 100644 --- a/tokio/src/runtime/blocking/pool.rs +++ b/tokio/src/runtime/blocking/pool.rs @@ -94,10 +94,7 @@ where impl BlockingPool { pub(crate) fn new(builder: &Builder, thread_cap: usize) -> BlockingPool { let (shutdown_tx, shutdown_rx) = shutdown::channel(); - #[cfg(feature = "blocking")] let keep_alive = builder.keep_alive.unwrap_or(KEEP_ALIVE); - #[cfg(not(feature = "blocking"))] - let keep_alive = KEEP_ALIVE; BlockingPool { spawner: Spawner { diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index fb2b9f05833..abdcaebf6fc 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -68,8 +68,6 @@ pub struct Builder { /// To run before each worker thread stops pub(super) before_stop: Option, - #[cfg(feature = "blocking")] - #[cfg_attr(docsrs, doc(cfg(feature = "blocking")))] /// Customizable keep alive timeout for BlockingPool pub(super) keep_alive: Option, } @@ -116,7 +114,6 @@ impl Builder { after_start: None, before_stop: None, - #[cfg(feature = "blocking")] keep_alive: None, } } From 71803eaa305d4fced2c3799e064ed25ed0d160f3 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Thu, 3 Sep 2020 10:44:21 +0200 Subject: [PATCH 07/10] remove feature on import --- tokio/src/runtime/builder.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index abdcaebf6fc..47b7f4c485c 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -4,7 +4,6 @@ use crate::runtime::shell::Shell; use crate::runtime::{blocking, io, time, Callback, Runtime, Spawner}; use std::fmt; -#[cfg(feature = "blocking")] use std::time::Duration; /// Builds Tokio Runtime with custom configuration values. From 7ca115075f120bdf9ee31e40d1f61384c44e5f22 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Fri, 4 Sep 2020 09:33:48 +0200 Subject: [PATCH 08/10] Revert "remove feature on import" This reverts commit 71803eaa305d4fced2c3799e064ed25ed0d160f3. --- tokio/src/runtime/builder.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 47b7f4c485c..abdcaebf6fc 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -4,6 +4,7 @@ use crate::runtime::shell::Shell; use crate::runtime::{blocking, io, time, Callback, Runtime, Spawner}; use std::fmt; +#[cfg(feature = "blocking")] use std::time::Duration; /// Builds Tokio Runtime with custom configuration values. From 9dec5618935885681b4a8095cf605599144c9603 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Fri, 4 Sep 2020 09:33:51 +0200 Subject: [PATCH 09/10] Revert "simplify feature flagging" This reverts commit f03aa82e2be149a107086d6a68117fa22b534f39. --- tokio/src/runtime/blocking/pool.rs | 3 +++ tokio/src/runtime/builder.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tokio/src/runtime/blocking/pool.rs b/tokio/src/runtime/blocking/pool.rs index 71886322b9f..633021ededf 100644 --- a/tokio/src/runtime/blocking/pool.rs +++ b/tokio/src/runtime/blocking/pool.rs @@ -94,7 +94,10 @@ where impl BlockingPool { pub(crate) fn new(builder: &Builder, thread_cap: usize) -> BlockingPool { let (shutdown_tx, shutdown_rx) = shutdown::channel(); + #[cfg(feature = "blocking")] let keep_alive = builder.keep_alive.unwrap_or(KEEP_ALIVE); + #[cfg(not(feature = "blocking"))] + let keep_alive = KEEP_ALIVE; BlockingPool { spawner: Spawner { diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index abdcaebf6fc..fb2b9f05833 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -68,6 +68,8 @@ pub struct Builder { /// To run before each worker thread stops pub(super) before_stop: Option, + #[cfg(feature = "blocking")] + #[cfg_attr(docsrs, doc(cfg(feature = "blocking")))] /// Customizable keep alive timeout for BlockingPool pub(super) keep_alive: Option, } @@ -114,6 +116,7 @@ impl Builder { after_start: None, before_stop: None, + #[cfg(feature = "blocking")] keep_alive: None, } } From bf76af6beff8758c99dfec15b7368c0a0f0f1458 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Sun, 6 Sep 2020 09:49:51 +0200 Subject: [PATCH 10/10] rename blocking_keep_alive() to thread_keep_alive() --- tokio/src/runtime/builder.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index fb2b9f05833..ed2cd251c35 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -391,7 +391,7 @@ impl Builder { /// Sets a custom timeout for a thread in the blocking pool. /// /// By default, the timeout for a thread is set to 10 seconds. This can - /// be overriden using .blocking_keep_alive(). + /// be overriden using .thread_keep_alive(). /// /// # Example /// @@ -401,11 +401,11 @@ impl Builder { /// /// # pub fn main() { /// let rt = runtime::Builder::new() - /// .blocking_keep_alive(Duration::from_millis(100)) + /// .thread_keep_alive(Duration::from_millis(100)) /// .build(); /// # } /// ``` - pub fn blocking_keep_alive(&mut self, duration: Duration) -> &mut Self { + pub fn thread_keep_alive(&mut self, duration: Duration) -> &mut Self { self.keep_alive = Some(duration); self }