hyperate

package module
v0.0.0-...-b69c42b Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: MIT Imports: 4 Imported by: 0

README

hyperate ⏱

hyperate provides a wrapper around a http.RoundTripper to help rate limit outgoing HTTP requests.

Installation

go get github.com/dstotijn/hyperate

Usage

Simple
// Create an http.RoundTripper that rate limits outgoing HTTP requests to 10
// per second, allowing bursts of 5 requests.
rt := hyperate.New(http.DefaultTransport, rate.NewLimiter(10, 5))
client := &http.Client{Transport: rt}
Based on HTTP response headers

Use HTTP response headers (RateLimit-Remaining and RateLimit-Reset) (draft-polli-ratelimit-headers-02) to continously set rate limit.

rt := hyperate.New(http.DefaultTransport, rate.NewLimiter(rate.Inf, 1),
    hyperate.WithRateLimitHeaderCheck(),
)
client := &http.Client{Transport: rt}

License

MIT


© 2023 David Stotijn

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OnRespFunc

type OnRespFunc func(*http.Response, error) (*http.Response, error)

type RateLimitRoundTripper

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

RateLimitRoundTripper is an http.RoundTripper that rate limits requests.

func New

New returns a a new RateLimitRoundtripper.

func (*RateLimitRoundTripper) RoundTrip

func (rt *RateLimitRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

It blocks, waiting until the rate limiter permits the request to be made, then delegates to the underlying RoundTripper to make the request. If the request's context is canceled or its deadline is exceeded while waiting, an error is returned.

type RateLimitRoundTripperOption

type RateLimitRoundTripperOption func(*RateLimitRoundTripper)

func WithRateLimitHeaderCheck

func WithRateLimitHeaderCheck() RateLimitRoundTripperOption

WithRateLimitHeaderCheck returns a RateLimitRoundTripperOption that checks the response headers for rate limit information.

See: https://www.ietf.org/archive/id/draft-polli-ratelimit-headers-02.html

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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