Documentation
¶
Index ¶
- Constants
- type ProxyRateLimiter
- func (prl *ProxyRateLimiter) ActiveConnectionsForHost(host string) int
- func (prl *ProxyRateLimiter) GetProxyURL() *url.URL
- func (prl *ProxyRateLimiter) ListenAndServe() error
- func (prl *ProxyRateLimiter) RemoveHostLimit(host string)
- func (prl *ProxyRateLimiter) SetDefaultMaxConnectionsPerHost(limit int)
- func (prl *ProxyRateLimiter) SetHostLimit(host string, limit int)
- func (prl *ProxyRateLimiter) SetHostLimits(limits map[string]int)
- func (prl *ProxyRateLimiter) Start() error
- func (prl *ProxyRateLimiter) Stop(ctx context.Context) error
Constants ¶
const RedisKeyPrefix = "proxy_rate_limiter"
RedisKeyPrefix is the base prefix for Redis keys used in distributed mode.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ProxyRateLimiter ¶
type ProxyRateLimiter struct {
// contains filtered or unexported fields
}
ProxyRateLimiter is an HTTP/HTTPS forward proxy that enforces per-host concurrent connection limits locally or across a Redis-backed cluster.
func NewDistributedProxyRateLimiter ¶
func NewDistributedProxyRateLimiter( port uint16, defaultMaxConnectionsPerHost int, rOpts *redis.Options, maxLifetime time.Duration, logger *slog.Logger, ) *ProxyRateLimiter
NewDistributedProxyRateLimiter creates a Redis-backed proxy rate limiter that enforces per-host concurrent limits across multiple workers. defaultMaxConnectionsPerHost applies ONLY to hosts with no explicit limit set via SetHostLimit/SetHostLimits. 0 or less means "unlimited". maxLifetime is a safety TTL for this worker's Redis counters; avoid 0 unless you are sure cleanup always runs, otherwise stale counters can block traffic.
func NewProxyConnectionRateLimiter ¶
func NewProxyConnectionRateLimiter(port uint16, defaultMaxConnectionsPerHost int, logger *slog.Logger) *ProxyRateLimiter
NewProxyConnectionRateLimiter creates an in-memory proxy rate limiter that enforces per-host concurrent limits without Redis. defaultMaxConnectionsPerHost applies ONLY to hosts with no explicit limit set via SetHostLimit/SetHostLimits. 0 or less means "unlimited".
func (*ProxyRateLimiter) ActiveConnectionsForHost ¶
func (prl *ProxyRateLimiter) ActiveConnectionsForHost(host string) int
ActiveConnectionsForHost returns the current active connection count for a host. In distributed mode, it sums all workers' counters for that host. In local mode, it reads the in-memory map.
func (*ProxyRateLimiter) GetProxyURL ¶
func (prl *ProxyRateLimiter) GetProxyURL() *url.URL
GetProxyURL returns the proxy URL for use in http.Transport.Proxy. Returns nil if the proxy has not been started yet.
func (*ProxyRateLimiter) ListenAndServe ¶
func (prl *ProxyRateLimiter) ListenAndServe() error
ListenAndServe starts the proxy and blocks until it stops. It mirrors http.Server.ListenAndServe semantics.
func (*ProxyRateLimiter) RemoveHostLimit ¶
func (prl *ProxyRateLimiter) RemoveHostLimit(host string)
RemoveHostLimit clears any host-specific limit, falling back to the default.
func (*ProxyRateLimiter) SetDefaultMaxConnectionsPerHost ¶
func (prl *ProxyRateLimiter) SetDefaultMaxConnectionsPerHost(limit int)
SetDefaultMaxConnectionsPerHost sets the fallback per-host limit used when no explicit limit exists via SetHostLimit/SetHostLimits. 0 or less means "unlimited".
func (*ProxyRateLimiter) SetHostLimit ¶
func (prl *ProxyRateLimiter) SetHostLimit(host string, limit int)
SetHostLimit sets or updates the limit for a single host. A non-positive limit removes the host-specific override.
func (*ProxyRateLimiter) SetHostLimits ¶
func (prl *ProxyRateLimiter) SetHostLimits(limits map[string]int)
SetHostLimits replaces all per-host limits with the provided map. Hosts with non-positive limits are ignored.
func (*ProxyRateLimiter) Start ¶
func (prl *ProxyRateLimiter) Start() error
Start begins serving in the background without blocking.