Crate permit[][src]

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

permit

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.
  • Similar to Golang’s context
  • Depends only on std.
  • forbid(unsafe_code)
  • 100% test coverage

Limitations

Alternatives

  • async_ctx
    • Good API
    • 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

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 an drop their permits.
top_permit.revoke().try_wait_for(
    core::time::Duration::from_secs(3));

Cargo Geiger Safety Report

Changelog

  • v0.1.1 - Make revoke return &Self
  • v0.1.0 - Initial version

Happy Contributors 🙂

Fixing bugs and adding features is easy and fast. Send us a pull request and we intend to:

  • Always respond within 24 hours
  • Provide clear & concrete feedback
  • Immediately make a new release for your accepted change

Structs

DeadlineExceeded
Permit

A struct for cancelling operations.