/go-rate-limiter

go rate limiter

Primary LanguageGoMIT LicenseMIT

Rate limiting for implementation for rueidis package

Go Reference

This package is based on rwz/redis-gcra and implements GCRA (aka leaky bucket) and go-redis redis_rate for go code inspiration for rate limiting based on Redis. The code requires Redis version 3.2 or newer since it relies on replicate_commands feature.

Installation

redis_rate supports 2 last Go versions and requires a Go version with modules support. So make sure to initialize a Go module:

go mod init github.com/my/repo
go get github.com/jsjain/go-rate-limiter

Example

package main

import (
	"context"
	"fmt"

	rl "github.com/jsjain/go-rate-limiter"
)

func ExampleNewLimiter() {
	client, err := rueidis.NewClient(rueidis.ClientOption{
	InitAddress:           []string{"127.0.0.1:6379"},
  })
  if err != nil {
    panic(err)
  }
	limiter := rl.NewLimiter(client)
	res, err := limiter.Allow(ctx, "key")
	if err != nil {
		panic(err)
	}
	fmt.Println("allowed", res.Allowed, "remaining", res.Remaining)
	// Output: allowed 1 remaining 9
}

Setting custom rate limits for different keys and default rate limit

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/alphadose/haxmap"
	rl "github.com/jsjain/go-rate-limiter"
	"github.com/redis/rueidis"
)

func NewLimiterWithCustomLimits() {
	client, err := rueidis.NewClient(rueidis.ClientOption{
		InitAddress: []string{"127.0.0.1:6379"},
	})
	if err != nil {
		panic(err)
	}
	customLimits := haxmap.New[string, Limit]()
	customLimits.Set("key1", Limit{Burst: 50, Rate: 50, Period: time.Second})
	limiter := rl.NewLimiter(client, WithCustomLimits(customLimits), WithRateLimit(rl.PerSecond(20)))
	
	res, err := limiter.Allow(context.Background(), "key")
	if err != nil {
		panic(err)
	}
	fmt.Println("allowed", res.Allowed, "remaining", res.Remaining)
	// Output: allowed 1 remaining 19

	res, err := limiter.Allow(context.Background(), "key1")
	if err != nil {
		panic(err)
	}
	fmt.Println("allowed", res.Allowed, "remaining", res.Remaining)
	// Output: allowed 1 remaining 49
}