Coder Social home page Coder Social logo

shareed2k / go_limiter Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 6.0 20 KB

Rate limiting with few algorithms

License: BSD 2-Clause "Simplified" License

Makefile 1.19% Go 98.81%
golang rate-limiting gcra http middleware redis go-redis sliding-window leaky-bucket

go_limiter's Introduction

Rate limiting with few algorithms (Sliding Window, Leaky Bucket)

Build Status

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

Installation

go_limiter requires a Go version with Modules support and uses import versioning. So please make sure to initialize a Go module before installing go_limiter:

go get github.com/shareed2k/go_limiter

Import:

import "github.com/shareed2k/go_limiter"

Examplle

import (
	"log"
	"time"

	"github.com/redis/go-redis/v9"

	"github.com/shareed2k/go_limiter"
)

func main() {
	option, err := redis.ParseURL("redis://127.0.0.1:6379/0")
	if err != nil {
		log.Fatal(err)
	}
	client := redis.NewClient(option)
	_ = client.FlushDB().Err()

	limiter := go_limiter.NewLimiter(client)
	res, err := limiter.Allow("api_gateway_cache:klu4ik", &go_limiter.Limit{
		// or you can use go_limiter.SlidingWindowAlgorithm
		Algorithm: go_limiter.GCRAAlgorithm,
		Rate:      10,
		Period:    2 * time.Minute,
		Burst:     10,
	})

	if err != nil {
		log.Fatal(err)
	}

	log.Println("===> ", res.Allowed, res.Remaining)
	// Output: true 1
}
 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.