Skip to content

feat: Add KeyedFairSemaphore#114

Open
tisonkun wants to merge 2 commits into
mainfrom
codex/keyed-fair-semaphore
Open

feat: Add KeyedFairSemaphore#114
tisonkun wants to merge 2 commits into
mainfrom
codex/keyed-fair-semaphore

Conversation

@tisonkun

@tisonkun tisonkun commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • add mea::keyed_fair_semaphore::KeyedFairSemaphore with borrowed and owned permits
  • schedule queued waiters by the fewest in-flight permits for their key, preserving queue-order ties
  • cover keyed fairness, per-key FIFO, cancellation, capacity stress, docs, and Send/Sync/Unpin assertions

Verification

  • cargo x test
  • cargo +nightly x lint

@tisonkun tisonkun changed the title Add KeyedFairSemaphore feat: Add KeyedFairSemaphore Jun 6, 2026
@tisonkun tisonkun requested a review from Copilot June 6, 2026 12:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new synchronization primitive, mea::keyed_fair_semaphore::KeyedFairSemaphore, to bound global concurrency while distributing permits fairly across independent “keys” (e.g., tenants/partitions) based on lowest in-flight counts with FIFO tie-breaking.

Changes:

  • Added the keyed_fair_semaphore module implementing KeyedFairSemaphore plus borrowed/owned permit types.
  • Added a dedicated test suite covering fairness behavior, FIFO within a key, cancellation, and capacity stress.
  • Updated crate docs/README to list and link the new primitive, and added Send/Sync/Unpin assertions in lib.rs tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
README.md Documents the new KeyedFairSemaphore and links to its docs.rs page.
mea/src/lib.rs Exposes the new module and adds trait assertions for the new types.
mea/src/keyed_fair_semaphore/mod.rs Implements the keyed fair semaphore, permits, and scheduling logic.
mea/src/keyed_fair_semaphore/tests.rs Adds unit/stress tests for fairness, cancellation, and capacity invariants.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mea/src/keyed_fair_semaphore/tests.rs
Comment thread mea/src/keyed_fair_semaphore/tests.rs
Comment thread mea/src/keyed_fair_semaphore/tests.rs
Comment thread mea/src/keyed_fair_semaphore/tests.rs
Comment on lines +370 to +382
fn release(&mut self, key: &Arc<K>) {
let Some(in_flight) = self.in_flight_by_key.get_mut(key) else {
return;
};

*in_flight -= 1;
if *in_flight == 0 {
self.in_flight_by_key.remove(key);
}

self.available_permits += 1;
debug_assert!(self.available_permits <= self.total_permits);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants