A fast, resizable semaphore primitive for Go with weighted operations, context cancellation, and dynamic limit adjustment.
Semaphore is a high-performance Go library that implements a semaphore synchronization primitive using compare-and-swap (CAS) operations. It provides a flexible and efficient alternative to channel-based semaphores, enabling fine-grained control over concurrent resource access in Go applications by allowing weighted operations, dynamic resizing, and context support.
Go developers building concurrent applications that require precise control over resource access, such as those implementing rate limiting, connection pooling, or task coordination with variable resource demands.
Developers choose this library for its performance advantages over channel-based semaphores, as demonstrated in benchmarks, and its advanced features like weighted acquire/release, dynamic limit adjustments, and non-blocking TryAcquire, which are not all available in standard Go approaches.
Fast resizable golang semaphore primitive
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Supports acquire and release with custom weights, enabling fine-grained resource control for scenarios like rate limiting with variable costs, as shown in the usage examples.
Integrates with Go's context package for cancellable and timeout-based acquisitions, making it robust for real-world concurrent applications with deadlines or cancellations.
Allows resizing the semaphore limit after creation via SetLimit, providing flexibility without reinitialization, a feature highlighted in the README.
Offers TryAcquire for immediate, non-blocking permit attempts, useful for responsive systems where blocking is undesirable.
Built using compare-and-swap operations, with benchmarks showing it outperforms many channel-based semaphores, especially under load with lower latency.
Benchmarks reveal memory allocations per operation in some tests (e.g., 96 B/op), which could impact performance in allocation-sensitive environments compared to zero-allocation alternatives.
The README does not address fairness or starvation prevention, which might be a concern for applications requiring strict order in permit acquisition.
Documentation is minimal, lacking detailed examples, error handling guidance, or best practices, which could increase onboarding time for complex use cases.