Giter Site home page Giter Site logo

go-ipratelimit's Introduction

Rate limiter for HTTP requests based on IP address for go

Go Report Card GoDoc License: MIT

This package provides a simple and easy to implement middleware for HTTP requests to rate limit the requests for each ip-address

imports "github.com/geekfarmer/go-ipratelimit"

Create a ip address rate limiter with a maximum number of requests can be process per second and with a bucket size.

var limiter = ipratelimit.New(1, 7)

Call IPRateLimitMiddleware() with all endpoints to process request. IPRateLimitMiddleware process request until it reached maximum requests

How to use?

import (
	"log"
  "net/http"

  "github.com/geekfarmer/go-ipratelimit"
)

// ipratelimit.New(r, b)
// r -> max events/second
// b -> max bucket size
var limiter = ipratelimit.New(1, 7)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", okHandler)

	if err := http.ListenAndServe(":8888", limiter.IPRateLimitMiddleware(mux)); err != nil {
		log.Fatalf("unable to start server: %s", err.Error())
	}
}

func okHandler(w http.ResponseWriter, r *http.Request) {
	// any very expensive database call
	w.Write([]byte("alles gut"))
}

Test HTTP load with vegeta

you can get all files in ./example folder

cd example
go get github.com/geekfarmer/ipratelimit
go build -o server .
./server

We can use vegaeta for HTTP load testing

brew install vegeta

we need to create a simple config file ( /example/request.config ) which have config for what requests do we want to produce

vegeta attack -duration=10s -rate=100 -targets=/PATH_TO/request.conf | vegeta report

License

The MIT License (MIT). Please see License File for more information.

forthebadge

go-ipratelimit's People

Contributors

geekfarmer avatar

Stargazers

 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.