GithubHelp home page GithubHelp logo

shareed2k / http_limiter Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 7 KB

http_limiter using redis as store for rate limit with two algorithms for choosing sliding window, gcra leaky bucket

Go 100.00%
middleware http go redis rate-limiting gcra leaky-bucket sliding-window

http_limiter's Introduction

http_limiter is HTTP middleware for Go

http_limiter using redis as store for rate limit with two algorithms for choosing sliding window, gcra leaky bucket

Install

go get github.com/shareed2k/http_limiter

Example

package main

import (
	"log"
	"net/http"
	"time"

	"github.com/go-redis/redis/v7"
	limiter "github.com/shareed2k/http_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()

	// 3 requests per 10 seconds max
	cfg := limiter.Config{
		Rediser:     client,
		Max:         3,
		Burst:       3,
		Period:      10 * time.Second,
		Algorithm:   limiter.GCRAAlgorithm,
	}

	limiterHandler := limiter.NewWithConfig(cfg)

	var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello world"))
	})

	log.Fatal(http.ListenAndServe(":3000", limiterHandler(myHandler)))
}

Test

curl http://localhost:3000
...
< HTTP/1.1 200 OK
< Date: Fri, 03 Apr 2020 13:02:02 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 8
< X-Ratelimit-Limit: 3
< X-Ratelimit-Remaining: 2
< X-Ratelimit-Reset: 1585918925
...

curl http://localhost:3000
curl http://localhost:3000
curl http://localhost:3000

...
< HTTP/1.1 429 Too Many Requests
< Date: Fri, 03 Apr 2020 13:02:29 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 42
< Retry-After: 1585918951
...

http_limiter's People

Contributors

shareed2k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.