ratelimit

package module
v0.0.0-...-05b6d70 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2017 License: MIT Imports: 7 Imported by: 0

README

ratelimit

Build Status Coverage Status

HTTP ratelimit middleware for Go.

Installation

go get -u github.com/go-http-utils/ratelimit

Documentation

API documentation can be found here: https://godoc.org/github.com/go-http-utils/ratelimit

Usage

import (
  "github.com/go-http-utils/ratelimit"
)
getIDByReq := func(req *http.Request) string {
  return req.RemoteAddr
}

m := http.NewServeMux()

m.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
  res.WriteHeader(http.StatusOK)

  res.Write([]byte("Hello Worlkd"))
})

http.ListenAndServe(":8080", ratelimit.Handler(m, ratelimit.Options{
  GetID:    getIDByReq,
  Duration: 1 * time.Second,
  Count:    1000,
}))

Documentation

Overview

Example
package main

import (
	"net/http"
	"time"

	"github.com/go-http-utils/ratelimit"
)

func main() {
	getIDByReq := func(req *http.Request) string {
		return req.RemoteAddr
	}

	m := http.NewServeMux()

	m.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		res.WriteHeader(http.StatusOK)

		res.Write([]byte("Hello Worlkd"))
	})

	http.ListenAndServe(":8080", ratelimit.Handler(m, ratelimit.Options{
		GetID:    getIDByReq,
		Duration: 1 * time.Second,
		Count:    1000,
	}))
}

Index

Examples

Constants

View Source
const Version = "0.3.0"

Version is this package's version number.

Variables

This section is empty.

Functions

func Handler

func Handler(h http.Handler, opts Options) http.Handler

Handler wraps the http.Handler with reatelimit support (only count requests can pass through in duration).

Types

type GetIDFunc

type GetIDFunc func(*http.Request) string

GetIDFunc represents a function that return an ID for each request. All requests which have the same ID will be regarded from one source and be ratelimited.

type Options

type Options struct {
	// GetIDFunc represents a function that return an ID for each request.
	// All requests which have the same ID will be regarded from one source and
	// be ratelimited.
	GetID GetIDFunc
	// Ratelimit factor: only Count requests can pass through in Duration.
	// By default is 1 minute.
	Duration time.Duration
	// Ratelimit factor: only Count requests can pass through in Duration.
	// By default is 1000.
	Count int64
}

Options is the ratelimit middleware options.

Jump to

Keyboard shortcuts

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