Coder Social home page Coder Social logo

fiber_limiter's Introduction

Limiter

fiber_limiter is based on rate which forks of golang.org/x/time/rate. The core algorithm is delay calculate of token bucket which supports burst.

Install

go get -u github.com/gofiber/fiber
go get -u github.com/kiyonlin/fiber-limiter

Config

Property Type Description Default
Filter func(*fiber.Ctx) bool Defines a function to skip middleware. nil
Limit int Defines the maximum frequency of requests and is represented as integer of requests per second. 10
Burst int Maximum burst size. 10
Message string Response Message. "Too many requests, please try again later."
StatusCode int Status Code. 429 Too Many Requests
Key func(*fiber.Ctx) string Allows to use a custom handler to create custom keys. func(c *fiber.Ctx) string { return c.IP() }
Handler func(*fiber.Ctx) Is called when a request hits the limit. func(c *fiber.Ctx) { c.Status(cfg.StatusCode).Format(cfg.Message) }

Example

package main

import (
	"github.com/gofiber/fiber"
	limiter "github.com/kiyonlin/fiber_limiter"
)

func main() {
	app := fiber.New()

	// 10 requests per second, support 10 burst
	cfg := limiter.Config{
		Limit: 10,
		Burst: 10,
	}

	app.Use(limiter.New(cfg))

	app.Get("/", func(c *fiber.Ctx) {
		c.Send("Welcome!")
	})

	app.Listen(3000)
}

Test

curl http://localhost:3000 -I
...
< HTTP/1.1 200 OK
< Date: Fri, 19 Jun 2020 05:43:51 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 8
< X-Ratelimit-Limit: 2
< X-Ratelimit-Remaining: 1
< X-Ratelimit-Reset: 1
...

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

curl http://localhost:3000 -I
...
< HTTP/1.1 429 Too Many Requests
< Date: Fri, 19 Jun 2020 05:43:52 GMT
< Content-Type: text/html
< Content-Length: 49
< Retry-After: 3
...

Custom limiter

We can set custom limiter for specific key so that every user can have a different limiter:

package main

import (
	"github.com/gofiber/fiber"
	limiter "github.com/kiyonlin/fiber_limiter"
	"github.com/kiyonlin/rate"
)

func main() {
	app := fiber.New()

	// 10 requests per second, support 100 burst
	limiter.Set("127.0.0.1", rate.NewLimiter(10, 100))

	app.Use(limiter.New())

	app.Get("/", func(c *fiber.Ctx) {
		c.Send("Welcome, VIP!")
	})

	app.Listen(3000)
}

fiber_limiter's People

Contributors

kiyonlin avatar

Stargazers

 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.