ratelimiter

package module
v0.0.0-...-e28fde5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2020 License: MIT Imports: 8 Imported by: 0

README

gin-ratelimiter

A light weight in-memory rate limiter middleware for gin-gonic.

Installation

go get github.com/wlwanpan/gin-ratelimiter

Usage

import (
  "github.com/wlwanpan/gin-ratelimiter"
  "github.com/gin-gonic/gin"
)

var limiter = ratelimiter.New(5, 10) // 5 request/s, 10 burst limit.

router := gin.Default()

router.Use(limiter.Limit())

Documentation

Index

Constants

View Source
const DefaultCacheTTL = time.Minute * 5

Variables

View Source
var (
	RateErr = errors.New("too many requests")
)

Functions

This section is empty.

Types

type Limiter

type Limiter struct {
	sync.Mutex

	RateLimit  rate.Limit
	BucketSize int
	// contains filtered or unexported fields
}

func New

func New(limit float64, size int, reqStore store) *Limiter

func (*Limiter) Limit

func (gl *Limiter) Limit() gin.HandlerFunc

RateLimit gin middleware that limit the requests per second per context. Each request is identified by the ip address and cached in the limiter, until dropped by the lru cache or until expired.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL