Skip to content

Commit d6ed00c

Browse files
authored
sync: Semaphore doc final cleanup (#6050)
1 parent 5d29136 commit d6ed00c

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

tokio/src/sync/semaphore.rs

+28-16
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use std::sync::Arc;
4747
/// }
4848
/// ```
4949
///
50-
/// ## Limit the number of simultaneously opened files in your program.
50+
/// ## Limit the number of simultaneously opened files in your program
5151
///
5252
/// Most operating systems have limits on the number of open file
5353
/// handles. Even in systems without explicit limits, resource constraints
@@ -76,7 +76,7 @@ use std::sync::Arc;
7676
/// }
7777
/// ```
7878
///
79-
/// ## Limit the number of incoming requests being handled at the same time.
79+
/// ## Limit the number of incoming requests being handled at the same time
8080
///
8181
/// Similar to limiting the number of simultaneously opened files, network handles
8282
/// are a limited resource. Allowing an unbounded amount of requests to be processed
@@ -125,19 +125,23 @@ use std::sync::Arc;
125125
///
126126
/// ## Prevent tests from running in parallel
127127
///
128-
/// By default, Rust runs tests in the same file in parallel. However, in some cases, running two tests in parallel may lead to problems.
129-
/// For example, this can happen when tests use the same database.
128+
/// By default, Rust runs tests in the same file in parallel. However, in some
129+
/// cases, running two tests in parallel may lead to problems. For example, this
130+
/// can happen when tests use the same database.
130131
///
131132
/// Consider the following scenario:
132-
/// 1. `test_insert`: Inserts a key-value pair into the database, then retrieves the value using the same key to verify the insertion.
133-
/// 2. `test_update`: Inserts a key, then updates the key to a new value and verifies that the value has been accurately updated.
134-
/// 3. `test_others`: A third test that doesn't modify the database state. It can run in parallel with the other tests.
133+
/// 1. `test_insert`: Inserts a key-value pair into the database, then retrieves
134+
/// the value using the same key to verify the insertion.
135+
/// 2. `test_update`: Inserts a key, then updates the key to a new value and
136+
/// verifies that the value has been accurately updated.
137+
/// 3. `test_others`: A third test that doesn't modify the database state. It
138+
/// can run in parallel with the other tests.
135139
///
136-
/// In this example, `test_insert` and `test_update` need to run in sequence to work, but it doesn't matter which test runs first.
137-
/// We can leverage a semaphore with a single permit to address this challenge.
140+
/// In this example, `test_insert` and `test_update` need to run in sequence to
141+
/// work, but it doesn't matter which test runs first. We can leverage a
142+
/// semaphore with a single permit to address this challenge.
138143
///
139144
/// ```
140-
/// use tokio::sync::Semaphore;
141145
/// # use tokio::sync::Mutex;
142146
/// # use std::collections::BTreeMap;
143147
/// # struct Database {
@@ -164,6 +168,7 @@ use std::sync::Arc;
164168
/// # *self.map.lock().await.get(key).unwrap()
165169
/// # }
166170
/// # }
171+
/// use tokio::sync::Semaphore;
167172
///
168173
/// // Initialize a static semaphore with only one permit, which is used to
169174
/// // prevent test_insert and test_update from running in parallel.
@@ -173,7 +178,7 @@ use std::sync::Arc;
173178
/// static DB: Database = Database::setup();
174179
///
175180
/// #[tokio::test]
176-
/// # async fn fake_test() {}
181+
/// # async fn fake_test_insert() {}
177182
/// async fn test_insert() {
178183
/// // Acquire permit before proceeding. Since the semaphore has only one permit,
179184
/// // the test will wait if the permit is already acquired by other tests.
@@ -196,7 +201,7 @@ use std::sync::Arc;
196201
/// }
197202
///
198203
/// #[tokio::test]
199-
/// # async fn fake_test() {}
204+
/// # async fn fake_test_update() {}
200205
/// async fn test_update() {
201206
/// // Acquire permit before proceeding. Since the semaphore has only one permit,
202207
/// // the test will wait if the permit is already acquired by other tests.
@@ -221,12 +226,12 @@ use std::sync::Arc;
221226
/// }
222227
///
223228
/// #[tokio::test]
224-
/// # async fn fake_test() {}
229+
/// # async fn fake_test_others() {}
225230
/// async fn test_others() {
226231
/// // This test can run in parallel with test_insert and test_update,
227232
/// // so it does not use PERMIT.
228233
/// }
229-
/// # #[tokio::main]
234+
/// # #[tokio::main(flavor = "current_thread")]
230235
/// # async fn main() {
231236
/// # test_insert().await;
232237
/// # test_update().await;
@@ -236,6 +241,8 @@ use std::sync::Arc;
236241
///
237242
/// ## Rate limiting using a token bucket
238243
///
244+
/// This example showcases the [`add_permits`] and [`SemaphorePermit::forget`] methods.
245+
///
239246
/// Many applications and systems have constraints on the rate at which certain
240247
/// operations should occur. Exceeding this rate can result in suboptimal
241248
/// performance or even errors.
@@ -256,6 +263,8 @@ use std::sync::Arc;
256263
/// lot of cpu constantly looping and sleeping.
257264
///
258265
/// [token bucket]: https://en.wikipedia.org/wiki/Token_bucket
266+
/// [`add_permits`]: crate::sync::Semaphore::add_permits
267+
/// [`SemaphorePermit::forget`]: crate::sync::SemaphorePermit::forget
259268
/// ```
260269
/// use std::sync::Arc;
261270
/// use tokio::sync::Semaphore;
@@ -292,8 +301,11 @@ use std::sync::Arc;
292301
///
293302
/// async fn acquire(&self) {
294303
/// // This can return an error if the semaphore is closed, but we
295-
/// // never close it, so just ignore errors.
296-
/// let _ = self.sem.acquire().await;
304+
/// // never close it, so this error can never happen.
305+
/// let permit = self.sem.acquire().await.unwrap();
306+
/// // To avoid releasing the permit back to the semaphore, we use
307+
/// // the `SemaphorePermit::forget` method.
308+
/// permit.forget();
297309
/// }
298310
/// }
299311
///

0 commit comments

Comments
 (0)