/go-doublelimiter

Primary LanguageGoMIT LicenseMIT

go-doublelimiter

A two-level rate limiter for Go that controls both global backend resource access and per-client concurrency.

Features

  • Two-level limiting: Global backend semaphore + per-client queues
  • Flexible configuration: Per-client limits and timeouts via callback functions
  • Type-safe: Generic implementation with comparable key types
  • Concurrent-safe: Built for high-concurrency environments
  • Memory efficient: Automatic cleanup of unused client semaphores

Installation

go get github.com/davidmz/go-doublelimiter

Quick Example

limiter := doublelimiter.New[string](10). // max 10 concurrent backend operations
    WithQueue(3).                          // default 3 operations per client
    WithTimeout(5*time.Second)             // default 5s timeout

release, err := limiter.Acquire("client-123")
if err != nil {
    // Handle ErrQueueFull or ErrTimeout
    return
}
defer release() // Always release when done

// Use your backend resource here

Documentation

For complete documentation and examples, see GoDoc.

License

MIT License - see LICENSE file for details.