restrict

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Relay

func Relay(client, backend net.Conn, cl *ConnLimits) error

Relay copies data bidirectionally between client and backend, enforcing limits via the ConnLimits tracker.

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

func NewRedisStore(client *redis.Client, keyPrefix string, m *metrics.Metrics) *RedisStore

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 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

func (s *RedisStore) AddBytes(ctx context.Context, user string, n int64) (int64, error)

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) GetConns

func (s *RedisStore) GetConns(ctx context.Context, user string) (int64, error)

GetConns returns the current connection count for a user.

func (*RedisStore) GetLastUsed

func (s *RedisStore) GetLastUsed(ctx context.Context, pgUser string) (time.Time, error)

GetLastUsed returns the last-used time for a provisioned user.

func (*RedisStore) IncrConns

func (s *RedisStore) IncrConns(ctx context.Context, user string) (int64, error)

IncrConns atomically increments the connection count for a user. Returns the new count.

func (*RedisStore) ReleaseLock added in v0.2.0

func (s *RedisStore) ReleaseLock(ctx context.Context, name string, token string) error

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

func NewTracker(store *RedisStore, m *metrics.Metrics, logger *slog.Logger) *Tracker

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL