Documentation
¶
Index ¶
- func Relay(client, backend net.Conn, cl *ConnLimits) error
- type BandwidthResult
- type ConnLimits
- func (cl *ConnLimits) BytesRead() int64
- func (cl *ConnLimits) BytesWritten() int64
- func (cl *ConnLimits) ReportBytes(n int64) error
- func (cl *ConnLimits) ReportRead(n int64) error
- func (cl *ConnLimits) ReportWrite(n int64) error
- func (cl *ConnLimits) SetFlushIntervalForTest(d time.Duration)
- func (cl *ConnLimits) Start()
- func (cl *ConnLimits) Stop()
- type RedisStore
- func (s *RedisStore) AcquireLock(ctx context.Context, name string, ttl time.Duration) (string, error)
- func (s *RedisStore) AddBandwidthBytesMulti(ctx context.Context, user, scope string, n int64, tiers []auth.BandwidthTier) (BandwidthResult, error)
- func (s *RedisStore) AddBytes(ctx context.Context, user, scope string, n int64) (int64, error)
- func (s *RedisStore) DecrConns(ctx context.Context, user, scope string) error
- func (s *RedisStore) GetBandwidthBytes(ctx context.Context, user, scope string, period time.Duration) (int64, error)
- func (s *RedisStore) GetConns(ctx context.Context, user, scope string) (int64, error)
- func (s *RedisStore) GetLastUsed(ctx context.Context, pgUser string) (time.Time, error)
- func (s *RedisStore) IncrConns(ctx context.Context, user, scope string) (int64, error)
- func (s *RedisStore) ReleaseLock(ctx context.Context, name string, token string) error
- func (s *RedisStore) TouchLastUsed(ctx context.Context, pgUser string) error
- type Tracker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BandwidthResult ¶
type BandwidthResult struct {
// Exceeded is true if any tier's limit was exceeded.
Exceeded bool
// ExceededTier is the index of the first exceeded tier, or -1 if none.
ExceededTier int
}
BandwidthResult holds the result of a multi-tier bandwidth check.
type ConnLimits ¶
type ConnLimits struct {
// contains filtered or unexported fields
}
ConnLimits tracks per-connection limits and batches byte updates to Redis.
func (*ConnLimits) BytesRead ¶
func (cl *ConnLimits) BytesRead() int64
BytesRead returns the total bytes read from the client side.
func (*ConnLimits) BytesWritten ¶
func (cl *ConnLimits) BytesWritten() int64
BytesWritten returns the total bytes written to the client side.
func (*ConnLimits) ReportBytes ¶
func (cl *ConnLimits) ReportBytes(n int64) error
ReportBytes records bytes transferred and checks per-connection limits. Returns an error if a limit is exceeded.
func (*ConnLimits) ReportRead ¶
func (cl *ConnLimits) ReportRead(n int64) error
ReportRead records bytes read from the client side.
func (*ConnLimits) ReportWrite ¶
func (cl *ConnLimits) ReportWrite(n int64) error
ReportWrite records bytes written to the client side.
func (*ConnLimits) SetFlushIntervalForTest ¶
func (cl *ConnLimits) SetFlushIntervalForTest(d time.Duration)
SetFlushIntervalForTest overrides the flush interval. Must be called before Start.
func (*ConnLimits) Start ¶
func (cl *ConnLimits) Start()
Start begins the background flush goroutine. Call Stop when done.
func (*ConnLimits) Stop ¶
func (cl *ConnLimits) Stop()
Stop flushes remaining bytes and stops the background goroutine.
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore manages per-user counters in Redis.
func NewRedisStore ¶
NewRedisStore creates a new Redis-backed counter store.
func (*RedisStore) AcquireLock ¶ added in v0.2.0
func (s *RedisStore) AcquireLock(ctx context.Context, name string, ttl time.Duration) (string, error)
AcquireLock attempts to acquire a distributed lock using Redis SET NX EX. Returns a token that must be passed to ReleaseLock, or empty string if not acquired.
func (*RedisStore) AddBandwidthBytesMulti ¶
func (s *RedisStore) AddBandwidthBytesMulti(ctx context.Context, user, scope string, n int64, tiers []auth.BandwidthTier) (BandwidthResult, error)
AddBandwidthBytesMulti adds bytes to all bandwidth tiers using the hierarchical sliding window. Writes cascade from scope (leaf) to user (root) at each tier. Returns which tier was exceeded at the leaf level, if any.
func (*RedisStore) AddBytes ¶
AddBytes adds to the total byte count, cascading up from scope to user total.
func (*RedisStore) DecrConns ¶
func (s *RedisStore) DecrConns(ctx context.Context, user, scope string) error
DecrConns decrements the connection count for a user/scope, cascading the decrement up to the user-level total.
func (*RedisStore) GetBandwidthBytes ¶
func (s *RedisStore) GetBandwidthBytes(ctx context.Context, user, scope string, period time.Duration) (int64, error)
GetBandwidthBytes returns the current bandwidth usage for a single period at a specific scope level using the sliding window (read-only). Pass scope="" for user-level total, or a listener name for per-endpoint.
func (*RedisStore) GetConns ¶
GetConns returns the current connection count at a specific scope level. Pass scope="" to read the user-level total, or a listener name for per-endpoint.
func (*RedisStore) GetLastUsed ¶
GetLastUsed returns the last-used time for a provisioned user.
func (*RedisStore) IncrConns ¶
IncrConns atomically increments the connection count for a user/scope, cascading the increment up to the user-level total. Returns the new leaf (scoped) count.
func (*RedisStore) ReleaseLock ¶ added in v0.2.0
ReleaseLock releases a distributed lock acquired by AcquireLock. The token must match the one returned by AcquireLock.
func (*RedisStore) TouchLastUsed ¶
func (s *RedisStore) TouchLastUsed(ctx context.Context, pgUser string) error
TouchLastUsed updates the last-used timestamp for a provisioned user.
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
Tracker orchestrates per-connection (local) and per-user (Redis) limits.
func NewTracker ¶
NewTracker creates a new limit tracker.
func (*Tracker) Acquire ¶
func (t *Tracker) Acquire(ctx context.Context, user string, limits auth.MergedLimits, listenerName string) (func(), error)
Acquire checks and increments the connection count for a user, enforcing both per-user (global) and per-endpoint limits. Returns a release function that must be called when the connection closes.
func (*Tracker) WrapConn ¶
func (t *Tracker) WrapConn(ctx context.Context, user string, limits auth.MergedLimits, listenerName string) *ConnLimits
WrapConn wraps a net.Conn with byte counting and limit enforcement.