throttled

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2019 License: MIT Imports: 3 Imported by: 0

README

HTTP Throttled Transport

An HTTP transport that throttles requests using a RateLimiter. RoundTripper middleware so that you can use it with third party clients

Usage

client := &http.Client{
    Transport: throttled.NewTransport(http.DefaultTransport, rate.NewLimiter(rate.Limit(10), 1)),
}

// requests are now rate limited to 10/second
res, err := client.Get("/bla")

Using a rate limiter with a bucket

client := throttled.Client(rate.NewLimiter(rate.Limit(4), 40))

// requests are now rate limited to 4/second with a bucket of 40
res, err := client.Get("/bla")

You can wrap an existing client

client = throttled.WrapClient(client, rate.NewLimiter(rate.Limit(4), 40))

// requests of an existing client are now rate limited to 4/second with a bucket of 40
res, err := client.Get("/bla")

Development

You can test using the make method which will call go locally or use docker if not installed

make test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client(limiter *rate.Limiter) *http.Client

Client returns a default http client with rate limiting

func WrapClient

func WrapClient(client *http.Client, limiter *rate.Limiter) *http.Client

WrapClient wraps an existing clients transport with the rate limiting transport

Types

type Transport

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

Transport will limit the requests to a base round tripper

func NewTransport

func NewTransport(base http.RoundTripper, limiter *rate.Limiter) *Transport

NewTransport thottled http transport

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implementation with rate limiting

Jump to

Keyboard shortcuts

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