#semaphore #tokio #async #sync #priority

ranked-semaphore

A high-performance ranked semaphore with priority support

5 releases

0.1.4 Jul 7, 2025
0.1.3 Jun 27, 2025
0.1.2 Jun 27, 2025
0.1.1 Jun 27, 2025
0.1.0 Jun 27, 2025

#525 in Asynchronous

Download history 144/week @ 2025-11-03 184/week @ 2025-11-10 133/week @ 2025-11-17 229/week @ 2025-11-24 172/week @ 2025-12-01 109/week @ 2025-12-08 177/week @ 2025-12-15 95/week @ 2025-12-22 39/week @ 2025-12-29 81/week @ 2026-01-05 104/week @ 2026-01-12 77/week @ 2026-01-19 87/week @ 2026-01-26 69/week @ 2026-02-02 91/week @ 2026-02-09 330/week @ 2026-02-16

591 downloads per month

MIT license

95KB
1K SLoC

Crates.io Docs.rs CI License: MIT Crates.io downloads

ranked-semaphore

A priority-aware semaphore for async Rust.


Features

  • Priority scheduling: Configurable priority-based task ordering
  • No runtime dependency: Works with any async runtime (Tokio, async-std, smol, etc.)
  • Flexible queue strategies: FIFO/LIFO and custom strategy
  • High performance: Optimized for low latency and high throughput

Quick Start

use ranked_semaphore::RankedSemaphore;

#[tokio::main]
async fn main() {
    // Create a semaphore with 3 permits, FIFO strategy
    let sem = RankedSemaphore::new_fifo(3);

    // Acquire a permit (default priority)
    let permit = sem.acquire().await.unwrap();

    // Acquire with custom priority
    let high = sem.acquire_with_priority(10).await.unwrap();

    // Release permits automatically on drop
    drop(permit);
    drop(high);
}

Advanced Usage

Priority-based API Rate Limiting

use ranked_semaphore::{RankedSemaphore, PriorityConfig, QueueStrategy};
use std::sync::Arc;

let config = PriorityConfig::new()
    .default_strategy(QueueStrategy::Fifo)
    .exact(10, QueueStrategy::Lifo);

let limiter = Arc::new(RankedSemaphore::new_with_config(2, config));

let _admin = limiter.acquire_with_priority(10).await.unwrap();
let _guest = limiter.acquire_with_priority(0).await.unwrap();

Runtime Support

The semaphore works with any async runtime. Runtime compatibility is ensured through comprehensive integration tests:

# Test Tokio runtime integration
cargo test --test runtime_tokio_test

# Test async-std runtime integration  
cargo test --test runtime_async_std_test

# Test smol runtime integration
cargo test --test runtime_smol_test

# Test futures runtime integration
cargo test --test runtime_futures_test

These tests serve as both verification and usage examples for each runtime.


License

MIT



No runtime deps