ratelimit

package
v2.7.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2021 License: Apache-2.0 Imports: 6 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) *echo.HTTPError

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

func LimitByKeysWithCustomTokenBucketTTL added in v1.3.0

func LimitByKeysWithCustomTokenBucketTTL(limiter *config.Limiter, keys []string, bucketExpireTTL time.Duration) *echo.HTTPError

LimitByKeysWithCustomTokenBucketTTL keeps track number of request made by keys separated by pipe. It returns HTTPError when limit is exceeded. User can define a TTL for the key to expire

func LimitByRequest

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

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

func LimitByRequestWithExpiring added in v1.3.0

func LimitByRequestWithExpiring(limiter *config.Limiter, r engine.Request, bucketExpireTTL time.Duration) *echo.HTTPError

func LimitHandler

func LimitHandler(limiter *config.Limiter, bucketExpireTTL ...time.Duration) echo.MiddlewareFunc

func LimitMiddleware

func LimitMiddleware(limiter *config.Limiter, bucketExpireTTL ...time.Duration) echo.MiddlewareFunc

func New

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

New is a convenience function to config.NewLimiter.

func NewExpiringBuckets added in v1.3.0

func NewExpiringBuckets(max int64, ttl, bucketDefaultExpirationTTL, bucketExpireJobInterval time.Duration) *config.Limiter

NewExpiringBuckets 带过期时间 @param max 每个周期的最大连接数 @param ttl 周期 @param bucketDefaultExpirationTTL 默认有效时长 @param bucketExpireJobInterval 默认清理周期

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.

Jump to

Keyboard shortcuts

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