Documentation
¶
Index ¶
- func ParseRedisURL(raw string) (addr, password string, db int, err error)
- type MemoryCounter
- func (m *MemoryCounter) Decr(ctx context.Context, key string, window time.Duration) (int64, error)
- func (m *MemoryCounter) Get(ctx context.Context, key string) (int64, error)
- func (m *MemoryCounter) Incr(ctx context.Context, key string, window time.Duration) (int64, error)
- func (m *MemoryCounter) IncrBy(ctx context.Context, key string, delta int64, window time.Duration) (int64, error)
- type RedisCounter
- func (r *RedisCounter) Decr(ctx context.Context, key string, window time.Duration) (int64, error)
- func (r *RedisCounter) Get(ctx context.Context, key string) (int64, error)
- func (r *RedisCounter) Incr(ctx context.Context, key string, window time.Duration) (int64, error)
- func (r *RedisCounter) IncrBy(ctx context.Context, key string, delta int64, window time.Duration) (int64, error)
- type WindowCounter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type MemoryCounter ¶
type MemoryCounter struct {
// contains filtered or unexported fields
}
MemoryCounter is the default single-process implementation.
func NewMemoryCounter ¶
func NewMemoryCounter() *MemoryCounter
type RedisCounter ¶
type RedisCounter struct {
// contains filtered or unexported fields
}
RedisCounter is a minimal RESP client (INCR + PEXPIRE / GET) over TCP. No third-party dependency. Failures return errors for callers to fail-open.
Connections are pooled and reused: AUTH/SELECT run once per connection instead of once per command, a connection the server closed while it sat idle is replaced transparently, and every operation honors ctx cancellation/deadline on top of the per-operation timeout. Safe for concurrent use.
func NewRedisCounter ¶
func NewRedisCounter(redisURL string) (*RedisCounter, error)
type WindowCounter ¶
type WindowCounter interface {
// Incr increments key by 1 within window and returns the new count.
// Implementations may approximate sliding windows with fixed TTL buckets.
Incr(ctx context.Context, key string, window time.Duration) (count int64, err error)
// Decr decrements key by 1 (compensating rollback for a prior Incr,).
// Does not extend/refresh TTL. Count is floored at 0 for memory; Redis DECR
// may go negative, and a rollback that lands after the window expired drops
// the re-created non-positive key instead of leaving it behind without a TTL.
Decr(ctx context.Context, key string, window time.Duration) (count int64, err error)
// IncrBy increments key by delta within window and returns the new total.
// Used for TPM token reservations. delta==0 returns the current total.
// Negative delta is a compensating rollback and does not refresh TTL; like
// Decr it removes the key when the rollback takes the total to <= 0.
IncrBy(ctx context.Context, key string, delta int64, window time.Duration) (count int64, err error)
// Get returns the current count without incrementing.
Get(ctx context.Context, key string) (count int64, err error)
}
WindowCounter increments a named key inside a sliding/fixed window and returns the post-increment count. Used for multi-instance RPM/TPM admission.