How to use tokio sync Semaphore

Limit concurrent async tasks in Rust by acquiring a permit from a tokio::sync::Semaphore before entering a critical section.

Use tokio::sync::Semaphore to limit concurrent access to a resource by acquiring a permit before execution and releasing it afterward.

use tokio::sync::Semaphore;

#[tokio::main]
async fn main() {
    let semaphore = Semaphore::new(3);
    let permit = semaphore.acquire().await.unwrap();
    // Critical section
    drop(permit);
}