ratelimit

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2016 License: Apache-2.0 Imports: 7 Imported by: 2

README

tollbooth_echo

Echo middleware for rate limiting HTTP requests.

Five Minutes Tutorial

package main

import (
	"time"

	"github.com/webx-top/echo"
	"github.com/webx-top/echo/engine/standard"
	"github.com/webx-top/webx/lib/ratelimit"
)

func main() {
	e := echo.New()

	// Create a limiter struct.
	limiter := ratelimit.NewLimiter(1, time.Second)

	e.Get("/", echo.HandlerFunc(func(c echo.Context) error {
		return c.String(200, "Hello, World!")
	}), ratelimit.LimitHandler(limiter))

	e.Run(standard.New(":4444"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildKeys

func BuildKeys(limiter *config.Limiter, r engine.Request) [][]string

BuildKeys generates a slice of keys to rate-limit by given config and request structs.

func LimitByKeys

func LimitByKeys(limiter *config.Limiter, keys []string) *errors.HTTPError

LimitByKeys keeps track number of request made by keys separated by pipe. It returns HTTPError when limit is exceeded.

func LimitByRequest

func LimitByRequest(limiter *config.Limiter, r engine.Request) *errors.HTTPError

LimitByRequest builds keys based on http.Request struct, loops through all the keys, and check if any one of them returns HTTPError.

func LimitHandler

func LimitHandler(limiter *config.Limiter) echo.MiddlewareFunc

func LimitMiddleware

func LimitMiddleware(limiter *config.Limiter) echo.MiddlewareFunc

func New

func New(max int64, ttl time.Duration) *config.Limiter

New is a convenience function to config.NewLimiter.

func RemoteIP

func RemoteIP(ipLookups []string, r engine.Request) string

RemoteIP finds IP Address given http.Request struct.

func SetResponseHeaders

func SetResponseHeaders(limiter *config.Limiter, w engine.Response)

SetResponseHeaders configures X-Rate-Limit-Limit and X-Rate-Limit-Duration

func StringInSlice

func StringInSlice(sliceString []string, needle string) bool

StringInSlice finds needle in a slice of strings.

Types

This section is empty.

Directories

Path Synopsis
Package config provides data structure to configure rate-limiter.
Package config provides data structure to configure rate-limiter.
Package errors provide data structure for errors.
Package errors provide data structure for errors.

Jump to

Keyboard shortcuts

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