ratelimit

package module
v0.0.0-...-6d1edab Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2016 License: MIT Imports: 5 Imported by: ? modules, 0 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

ratelimit Build Status GoDoc Coverage Status Go Report Card

Simple, efficient token bucket rate limiter for your proxies.

Supports filters and whitelist exceptions to determine when to apply the rate limiter.

Installation

go get -u gopkg.in/vinxi/ratelimit.v0

API

See godoc reference.

Example

Rate limit based on time window
package main

import (
  "fmt"
  "time"
  "gopkg.in/vinxi/ratelimit.v0"
  "gopkg.in/vinxi/vinxi.v0"
)

const port = 3100

func main() {
  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: port})
  
  // Attach the rate limit middleware for 100 req/min
  vs.Use(ratelimit.NewTimeLimiter(time.Minute, 100))
  
  // Target server to forward
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", port)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}
Limit requests per second
package main

import (
  "fmt"
  "gopkg.in/vinxi/ratelimit.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "time"
)

const port = 3100

func main() {
  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: port})

  // Attach the ratelimit middleware for 10 req/second
  vs.Use(ratelimit.NewRateLimiter(10, 10))

  // Target server to forward
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", port)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

License

MIT

Documentation

Overview

Package ratelimit provides an efficient token bucket implementation that can be used to limit the rate concurrent HTTP traffic. See http://en.wikipedia.org/wiki/Token_bucket.

Index

Constants

View Source
const Version = "0.1.0"

Version defines the current package semantic version.

Variables

View Source
var RateLimitResponder = func(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(429)
	w.Write([]byte("Too Many Requests"))
}

RateLimitResponder is used as default function to repond when the rate limit is reached. You can customize it via Limiter.SetResponder(fn).

Functions

This section is empty.

Types

type Exception

type Exception Filter

Exception represents the Limiter exception function signature.

type Filter

type Filter func(r *http.Request) bool

Filter represents the Limiter filter function signature.

type Limiter

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

Limiter implements a token bucket rate limiter middleware. Rate limiter can support multiple rate limit strategies, such as time based limiter.

func NewRateLimiter

func NewRateLimiter(rate float64, capacity int64) *Limiter

NewRateLimiter creates a rate limiter middleware which limits the amount of requests accepted per second.

func NewTimeLimiter

func NewTimeLimiter(timeWindow time.Duration, capacity int64) *Limiter

NewTimeLimiter creates a new time based rate limiter middleware.

func (*Limiter) Exception

func (l *Limiter) Exception(fn ...Exception)

Exception registers whitelist exception. If the exception function matches, the traffic won't be limited.

func (*Limiter) Filter

func (l *Limiter) Filter(fn ...Filter)

Filter registers a new rate limiter whitelist filter. If the filter matches, the traffic won't be limited.

func (*Limiter) LimitHTTP

func (l *Limiter) LimitHTTP(h http.Handler) func(w http.ResponseWriter, r *http.Request)

LimitHTTP limits an incoming HTTP request. If some filter passes, the request won't be limited. This method is used internally, but made public for public testing.

func (*Limiter) Register

func (l *Limiter) Register(mw layer.Middleware)

Register registers the middleware handler.

func (*Limiter) SetResponder

func (l *Limiter) SetResponder(fn http.HandlerFunc)

SetResponder sets a custom function to reply in case of rate limit reached.

Directories

Path Synopsis
_examples
rate command
timewindow command

Jump to

Keyboard shortcuts

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