limit

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLimiter

func NewLimiter(size time.Duration, limit int64, newWindow NewWindow) (*Limiter, StopFunc)

NewLimiter creates a new limiter, and returns a function to stop the possible sync behaviour within the current window.

func NewLocalWindow

func NewLocalWindow() (*LocalWindow, StopFunc)

func NewSyncWindow

func NewSyncWindow(key string, syncer Synchronizer) (*SyncWindow, StopFunc)

NewSyncWindow creates an instance of SyncWindow with the given synchronizer.

Types

type HandleFunc

type HandleFunc func(SyncResponse)

type Limiter

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

func (*Limiter) Allow

func (lim *Limiter) Allow() bool

Allow is shorthand for AllowN(time.Now(), 1).

func (*Limiter) AllowN

func (lim *Limiter) AllowN(now time.Time, n int64) bool

AllowN reports whether n events may happen at time now.

func (*Limiter) Limit

func (lim *Limiter) Limit() int64

Limit returns the maximum events permitted to happen during one window size.

func (*Limiter) SetLimit

func (lim *Limiter) SetLimit(newLimit int64)

SetLimit sets a new Limit for the limiter.

func (*Limiter) Size

func (lim *Limiter) Size() time.Duration

Size returns the time duration of one window size. Note that the size is defined to be read-only, if you need to change the size, create a new limiter with a new size instead.

type LocalWindow

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

LocalWindow represents a window that ignores sync behavior entirely and only stores counters in memory.

func (*LocalWindow) AddCount

func (w *LocalWindow) AddCount(n int64)

func (*LocalWindow) Count

func (w *LocalWindow) Count() int64

func (*LocalWindow) Reset

func (w *LocalWindow) Reset(s time.Time, c int64)

func (*LocalWindow) Start

func (w *LocalWindow) Start() time.Time

func (*LocalWindow) Sync

func (w *LocalWindow) Sync(now time.Time)

type MakeFunc

type MakeFunc func() SyncRequest

type NewWindow

type NewWindow func() (Window, StopFunc)

NewWindow creates a new window, and returns a function to stop the possible sync behaviour within it.

type StopFunc

type StopFunc func()

StopFunc stops the window's sync behaviour.

type SyncRequest

type SyncRequest struct {
	Key     string
	Start   int64
	Count   int64
	Changes int64
}

type SyncResponse

type SyncResponse struct {
	// Whether the synchronization succeeds.
	OK    bool
	Start int64
	// The changes accumulated by the local limiter.
	Changes int64
	// The total changes accumulated by all the other limiters.
	OtherChanges int64
}

type SyncWindow

type SyncWindow struct {
	LocalWindow
	// contains filtered or unexported fields
}

SyncWindow represents a window that will sync counter data to the central datastore asynchronously.

Note that for the best coordination between the window and the synchronizer, the synchronization is not automatic but is driven by the call to Sync.

func (*SyncWindow) AddCount

func (w *SyncWindow) AddCount(n int64)

func (*SyncWindow) Reset

func (w *SyncWindow) Reset(s time.Time, c int64)

func (*SyncWindow) Sync

func (w *SyncWindow) Sync(now time.Time)

type Synchronizer

type Synchronizer interface {
	// Start starts the synchronization goroutine, if any.
	Start()

	// Stop stops the synchronization goroutine, if any, and waits for it to exit.
	Stop()

	// Sync sends a synchronization request.
	Sync(time.Time, MakeFunc, HandleFunc)
}

type Window

type Window interface {
	// Start returns the start boundary.
	Start() time.Time

	// Count returns the accumulated count.
	Count() int64

	// AddCount increments the accumulated count by n.
	AddCount(n int64)

	// Reset sets the state of the window with the given settings.
	Reset(s time.Time, c int64)

	// Sync tries to exchange data between the window and the central
	// datastore at time now, to keep the window's count up-to-date.
	Sync(now time.Time)
}

Window represents a fixed-window.

type WindowLimit

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

func NewWindowLimit

func NewWindowLimit(opts ...WindowLimitOption) *WindowLimit

max: limit num in a period period: second

func (*WindowLimit) Access

func (wl *WindowLimit) Access() bool

func (*WindowLimit) Count

func (wl *WindowLimit) Count() uint64

type WindowLimitOption

type WindowLimitOption func(w *WindowLimit)

func WithLimit

func WithLimit(cnt uint64) WindowLimitOption

func WithPeriod

func WithPeriod(period int64) WindowLimitOption

period sec

Jump to

Keyboard shortcuts

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