Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRateLimitExceeded = fmt.Errorf("rate limit exceeded")
ErrRateLimitExceeded is an error that is returned when the rate limit is exceeded. This error indicates that the maximum number of requests allowed within a certain time period has been reached.
var ErrRateStorClosed = fmt.Errorf("rate stor is closed")
ErrRateStorClosed is an error that indicates the rate stor is closed.
Functions ¶
This section is empty.
Types ¶
type Optition ¶
type Optition func(*RateStor)
func WithGCBatchSize ¶
WithGCBatchSize sets the garbage collection batch size for the RateStor instance. The garbage collection batch size determines how many expired rate limit entries will be removed in each garbage collection cycle. The default garbage collection batch size is 100.
func WithGCInterval ¶
WithGCInterval sets the garbage collection interval for the RateStor instance. The garbage collection interval determines how often the RateStor instance will perform garbage collection to remove expired rate limit entries. The default garbage collection interval is 1 second.
type RateStor ¶
type RateStor struct {
// contains filtered or unexported fields
}
func NewRateStor ¶
NewRateStor creates a new instance of RateStor with the provided options. It initializes the necessary fields and starts a goroutine for periodic cleaning. The cleaning interval is set to 1 second by default. The provided options can be used to customize the behavior of the RateStor instance.
func (*RateStor) Allow ¶
Allow allows a request with the given key if the rate limit is not exceeded. It takes the key, period, and limit as parameters and returns an error if the rate limit is exceeded. The key is used to identify the request, the period is the duration for which the rate limit is enforced, and the limit is the maximum number of requests allowed within the given period. If the rate limit is not exceeded, the function increments the rate value for the given key. If the rate limit is exceeded, it returns an ErrRateLimitExceeded error.