httpcap

package
v0.0.0-...-dd64b79 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2016 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package httpcap provides rate limiting for HTTP handlers and response writers.

Rate limiting can be applied to an http.Handler either on a per-request basis, or using a rate limiting group.

h = httpcap.Handler(h, rate)
...
g := iocap.NewGroup(rate)
h = httpcap.GroupHandler(h, g)

See the LimitByRequestIP method for a short-hand quick start.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GroupHandler

func GroupHandler(h http.Handler, g *iocap.Group) http.Handler

GroupHandler is like Handler, but wraps an http.Handler with group rate limiting such that all requests share the same quota.

Example
// Create a normal HTTP handler to serve data.
h := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("hello world!"))
}))

// Wrap the handler with a rate limit group. All requests to this handler
// will share the rate below.
rate := iocap.Kbps(512)
group := iocap.NewGroup(rate)
h = GroupHandler(h, group)

// Start a test server using the rate limited handler.
ts := httptest.NewServer(h)
defer ts.Close()

// Make a request to the server.
resp, err := http.Get(ts.URL)
if err != nil {
	fmt.Println(err)
	return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(string(body))
Output:

hello world!

func Handler

func Handler(h http.Handler, ro iocap.RateOpts) http.Handler

Handler creates a new rate limited HTTP handler wrapper. The rate described by ro is used to rate limit each request independently.

Example
// Create a normal HTTP handler to serve data.
h := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("hello world!"))
}))

// Wrap the handler with a rate limit.
rate := iocap.Kbps(512)
h = Handler(h, rate)

// Start a test server using the rate limited handler.
ts := httptest.NewServer(h)
defer ts.Close()

// Make a request to the server.
resp, err := http.Get(ts.URL)
if err != nil {
	fmt.Println(err)
	return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(string(body))
Output:

hello world!

func LimitByRequestIP

func LimitByRequestIP(h http.Handler, opts iocap.RateOpts) http.Handler

LimitByRequestIP is a convenience wrapper to automatically limit inbound requests by the given rate, per client IP address. Just give it any old HTTP handler and a rate.

Example
// Create a normal HTTP handler to serve data.
h := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("hello world!"))
}))

// Rate limit requests to the handler per client IP.
h = LimitByRequestIP(h, iocap.Kbps(512))

// Start the server.
ts := httptest.NewServer(h)
defer ts.Close()

// Make a request to the server.
resp, err := http.Get(ts.URL)
if err != nil {
	fmt.Println(err)
	return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(string(body))
Output:

hello world!

Types

This section is empty.

Directories

Path Synopsis
Package mapper provides HTTP request mapping based on arbitrary criteria, including the raw HTTP request details.
Package mapper provides HTTP request mapping based on arbitrary criteria, including the raw HTTP request details.

Jump to

Keyboard shortcuts

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