ratelimiter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2023 License: GPL-2.0 Imports: 6 Imported by: 0

README

Rate Limiter

A Go library for rate limiting user requests using both in-memory and Redis storage to ensure optimal performance and security.

Features

  • In-memory rate limiting using token bucket algorithm.
  • Redis backed storage for distributed rate limiting across multiple instances.
  • Supports custom rate limiting rules per endpoint.
  • Thread-safe operations for adding and retrieving configurations.

Installation

To install the ratelimiter package, run the following command:

go get github.com/firdasafridi/ratelimiter

Usage

Here's a basic example of how to use the ratelimiter library.

Creating a New Rate Limiter

package main

import (
	"time"
	"github.com/go-redis/redis/v8"
	"github.com/firdasafridi/ratelimiter"
)

func main() {
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		DB:   0, // use default DB
	})
	
	defaultConf := ratelimiter.RateLimitConfig{
		MaxRequests: 100,
		Interval:    time.Minute,
	}

	rl := ratelimiter.NewRateLimiter(rdb, defaultConf)
}

Adding a Custom Rate Limit for an Endpoint

rl.AddCustomRateLimit("/api/sensitive-endpoint", ratelimiter.RateLimitConfig{
	MaxRequests: 10,
	Interval:    time.Minute,
})

Checking if a Request is Allowed

import "context"

isAllowed := rl.Allow(context.TODO(), "userID", "/api/sensitive-endpoint")
if isAllowed {
	// Process the request
} else {
	// Deny the request
}

Configuration

The rate limiter uses the following configurations:

MaxRequests: The maximum number of requests allowed in a specified interval.

Interval: The time duration in which the MaxRequests are counted.

These configurations can be set globally for all endpoints and can also be customized per endpoint.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateLimitConfig

type RateLimitConfig struct {
	MaxRequests int           // Maximum number of requests allowed in a given interval.
	Interval    time.Duration // The interval in which MaxRequests are counted.
}

RateLimitConfig struct holds the configuration for rate limiting.

type RateLimiter

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

RateLimiter struct holds the configurations and states for the rate limiter.

func NewRateLimiter

func NewRateLimiter(rdb *redis.Client, defaultConf RateLimitConfig) *RateLimiter

NewRateLimiter function initializes and returns a new RateLimiter instance. It accepts a Redis client and a default rate limiting configuration.

func (*RateLimiter) AddCustomRateLimit

func (rl *RateLimiter) AddCustomRateLimit(endpoint string, conf RateLimitConfig)

AddCustomRateLimit function adds a custom rate limiting configuration for a specific endpoint. It is thread-safe and can be called concurrently.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(ctx context.Context, userId, endpoint string) bool

Allow function checks if a request from a user to a specific endpoint should be allowed or denied based on the rate limiting rules. It is not fully thread-safe as of this implementation and should not be called concurrently without consideration.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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