ratelimit

package module
v0.0.0-...-7a8a9e4 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2020 License: MIT Imports: 5 Imported by: 1 module, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Rate Limit HTTP middleware

GoDoc Widget Travis Widget

Golang package for rate limiting HTTP endpoints based on context and request headers.

Under development

Goals

  • Simple but powerful API
  • Token Bucket algorithm (rate + burst)
  • Storage independent (Redis, In-Memory or any other K/V store)

License

Copyright (c) 2016 Vojtech Vitek

Licensed under the MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DownloadSpeed

func DownloadSpeed(keyFn KeyFn) *downloadBuilder
Example

Watch the download speed with wget http://localhost:3333/file -q --show-progress

package main

import (
	"net/http"
	"time"

	"github.com/VojtechVitek/ratelimit"
	"github.com/VojtechVitek/ratelimit/memory"
)

func main() {
	middleware := ratelimit.DownloadSpeed(ratelimit.IP).Rate(1024, time.Second).LimitBy(memory.New())

	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "/dev/random")
	})

	http.ListenAndServe(":3333", middleware(handler))
}

func IP

func IP(r *http.Request) string

IP returns unique key per request IP.

func NOP

func NOP(r *http.Request) string

NOP returns empty key for each request.

func Request

func Request(keyFn KeyFn) *requestBuilder
Example
package main

import (
	"net/http"
	"time"

	"github.com/VojtechVitek/ratelimit"
	"github.com/VojtechVitek/ratelimit/memory"
)

func main() {
	middleware := ratelimit.Request(ratelimit.IP).Rate(30, time.Minute).LimitBy(memory.New())

	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World!"))
	})

	http.ListenAndServe(":3333", middleware(handler))
}

func Throttle

func Throttle(limit int) func(http.Handler) http.Handler

Throttle is a middleware that limits number of currently processed requests at a time.

Example
package main

import (
	"net/http"
	"time"

	"github.com/VojtechVitek/ratelimit"
)

func main() {
	middleware := ratelimit.Throttle(1)

	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("working hard...\n\n"))
		if f, ok := w.(http.Flusher); ok {
			f.Flush()
		}
		time.Sleep(10 * time.Second)
		w.Write([]byte("done"))
	})

	http.ListenAndServe(":3333", middleware(handler))
}

Types

type KeyFn

type KeyFn func(r *http.Request) string

KeyFn is a function returning bucket key depending on request data.

type TokenBucketStore

type TokenBucketStore interface {
	InitRate(rate int, window time.Duration)
	Take(key string) (taken bool, remaining int, reset time.Time, err error)
}

TokenBucketStore is an interface for for any storage implementing Token Bucket algorithm.

Directories

Path Synopsis
examples
download command
request command
throttle command

Jump to

Keyboard shortcuts

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