sharedcount

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseRedisURL

func ParseRedisURL(raw string) (addr, password string, db int, err error)

ParseRedisURL parses redis://[:password@]host:port[/db] into RedisCounter fields. Empty password/db are ok. redis://localhost:6379/0 is the common form.

Types

type MemoryCounter

type MemoryCounter struct {
	// contains filtered or unexported fields
}

MemoryCounter is the default single-process implementation.

func NewMemoryCounter

func NewMemoryCounter() *MemoryCounter

func (*MemoryCounter) Decr

func (m *MemoryCounter) Decr(ctx context.Context, key string, window time.Duration) (int64, error)

func (*MemoryCounter) Get

func (m *MemoryCounter) Get(ctx context.Context, key string) (int64, error)

func (*MemoryCounter) Incr

func (m *MemoryCounter) Incr(ctx context.Context, key string, window time.Duration) (int64, error)

func (*MemoryCounter) IncrBy

func (m *MemoryCounter) IncrBy(ctx context.Context, key string, delta int64, window time.Duration) (int64, error)

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)

func (*RedisCounter) Decr

func (r *RedisCounter) Decr(ctx context.Context, key string, window time.Duration) (int64, error)

func (*RedisCounter) Get

func (r *RedisCounter) Get(ctx context.Context, key string) (int64, error)

func (*RedisCounter) Incr

func (r *RedisCounter) Incr(ctx context.Context, key string, window time.Duration) (int64, error)

func (*RedisCounter) IncrBy

func (r *RedisCounter) IncrBy(ctx context.Context, key string, delta int64, window time.Duration) (int64, 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.

Jump to

Keyboard shortcuts

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