Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Go
  3. semaphore

semaphore

MITGov2.5.0

A fast, resizable semaphore primitive for Go with weighted operations, context cancellation, and dynamic limit adjustment.

GitHubGitHub
178 stars9 forks0 contributors

What is semaphore?

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.

Target Audience

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.

Value Proposition

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.

Overview

Fast resizable golang semaphore primitive

Use Cases

Best For

  • Implementing rate limiting where requests have different resource costs (weighted operations).
  • Managing concurrent access to a pool of resources with dynamic capacity adjustments.
  • Building task coordination systems that require cancellable or timeout-based acquisitions via context support.
  • Optimizing high-performance concurrent applications where low-latency semaphore operations are critical.
  • Handling scenarios requiring non-blocking semaphore acquisition attempts for responsive system design.
  • Replacing channel-based semaphores in existing Go codebases for improved flexibility and performance.

Not Ideal For

  • Projects requiring only simple, unweighted semaphores where Go's built-in channels or sync primitives suffice
  • Teams building distributed systems needing cross-process or cross-machine synchronization, as this is an in-memory library
  • Developers prioritizing extensive documentation and large community support over performance optimizations

Pros & Cons

Pros

Weighted Semaphore Operations

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.

Context Integration

Integrates with Go's context package for cancellable and timeout-based acquisitions, making it robust for real-world concurrent applications with deadlines or cancellations.

Dynamic Limit Adjustment

Allows resizing the semaphore limit after creation via SetLimit, providing flexibility without reinitialization, a feature highlighted in the README.

Non-blocking TryAcquire

Offers TryAcquire for immediate, non-blocking permit attempts, useful for responsive systems where blocking is undesirable.

High Performance Design

Built using compare-and-swap operations, with benchmarks showing it outperforms many channel-based semaphores, especially under load with lower latency.

Cons

Allocation Overhead in Benchmarks

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.

Limited Fairness Guarantees

The README does not address fairness or starvation prevention, which might be a concern for applications requiring strict order in permit acquisition.

Sparse Documentation

Documentation is minimal, lacking detailed examples, error handling guidance, or best practices, which could increase onboarding time for complex use cases.

Frequently Asked Questions

Quick Stats

Stars178
Forks9
Contributors0
Open Issues0
Last commit5 years ago
CreatedSince 2017

Tags

#parallel-computing#context-cancellation#go-library#goroutines#mutex#golang#concurrency#go#semaphore#synchronization

Built With

G
Go

Included in

Go169.1k
Auto-fetched 1 day ago

Related Projects

antsants

🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.

Stars14,526
Forks1,442
Last commit1 month ago
concconc

Better structured concurrency for go

Stars10,435
Forks366
Last commit1 month ago
tunnytunny

A goroutine pool for Go

Stars4,021
Forks305
Last commit3 years ago
goworkergoworker

goworker is a Go-based background worker that runs 10 to 100,000* times faster than Ruby-based workers.

Stars2,847
Forks242
Last commit28 days ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub