Categorygithub.com/n-r-w/smoother
modulepackage
1.0.0
Repository: https://github.com/n-r-w/smoother.git
Documentation: pkg.go.dev

# README

Smoother

Smoother is a Go package that provides traffic smoothing capabilities by introducing controlled delays to maintain a target request rate. Unlike traditional rate limiters that reject excess requests, Smoother keeps all requests active while ensuring a consistent throughput through intelligent request spacing.

Go Reference CI Status

Features

  • 🎯 Maintains target RPS through controlled delays rather than request rejection
  • ⏱️ Dynamically adjusts wait times to smooth traffic spikes
  • 🔄 Keeps all requests active and processing
  • 🌊 Smooths out traffic spikes while ensuring all requests complete
  • 📡 Supports both local and distributed (Redis-based) implementations
  • 🛠️ Pluggable interface for custom implementations
  • 🔌 Thread-safe operation

Key Benefits

  • No dropped connections or rejected requests
  • Consistent, predictable throughput
  • Better resource utilization through traffic smoothing
  • Ideal for scenarios where request completion is more important than immediate error response
  • Suitable for both single-instance and distributed systems

Special features of the implementation Primary + fallback + circuit breaker

In the FallbackTryer, requests are sent concurrently to both the primary and fallback. When in the closed state, it chooses the primary if it succeeds, and falls back if it fails. Consequently, both implementations handle traffic simultaneously. Traffic is only removed from the primary when the circuit breaker opens

Installation

go get github.com/n-r-w/smoother

Rate Limiter backends

Usage

See the example for usage examples.

# Packages

nolint:revive // errors.
No description provided by the author

# Functions

DefaultBurstFromRPS calculates the empirical dependency of the burst, so that it does not freeze rps.
NewFallbackTryer creates a new FallbackTryer instance.
NewLocalTryer creates a new LocalTryer instance.
NewRateSmoother creates a new RateSmoother instance.
NewRedisRateTryer creates a new RedisTryer.
NewRedisThrottledTryer creates a new RedisThrottledTryer.
WithFallbackTryerBreakerOptions sets the circuit breaker options for the FallbackTryer.
WithFallbackTryerBreakerRunOptions sets the circuit breaker run options for the FallbackTryer.
WithFallbackTryerErrorFunc sets the error function for the FallbackTryer.
WithMultiplier sets the multiplier for the rate limit.
WithRedisRateTryerBurstFromRPSFunc sets the burst from rps function.
WithRedisThrottledTryerBurstFromRPSFunc sets the burst from rps function.
WithTimeout sets the timeout for requests to rate limiter.

# Constants

DefaultTimeout is the default timeout for requests to rate limiter.

# Structs

FallbackTryer implements a rate limiter that uses one primary Tryer and one fallback Tryer.
LocalTryer implements a simple rate limiter.
RateSmoother implements a rate limiter that blocks to ensure that the time spent between multiple.
RedisRateTryer implements a rate limiter using Redis and Redis Rate.
RedisThrottledTryer implements a rate limiter using Redis and throttled limiter.

# Interfaces

Tryer is an interface that responsible for trying to take n requests.

# Type aliases

BurstFromRPSFunc is the function to calculate the burst from rps.
No description provided by the author
No description provided by the author
LocalTryerOption is a function that configures a LocalTryer.
Option is a function that configures a RateSmoother.
RedisRateTryerOption is a function that configures a RedisRateTryer.
RedisThrottledTryerOption is a function that configures a RedisThrottledTryer.