ratelimit

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2017 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/echo/middleware/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

Overview

Copyright 2016 Wenhui Shen <www.webx.top>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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