reili

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

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

Go to latest
Published: Dec 13, 2019 License: GPL-3.0 Imports: 5 Imported by: 0

README

reili

A simple but general purpose http Rate Limiter

Installing

$ go get -u github.com/mzampetakis/reili

Usage

Define your own identifier function

IdentifyVisitor(r *http.Request) (string, error)

following the interface that package reili provides

type VisitorIdentifier interface {
	IdentifyVisitor(r *http.Request) (string, error)
}

Create a rate limiter using reili.NewRateLimiter(reqPerSec, burstReq, visitorIdentifier) and then limit your server's mux using your limiter.

Examples

Limit by request IP

Documentation

Overview

Package reili provides a simple but general purpose http rate limiter http middleware. It uses the rate package and support limit and burst at second window. Visitor identifier is defined by the end user.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter is the basic struct of reili that NewRateLimiter returns. All field are not exported so it should be used only for variable initialization.

func NewRateLimiter

func NewRateLimiter(rps float64, burstReq int, identifier VisitorIdentifier) *RateLimiter

NewRateLimiter instantiates a new RateLimiter object. rps: requests per second burstReq: burst requests identifier: a Visitor Identifier interface

func (*RateLimiter) Limit

func (reili *RateLimiter) Limit(next http.Handler) http.Handler

Limit is an http limiter middleware that uses a specific RateLimiter to limit or allow http requests.

type VisitorIdentifier

type VisitorIdentifier interface {
	IdentifyVisitor(r *http.Request) (string, error)
}

VisitorIdentifier is an interface with a single function requirement. The function that requires is a IdentifyVisitor(r *http.Request) (string, error) which is used to identify a user by a string using the *http.Request It is used in every Limit call to identify the user.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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