Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semaphore example using an Arc<Semaphore> for semaphore passing #5956

Merged
merged 33 commits into from Sep 19, 2023
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f658611
Semaphore example using an Arc<Semaphore> for semaphore passing
alexanderkirilin Aug 27, 2023
08912f7
Merge remote-tracking branch 'origin/master' into semaphore-example-file
alexanderkirilin Aug 28, 2023
eef8f07
Update
alexanderkirilin Aug 28, 2023
2f9d830
Fix example
alexanderkirilin Aug 28, 2023
9bc830e
Fix example
alexanderkirilin Aug 28, 2023
2805755
Fix example
alexanderkirilin Aug 28, 2023
c7151c7
Update tokio/src/sync/semaphore.rs
alexanderkirilin Aug 29, 2023
912e928
Merge branch 'master' into semaphore-example-file
alexanderkirilin Aug 29, 2023
0914b0f
Change example to actual request limiter
alexanderkirilin Aug 29, 2023
7428441
Swap loop to get around CI
alexanderkirilin Aug 29, 2023
0123816
No run docstring
alexanderkirilin Aug 29, 2023
b17bddc
Add comment
alexanderkirilin Aug 29, 2023
1a7a25b
Update tokio/src/sync/semaphore.rs
alexanderkirilin Aug 29, 2023
a60ff46
Merge remote-tracking branch 'origin/semaphore-example-file' into sem…
alexanderkirilin Aug 29, 2023
bd20813
Update
alexanderkirilin Aug 29, 2023
a8bb79d
Update
alexanderkirilin Aug 29, 2023
d605549
Update
alexanderkirilin Aug 29, 2023
ae49b96
Update tokio/src/sync/semaphore.rs
alexanderkirilin Aug 31, 2023
77d465b
Update
alexanderkirilin Aug 31, 2023
8de8bef
Update
alexanderkirilin Aug 31, 2023
8fd7a16
Update
alexanderkirilin Aug 31, 2023
b92d933
Update tokio/src/sync/semaphore.rs
alexanderkirilin Sep 1, 2023
cf4f6fb
Update tokio/src/sync/semaphore.rs
alexanderkirilin Sep 1, 2023
87af825
Update tokio/src/sync/semaphore.rs
alexanderkirilin Sep 1, 2023
05a2900
Update tokio/src/sync/semaphore.rs
alexanderkirilin Sep 2, 2023
f8afd1f
Update tokio/src/sync/semaphore.rs
alexanderkirilin Sep 2, 2023
b4790c4
Update
alexanderkirilin Sep 2, 2023
b364ca8
Update tokio/src/sync/semaphore.rs
alexanderkirilin Sep 6, 2023
9180840
Update tokio/src/sync/semaphore.rs
alexanderkirilin Sep 6, 2023
e18f86c
Update tokio/src/sync/semaphore.rs
alexanderkirilin Sep 6, 2023
7a94754
Merge branch 'master' into semaphore-example-file
alexanderkirilin Sep 6, 2023
1158412
Update
alexanderkirilin Sep 6, 2023
bc2079d
Merge remote-tracking branch 'origin/master' into semaphore-example-file
alexanderkirilin Sep 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 26 additions & 5 deletions tokio/src/sync/semaphore.rs
Expand Up @@ -47,7 +47,23 @@ use std::sync::Arc;
/// }
/// ```
///
/// Use [`Semaphore::acquire_owned`] to move permits across tasks:
/// Limit number of incoming requests being handled at the same time, using a task-safe semaphore
/// via [`Semaphore::acquire_owned`].
alexanderkirilin marked this conversation as resolved.
Show resolved Hide resolved
///
/// Similar to limiting the numer of simultaneous opened files, network handles are a limited resource.
/// Allowing an unbounded amount of requests to be processed could result in a denial-of-service,
/// and many other issues.
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
///
/// However, in contrast to the file example, this example uses a non-static, `Arc`-wrapped `Semaphore`
/// for more fine-grained lifetime and ownership control. The `Arc` allows multiple threads and tasks
/// to share ownership of the semaphore. Here we create a shallow-copy of the `Arc<Semaphore>` reference
/// using `clone`. Then we move the `Arc` into our task, where we will wait to acquire a new permit
/// via [`Semaphore::acquire_owned`] so we can start processing. Once our processing is done, we
/// will drop the permit to allow other tasks to acquire it.
///
/// If we leave the scope where the `Arc<Semaphore>` was defined, and references still exist in any
/// of our tasks to it, the `Semaphore` will continue to exist in a static-like context, until all
/// `Arc`s have been dropped.
///
/// ```
/// use std::sync::Arc;
Expand All @@ -59,18 +75,23 @@ use std::sync::Arc;
/// let mut join_handles = Vec::new();
///
/// for _ in 0..5 {
/// let permit = semaphore.clone().acquire_owned().await.unwrap();
/// let semaphore_clone = semaphore.clone();
/// join_handles.push(tokio::spawn(async move {
/// // perform task...
/// // explicitly own `permit` in the task
/// drop(permit);
/// perform_task(semaphore_clone).await;
/// }));
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
/// }
///
/// for handle in join_handles {
/// handle.await.unwrap();
/// }
/// }
///
/// async fn perform_task(semaphore: Arc<Semaphore>) {
/// let permit = semaphore.acquire_owned().await.unwrap();
/// // perform task...
/// // explicitly own `permit` in the task
/// drop(permit);
/// }
/// ```
///
/// [`PollSemaphore`]: https://docs.rs/tokio-util/latest/tokio_util/sync/struct.PollSemaphore.html
Expand Down