5 releases

0.2.1 Apr 16, 2023
0.2.0 Apr 16, 2023
0.1.5 Jun 27, 2022
0.1.4 Apr 28, 2021
0.1.3 Mar 28, 2021

#265 in Concurrency

Download history 67/week @ 2024-05-26 5/week @ 2024-06-02 3/week @ 2024-06-09 15/week @ 2024-06-16 16/week @ 2024-06-23 2/week @ 2024-06-30 2/week @ 2024-07-07 14/week @ 2024-07-14 27/week @ 2024-07-21 63/week @ 2024-07-28 26/week @ 2024-08-04 32/week @ 2024-08-11 75/week @ 2024-08-18 27/week @ 2024-08-25 19/week @ 2024-09-01 70/week @ 2024-09-08

193 downloads per month
Used in 4 crates (3 directly)

Apache-2.0

20KB
254 lines

crates.io version license: Apache 2.0 unsafe forbidden pipeline status

permit::Permit is a struct for cancelling operations.

Use Cases

  • Graceful server shutdown
  • Cancel operations that take too long
  • Stop in-flight operations when revoking authorization

Features

  • Subordinate permits. Revoking a permit also revokes its subordinates, recursively.
  • Drop a permit to revoke its subordinates, recursively.
  • Wait for all subordinate permits to drop.
  • Implements Future. You can await a permit and return when it is revoked.
  • Similar to Golang's context
  • Depends only on std.
  • forbid(unsafe_code)
  • 100% test coverage

Limitations

Alternatives

  • async_ctx
    • Good API
    • Async only
  • stopper
    • Async only
  • io-context
    • Holds Any values
    • Unmaintained
  • ctx
    • Holds an Any value
    • API is a direct copy of Golang's context, even where that doesn't make sense for Rust. For example, to cancel, one must copy the context and call a returned Box<Fn>.
    • Unmaintained

Related Crates

Example

Graceful shutdown:

let top_permit = permit::Permit::new();
// Start some worker threads.
for _ in 0..5 {
    let permit = top_permit.new_sub();
    std::thread::spawn(move || {
        while !permit.is_revoked() {
            // ...
        }
    });
}
wait_for_shutdown_signal();
// Revoke all thread permits and wait for them to
// finish and drop their permits.
top_permit
    .revoke()
    .wait_subs_timeout(Duration::from_secs(3))
    .unwrap();

Cargo Geiger Safety Report


Metric output format: x/y
    x = unsafe code used by the build
    y = total unsafe code found in the crate

Symbols: 
    🔒  = No `unsafe` usage found, declares #![forbid(unsafe_code)]= No `unsafe` usage found, missing #![forbid(unsafe_code)]
    ☢️  = `unsafe` usage found

Functions  Expressions  Impls  Traits  Methods  Dependency

0/0        0/0          0/0    0/0     0/0      🔒  permit 0.2.1

0/0        0/0          0/0    0/0     0/0    

Changelog

  • v0.2.1 - Fix bug where sleep and sleep_until would sometimes not return early.
  • v0.2.0
    • Rename try_wait_for to wait_subs_timeout
    • Rename try_wait_until to wait_subs_deadline
    • Replace spinlock with Condvar in wait* methods
    • Remove wait
    • Add sleep and sleep_until
  • v0.1.5 - Implement Debug
  • v0.1.4 - Fix bug where revoke() and then wait() would not wait.
  • v0.1.3
    • Don't keep or wake stale std::task::Waker structs.
    • Eliminate race that causes unnecessary wake.
  • v0.1.2 - Implement Future
  • v0.1.1 - Make revoke return &Self
  • v0.1.0 - Initial version

License: Apache-2.0

No runtime deps