rate

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package rate provides an in-memory sliding window counter store for Keep's rateCount() CEL function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock abstracts time for testing.

type Store

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

Store is a thread-safe sliding window counter store.

func NewStore

func NewStore() *Store

NewStore creates a new counter store using real time.

func NewStoreWithClock

func NewStoreWithClock(clock Clock) *Store

NewStoreWithClock creates a store with a custom clock for testing.

func (*Store) Count

func (s *Store) Count(key string, window time.Duration) int

Count returns the number of hits for key within the given window duration.

func (*Store) GC

func (s *Store) GC(maxAge time.Duration)

GC removes entries older than maxAge from all keys. Removes keys that become empty.

func (*Store) Increment

func (s *Store) Increment(key string)

Increment records a hit for the given key at the current time. If the store has reached maxKeys and the key is new, the increment is skipped. If a key has reached maxTimestampsPerKey, the oldest entries are trimmed.

func (*Store) OnKeyDropped added in v0.2.0

func (s *Store) OnKeyDropped(fn func(key string))

OnKeyDropped sets a callback that is invoked when a new key is dropped because the store has reached maxKeys capacity. The callback is called outside the store lock, so it is safe to perform logging or metrics collection. The callback MUST NOT call any Store methods.

func (*Store) StartGC

func (s *Store) StartGC(interval, maxAge time.Duration)

StartGC begins periodic garbage collection in a background goroutine. Runs every interval, removing entries older than maxAge. Call StopGC to stop the goroutine.

func (*Store) StopGC

func (s *Store) StopGC()

StopGC stops the periodic garbage collection goroutine. It is safe to call multiple times.

Jump to

Keyboard shortcuts

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