Expand description
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 canawaita permit and return when it is revoked. - Similar to Golang’s
context - Depends only on
std. forbid(unsafe_code)- 100% test coverage
§Limitations
- Does not hold data values
- Allocates. Uses
alloc::sync::Arc.
§Alternatives
async_ctx- Good API
- Async only
stopper- Async only
io-context- Holds Any values
- Unmaintained
ctx
§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
§Changelog
- v0.2.1 - Fix bug where
sleepandsleep_untilwould sometimes not return early. - v0.2.0
- Rename
try_wait_fortowait_subs_timeout - Rename
try_wait_untiltowait_subs_deadline - Replace spinlock with Condvar in
wait*methods - Remove
wait - Add
sleepandsleep_until
- Rename
- v0.1.5 - Implement
Debug - v0.1.4 - Fix bug
where
revoke()and thenwait()would not wait. - v0.1.3
- Don’t keep or wake stale
std::task::Wakerstructs. - Eliminate race that causes unnecessary wake.
- Don’t keep or wake stale
- v0.1.2 - Implement
Future - v0.1.1 - Make
revokereturn&Self - v0.1.0 - Initial version
Structs§
- Deadline
Exceeded - Permit
- A struct for cancelling operations.
- Permit
Revoked