Lewati ke konten

All versions since v1.1.1

v1.1.1

🐛 Bug Fixes

  1. Fixed Header Value Formatting in nethttp Middleware
  • Issue: The nethttp middleware previously formatted rate limit header values using http.StatusText() instead of stringifying numerical values. This resulted in improper header values such as X-RateLimit-Limit: OK.
  • Fix: Replaced http.StatusText() with strconv.FormatInt(). Header values now correctly output formatted numeric strings (e.g., X-RateLimit-Limit: 100).
  1. Resolved Failover Race Condition in hybrid Driver
  • Issue: Under high concurrent loads during primary driver outages, multiple goroutines could simultaneously trigger the onError callback and update lastFailUnix timestamps.
  • Fix: Converted the isDown state transition to use atomic CompareAndSwap(false, true). Now, exactly one goroutine executes the circuit breaker transition and triggers the error callback per failure event.
  1. Added Guard Against nil Functions in WithKeyFunc
  • Issue: Passing WithKeyFunc(nil) explicitly during distlimit.New() initialization cleared the default key extraction function, leading to potential nil pointer dereference panics during Allow().
  • Fix: Added a nil check in WithKeyFunc(). If a nil function is provided, the Limiter safely preserves the default key extraction logic (“global”)

✨ New Features & Enhancements

  1. Manual Rate Limit Reset API (ResetKey & Driver.Reset)
  • Introduced Limiter.ResetKey(ctx context.Context, key string) error allowing operators to manually reset the rate limit counter for a specific key (e.g., following successful CAPTCHA verification or administrator action).
  • Added Reset(ctx context.Context, key string) error method to the Driver interface, implemented across all standard drivers:
    • Memory Driver: Removes state entries directly from the designated hash shard.
    • Redis Driver: Deletes the key and its corresponding sequence tracking key atomically in Redis.
    • Hybrid Driver: Delegates reset operations to both the primary and fallback storage drivers.
  1. RFC 6585 & IETF Draft Header Standardization
  • All HTTP framework middleware adapters (nethttp, gin, echo, echov5, fiber) now inject standard IETF RFC 6585 headers alongside legacy X-RateLimit-* headers for backward compatibility:
    • RateLimit-Limit: Maximum capacity for the window.
    • RateLimit-Remaining: Remaining quota in the current window.
    • RateLimit-Reset: Time remaining in seconds until window reset.
  1. Fallback Handling for Empty Keys
  • Updated Allow(), AllowKey(), and ResetKey() to automatically convert empty key strings (“”) to “global” to prevent key collisions across empty extractions.

📦 Upgrade Guide

To update your project to v1.1.1, run:

Terminal window
go get -u github.com/balramadan/distlimit@v1.1.1

What’s Changed

Full Changelog: https://github.com/balramadan/distlimit/compare/v1.1.0...v1.1.1

v1.2.0 Latest

✨ New Features & Enhancements

1. Pluggable Observability & Telemetry Engine (metrics/)

  • Core Observer Interface: Introduced metrics.Observer interface and metrics.Event snapshot data structure.
  • Zero-Allocation Guarantee: Evaluated at $0\text{ B/op}$ and $0\text{ allocs/op}$ when telemetry observers are disabled.
  • Prometheus Support (metrics/prometheus): Native collectors for evaluation counts (distlimit_requests_total), execution latency histograms (distlimit_evaluation_duration_seconds), and hybrid failovers (distlimit_hybrid_fallback_total).
  • OpenTelemetry Support (metrics/otel): Turnkey OTel MeterProvider integration (Int64Counter & Float64Histogram).

2. Lock-Free Dynamic Policy & Runtime Reload Engine

  • Atomic Policy Swapping: Limiter internal state refactored to atomic.Pointer[Policy]. Calling limiter.UpdatePolicy(limit, window) updates limits instantly with zero lock contention ($O(1)$ zero lock delay).
  • Multi-Tenant PolicyResolver: Added PolicyResolver interface allowing dynamic tier-based rate limit rules per key (e.g., VIP vs Free tiers) via WithPolicyResolver().

3. Middleware Route Labeling (WithRouteLabeling)

  • Context helper utilities ContextWithRoute(ctx, route) and RouteFromContext(ctx).
  • Added WithRouteLabeling(true) option across all middleware adapters (net/http, Gin, Echo v4, Echo v5, Fiber v3, and gRPC). This passes parametrized route patterns (e.g. /api/v1/users/:id or gRPC FullMethod) directly into metric labels.

📂 Working Examples


What’s Changed

Full Changelog: https://github.com/balramadan/distlimit/compare/v1.1.1...v1.2.0