From d1868d75bfba401518f66d114ea41d33036be575 Mon Sep 17 00:00:00 2001 From: Alice Ryhl <aliceryhl@google.com> Date: Sat, 23 Sep 2023 22:10:31 +0200 Subject: [PATCH 1/3] make TokenBucket::close into destructor --- tokio/src/sync/semaphore.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tokio/src/sync/semaphore.rs b/tokio/src/sync/semaphore.rs index 1edbb156863..7338bd1ac6c 100644 --- a/tokio/src/sync/semaphore.rs +++ b/tokio/src/sync/semaphore.rs @@ -179,14 +179,18 @@ use std::sync::Arc; /// Self { jh, sem } /// } /// -/// async fn acquire(&self) -> Result<(), AcquireError> { -/// self.sem.acquire().await.map(|p| p.forget()) +/// async fn acquire(&self) { +/// // This can return an error if the semaphore is closed, but we +/// // never close it, so just ignore errors. +/// let _ = self.sem.acquire().await; /// } +/// } /// -/// async fn close(self) { -/// self.sem.close(); +/// impl Drop for TokenBucket { +/// fn drop(&mut self) { +/// // Kill the background task so it stops taking up resources when we +/// // don't need it anymore. /// self.jh.abort(); -/// let _ = self.jh.await; /// } /// } /// @@ -197,12 +201,10 @@ use std::sync::Arc; /// let bucket = TokenBucket::new(update_interval, capacity); /// /// for _ in 0..5 { -/// bucket.acquire().await.unwrap(); +/// bucket.acquire().await; /// /// // do the operation /// } -/// -/// bucket.close().await; /// } /// ``` /// From f6937f63c3513623c03c37b77c0b226014196209 Mon Sep 17 00:00:00 2001 From: Alice Ryhl <aliceryhl@google.com> Date: Sat, 23 Sep 2023 22:21:14 +0200 Subject: [PATCH 2/3] fix --- tokio/src/sync/semaphore.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tokio/src/sync/semaphore.rs b/tokio/src/sync/semaphore.rs index 7338bd1ac6c..bc75f0c596d 100644 --- a/tokio/src/sync/semaphore.rs +++ b/tokio/src/sync/semaphore.rs @@ -147,7 +147,7 @@ use std::sync::Arc; /// [token bucket]: https://en.wikipedia.org/wiki/Token_bucket /// ``` /// use std::sync::Arc; -/// use tokio::sync::{AcquireError, Semaphore}; +/// use tokio::sync::Semaphore; /// use tokio::time::{interval, Duration}; /// /// struct TokenBucket { @@ -196,6 +196,7 @@ use std::sync::Arc; /// /// #[tokio::main] /// async fn main() { +/// # tokio::time::pause(); /// let capacity = 5; // operation per second /// let update_interval = Duration::from_secs_f32(1.0 / capacity as f32); /// let bucket = TokenBucket::new(update_interval, capacity); From 89a93ac9691561b2a54a589841b512829e1f116b Mon Sep 17 00:00:00 2001 From: Alice Ryhl <aliceryhl@google.com> Date: Sat, 23 Sep 2023 22:34:05 +0200 Subject: [PATCH 3/3] pause time to speed up test execution --- tokio/src/sync/semaphore.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tokio/src/sync/semaphore.rs b/tokio/src/sync/semaphore.rs index bc75f0c596d..08a86f4b9f7 100644 --- a/tokio/src/sync/semaphore.rs +++ b/tokio/src/sync/semaphore.rs @@ -195,9 +195,10 @@ use std::sync::Arc; /// } /// /// #[tokio::main] +/// # async fn _hidden() {} +/// # #[tokio::main(flavor = "current_thread", start_paused = true)] /// async fn main() { -/// # tokio::time::pause(); -/// let capacity = 5; // operation per second +/// let capacity = 5; /// let update_interval = Duration::from_secs_f32(1.0 / capacity as f32); /// let bucket = TokenBucket::new(update_interval, capacity); ///