WebsockexNova.Defaults.DefaultRateLimitHandler (WebsockexNova v0.1.1)

View Source

Default implementation of the RateLimitHandler behavior. This module now stores all rate limit state in the canonical WebsockexNova.ClientConn struct, under the :rate_limit field.

This module provides a standard token bucket algorithm implementation for rate limiting WebSocket requests. It supports:

  • Configurable token bucket capacity
  • Flexible token refill rates
  • Request queueing with priority handling
  • Burst handling through token accumulation

Configuration Options

  • :mode - Rate limiting mode (:normal, :always_allow, :always_queue, :always_reject)
  • :capacity - Maximum number of tokens in the bucket (default: 60)
  • :refill_rate - Number of tokens to add per interval (default: 1)
  • :refill_interval - Milliseconds between token refills (default: 1000)
  • :queue_limit - Maximum number of queued requests (default: 100)
  • :cost_map - Map of request types to their token costs (default: %{})

Examples

# Initialize with custom configuration
{:ok, state} = WebsockexNova.Defaults.DefaultRateLimitHandler.rate_limit_init(
  capacity: 100,
  refill_rate: 5,
  refill_interval: 1000,
  queue_limit: 50,
  mode: :normal,
  cost_map: %{
    subscription: 5,
    auth: 10,
    query: 1
  }
)

Summary

Functions

Check if a request can proceed based on current rate limits. Accepts and returns the canonical struct.

Process queued requests on a periodic tick. Accepts and returns the canonical struct.

Initialize the rate limit handler's state in the canonical struct.

Functions

check_rate_limit(request, conn)

Check if a request can proceed based on current rate limits. Accepts and returns the canonical struct.

handle_tick(conn)

Process queued requests on a periodic tick. Accepts and returns the canonical struct.

rate_limit_init(opts)

Initialize the rate limit handler's state in the canonical struct.