ratelimiter

package
v2.7.3 Latest Latest
Warning

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

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

Documentation

Overview

Package ratelimiter provides a rate limiter

Index

Constants

View Source
const (
	StateNormal = iota
	StateLimiting
	StateDisabled
)

circuit breaker states

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Time  time.Time
	State string
}

Event defines the event of rate limiter

type EventListenerFunc

type EventListenerFunc func(event *Event)

EventListenerFunc is a listener function to listen state transit event

type MultiPolicy

type MultiPolicy struct {
	TimeoutDuration    time.Duration
	LimitRefreshPeriod time.Duration
	LimitForPeriod     []int
}

MultiPolicy defines the policy of a rate limiter

func NewMultiPolicy

func NewMultiPolicy(timeout, refresh time.Duration, limit []int) *MultiPolicy

NewMultiPolicy create and initialize a policy

type MultiRateLimiter

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

MultiRateLimiter defines a rate limiter

func NewMulti

func NewMulti(policy *MultiPolicy) *MultiRateLimiter

NewMulti creates a multi rate limiter based on `policy`,

func (*MultiRateLimiter) AcquirePermission

func (rl *MultiRateLimiter) AcquirePermission(count []int) (bool, time.Duration, error)

AcquirePermission acquires a permission from the rate limiter. returns true if the request is permitted and false otherwise. when permitted, the caller should wait returned duration before action.

func (*MultiRateLimiter) SetState

func (rl *MultiRateLimiter) SetState(state State)

SetState sets the state of the rate limiter to `state`

func (*MultiRateLimiter) WaitPermission

func (rl *MultiRateLimiter) WaitPermission(counts []int) (bool, error)

WaitPermission waits a permission from the rate limiter returns true if the request is permitted and false if timed out

type Policy

type Policy struct {
	TimeoutDuration    time.Duration
	LimitRefreshPeriod time.Duration
	LimitForPeriod     int
}

Policy defines the policy of a rate limiter

func NewDefaultPolicy

func NewDefaultPolicy() *Policy

NewDefaultPolicy create and initialize a policy with default configuration

func NewPolicy

func NewPolicy(timeout, refresh time.Duration, limit int) *Policy

NewPolicy create and initialize a policy

type RateLimiter

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

RateLimiter defines a rate limiter

func New

func New(policy *Policy) *RateLimiter

New creates a rate limiter based on `policy`,

func (*RateLimiter) AcquireNPermission

func (rl *RateLimiter) AcquireNPermission(n int) (bool, time.Duration)

AcquireNPermission acquires N permission from the rate limiter. returns true if the request is permitted and false otherwise. when permitted, the caller should wait returned duration before action.

func (*RateLimiter) AcquirePermission

func (rl *RateLimiter) AcquirePermission() (bool, time.Duration)

AcquirePermission acquires a permission from the rate limiter. returns true if the request is permitted and false otherwise. when permitted, the caller should wait returned duration before action.

func (*RateLimiter) SetState

func (rl *RateLimiter) SetState(state State)

SetState sets the state of the rate limiter to `state`

func (*RateLimiter) SetStateListener

func (rl *RateLimiter) SetStateListener(listener EventListenerFunc)

SetStateListener sets a state listener for the RateLimiter

func (*RateLimiter) WaitPermission

func (rl *RateLimiter) WaitPermission() bool

WaitPermission waits a permission from the rate limiter returns true if the request is permitted and false if timed out

type State

type State uint8

State is rate limiter state

Jump to

Keyboard shortcuts

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