import "github.com/Bose/golimiter"
- Variables
- func EntrySyncInfo(e goCache.GenericCacheEntry) (currentCount int, lastSyncCount int, delta uint64)
- type Context
- type Limiter
- type LimiterStore
- func NewInMemoryLimiterStore(maxEntries int, opt ...Option) *LimiterStore
- func (store LimiterStore) CentralStorePeek(key string) (uint64, error)
- func (store LimiterStore) CentralStoreSync() error
- func (store LimiterStore) CentralStoreUpdates() error
- func (store LimiterStore) EntryCentralStoreExpiresIn(key string) (int64, error)
- func (store *LimiterStore) Get(ctx context.Context, key string, rate Rate) (lContext Context, err error)
- func (store *LimiterStore) Peek(ctx context.Context, key string, rate Rate) (lContext Context, err error)
- func (store *LimiterStore) StopCentralStoreUpdates() bool
- func (store LimiterStore) SyncEntryWithCentralStore(key string, opt ...Option) (updatedInMemoryCounter interface{})
- type Option
- type Rate
- type Store
- type StoreEntry
- type StoreOptions
context.go golimiter.go inmemory.go janitor.go options.go rate.go store.go
var (
// ErrNoCentralStore - no central store for the rate limiter was initialized
ErrNoCentralStore = errors.New("no central limiter store initialized")
)
func EntrySyncInfo(e goCache.GenericCacheEntry) (currentCount int, lastSyncCount int, delta uint64)
EntrySyncInfo - delta between last sync count
type Context struct {
Limit int64
Count int64
Remaining int64
Reset int64
Reached bool
Delay int64
}
Context is the limit context.
func GetContextFromState(now time.Time, rate Rate, expiration time.Time, count int64) Context
GetContextFromState generate a new Context from given state.
type Limiter struct {
Store Store
Rate Rate
}
Limiter is the limiter instance.
func New(store Store, rate Rate) *Limiter
New returns an instance of Limiter.
func (l *Limiter) Get(ctx context.Context, key string) (Context, error)
Get returns the limit for given identifier.
func (l *Limiter) Peek(ctx context.Context, key string) (Context, error)
Peek returns the limit for given identifier, without modification on current values.
type LimiterStore struct {
// contains filtered or unexported fields
}
LimiterStore - encapsulate the limiterStore
func NewInMemoryLimiterStore(maxEntries int, opt ...Option) *LimiterStore
NewInMemoryLimiterStore creates a new instance of memory store with defaults.
func (LimiterStore) CentralStorePeek
func (store LimiterStore) CentralStorePeek(key string) (uint64, error)
CentralStorePeek - peek at a value in the distributed central store
func (LimiterStore) CentralStoreSync
func (store LimiterStore) CentralStoreSync() error
CentralStoreSync - orchestrator for keeping the stores in sync that uses concurrency to get stuff done this syncs every entry, every time... see CentralStoreUpdates() which is far more efficient
func (LimiterStore) CentralStoreUpdates
func (store LimiterStore) CentralStoreUpdates() error
CentralStoreUpdates is used by the janitor.Run to "do" the updates via background workers
func (LimiterStore) EntryCentralStoreExpiresIn
func (store LimiterStore) EntryCentralStoreExpiresIn(key string) (int64, error)
EntryCentralStoreExpiresIn retrieves the entries TTL from the central store (if the limiter has one)
func (store *LimiterStore) Get(ctx context.Context, key string, rate Rate) (lContext Context, err error)
Get returns the limit for given identifier. (and increments the limiter's counter) if it's drifted too far from the central store, it will sync to the central store
func (store *LimiterStore) Peek(ctx context.Context, key string, rate Rate) (lContext Context, err error)
Peek returns the limit for given identifier, without modification on current values.
func (*LimiterStore) StopCentralStoreUpdates
func (store *LimiterStore) StopCentralStoreUpdates() bool
StopCentralStoreUpdates - stop the janitor
func (LimiterStore) SyncEntryWithCentralStore
func (store LimiterStore) SyncEntryWithCentralStore(key string, opt ...Option) (updatedInMemoryCounter interface{})
SyncEntryWithCentralStore - figures out what to sync for one entry and sets the in memory TTL to match the central store TTL
type Option func(*options)
Option - defines a func interface for passing in options to the NewInMemoryLimiterStore()
func WithIncrement(i int) Option
WithIncrement passes an optional increment
func WithLimiterExpiration(exp time.Duration) Option
WithLimiterExpSeconds sets the limiter's expiry in seconds
func WithRate(r Rate) Option
WithRate passes an optional rate
func WithUnsyncCounterLimit(deltaLimit uint64) Option
WithUnsyncCounterLimit - allows you to override the default limit to how far the in memory counter can drift from the central store
func WithUnsyncTimeLimit(timeLimit uint64) Option
WithUnsyncTimeLimit - allows you to override the default time limit (milliseconds) between syncs to the central store. this cannot be 0 or it will default to defaultUnsyncTimeLimit
func WithWorkerNumber(worker int) Option
WithWorkerNumber passes an optional worker number
type Rate struct {
Formatted string
Period time.Duration
Limit int64
Delay int64
}
Rate is the rate == 1-1-S-50
func NewRateFromFormatted(formatted string) (Rate, error)
NewRateFromFormatted - returns the rate from the formatted version ()
type Store interface {
// Get increments the limit for a given identtfier and returns its context
Get(ctx context.Context, key string, rate Rate) (Context, error)
// Peek returns the limit for given identifier, without modification on current values.
Peek(ctx context.Context, key string, rate Rate) (Context, error)
// CentrailStorePeek - Peek at an entry in the central store
CentralStorePeek(key string) (uint64, error)
// CentralStoreSync - syncs all the keys from in memory with the central store
CentralStoreSync() error
// CentralStoreUpdates - just sync the updated keys from in memory with the central store
CentralStoreUpdates() error
// SyncEntryWithCentralStore - sync just one key from in memory with the central store and return the new entry IF it's updated
SyncEntryWithCentralStore(key string, opt ...Option) interface{}
// StopCentralStoreUpdates - stop all janitor syncing to central store and return whether or not the message to stop as successfully sent
StopCentralStoreUpdates() bool
}
Store is the common interface for limiter stores.
type StoreEntry struct {
CurrentCount int
LastSyncedCount int
}
StoreEntry - represent the entry in the store
type StoreOptions struct {
// Prefix is the prefix to use for the key.
Prefix string
// MaxRetry is the maximum number of retry under race conditions.
MaxRetry int
// CleanUpInterval is the interval for cleanup.
CleanUpInterval time.Duration
}
StoreOptions are options for store.
Generated by godoc2md