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) AddBandwidthBytesMulti(ctx context.Context, user string, n int64, tiers []auth.BandwidthTier) (BandwidthResult, error)
- func (s *RedisStore) AddBytes(ctx context.Context, user string, n int64) (int64, error)
- func (s *RedisStore) DecrConns(ctx context.Context, user string) error
- func (s *RedisStore) GetBandwidthBytes(ctx context.Context, user string, period time.Duration) (int64, error)
- func (s *RedisStore) GetConns(ctx context.Context, user string) (int64, error)
- func (s *RedisStore) GetLastUsed(ctx context.Context, pgUser string) (time.Time, error)
- func (s *RedisStore) IncrConns(ctx context.Context, user string) (int64, 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) AddBandwidthBytesMulti ¶
func (s *RedisStore) AddBandwidthBytesMulti(ctx context.Context, user string, n int64, tiers []auth.BandwidthTier) (BandwidthResult, error)
AddBandwidthBytesMulti adds bytes to all bandwidth tiers using the sliding window. It iterates per-tier (not atomic across tiers). Returns which tier was exceeded, if any.
func (*RedisStore) AddBytes ¶
AddBytes adds to the total byte count for a user. Used for per-user aggregate tracking.
func (*RedisStore) DecrConns ¶
func (s *RedisStore) DecrConns(ctx context.Context, user string) error
DecrConns decrements the connection count for a user.
func (*RedisStore) GetBandwidthBytes ¶
func (s *RedisStore) GetBandwidthBytes(ctx context.Context, user string, period time.Duration) (int64, error)
GetBandwidthBytes returns the current bandwidth usage for a single period using the sliding window (read-only).
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. Returns the new count.
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) (func(), error)
Acquire checks and increments the connection count for a user. 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) *ConnLimits
WrapConn wraps a net.Conn with byte counting and limit enforcement.