ratestor

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 5 Imported by: 1

README

RateStor: A Go Rate Limiting Library

RateStor codecov

RateStor is a simple, efficient, and thread-safe rate-limiting library for Go. It allows you to limit the rate of requests. The library provides a flexible way to define different limits for different keys.

Installation

To install ratestor, use the go get command:

go get github.com/ksysoev/ratestor

Usage

package main 

import (
    "fmt"
    "time"

    "github.com/ksysoev/ratestor"
)

func main () {
    stor := ratestor.NewRateStor()
    defer stor.Close()

    err := stor.Allow("user1_min", 1*time.Minute, 100)
    if err == ratestor.ErrRateLimitExceeded {
        fmt.Println("Minute limit is exceded")
    }

    err := stor.Allow("user1_10min", 10*time.Minute, 300)
    if err == ratestor.ErrRateLimitExceeded {
        fmt.Println("10 Minute limit is exceded")
    }
}

Contributing

Contributions to Wasabi are welcome! Please submit a pull request or create an issue to contribute.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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

func WithGCBatchSize(size int) Optition

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

func WithGCInterval(interval time.Duration) Optition

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

func NewRateStor(opts ...Optition) *RateStor

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

func (rs *RateStor) Allow(key string, period time.Duration, limit uint64) error

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.

func (*RateStor) Close

func (rs *RateStor) Close()

Close stops the RateStor instance and waits for all goroutines to complete.

type RateValue

type RateValue struct {
	ExpiresAt time.Time
	Value     uint64
	Limit     uint64
}

Jump to

Keyboard shortcuts

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