ratelimiter

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateLimitUser

type RateLimitUser struct {
	CurrentRequests map[uuid.UUID]RequestInterval
}

RateLimitUser represents a user with a map of current requests.

type RateLimiter

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

func NewRateLimiter

func NewRateLimiter(rateLimitUserComputeTime time.Duration, rateLimitWindow time.Duration, concurrentRequestsLimit uint32, logger gethlog.Logger) *RateLimiter

func (*RateLimiter) AddRequest

func (rl *RateLimiter) AddRequest(userID common.Address, interval RequestInterval) uuid.UUID

AddRequest adds a new request interval to a user's current requests and returns the UUID.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(userID common.Address) (bool, uuid.UUID)

Allow checks if the user is allowed to make a request based on the rate limit threshold before comparing to the threshold also decays the score of the user based on the decay rate

func (*RateLimiter) CountOpenRequests

func (rl *RateLimiter) CountOpenRequests(userID common.Address) int

CountOpenRequests counts the number of requests without an End time set.

func (*RateLimiter) GetMaxConcurrentRequest

func (rl *RateLimiter) GetMaxConcurrentRequest() uint32

GetMaxConcurrentRequest returns the maximum number of concurrent requests allowed.

func (*RateLimiter) GetUserComputeTime

func (rl *RateLimiter) GetUserComputeTime() time.Duration

GetUserComputeTime returns the user compute time

func (*RateLimiter) IncrementRateLimitedRequests

func (rl *RateLimiter) IncrementRateLimitedRequests()

IncrementRateLimitedRequests increments the total requests counter by 1 with thread safety.

func (*RateLimiter) IncrementTotalRequests

func (rl *RateLimiter) IncrementTotalRequests()

IncrementTotalRequests increments the total requests counter by 1 with thread safety.

func (*RateLimiter) PruneRequests

func (rl *RateLimiter) PruneRequests()

PruneRequests deletes all requests that have ended before the rate limiter's window.

func (*RateLimiter) SetRequestEnd

func (rl *RateLimiter) SetRequestEnd(userID common.Address, id uuid.UUID)

SetRequestEnd updates the end time of a request interval given its UUID.

func (*RateLimiter) SumComputeTime

func (rl *RateLimiter) SumComputeTime(userID common.Address) time.Duration

SumComputeTime sums the compute time for requests within the rate limiter's window and returns it as uint32 milliseconds.

type RequestInterval

type RequestInterval struct {
	Start time.Time
	End   *time.Time // can be nil if the request is not over yet
}

RequestInterval represents an interval for a request with a start and optional end timestamp.

Jump to

Keyboard shortcuts

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