ratelimit

package module
v0.0.0-...-4218fd5 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: MIT Imports: 3 Imported by: 1

README

Go HTTP rate limited Transport

This package implements http transport functionality compatible with that of the Go standard library http.RoundTripper interface but which adds logic based on the 429 Too Many Requests response status and Retry-After header.

The simplest way to use is by overriding the http.DefaultTransport.

package main

import (
	"fmt"
	"net/http"
	"time"

	"git.sr.ht/~mariusor/ratelimit"
)

const uri = "https://example.com"

func main() {
	// Use std library http.DefaultTransport, but with a retry mechanism of 10 retries and a maximum wait time of 10s
	http.DefaultTransport = ratelimit.New(5, 10*time.Second)

	r, err := http.Get(uri)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Get %s: %s\n", uri, r.Status)
	fmt.Printf("Retry after: %ss\n", r.Header.Get("Retry-After"))
	fmt.Printf("Retries: %s\n", r.Header.Get(ratelimit.HeaderRetries))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HeaderRetries = "X-Retries"

	DefaultRetryAfter = 60 * time.Second

	MaxRetries  = 4
	MaxWaitTime = 10 * time.Second
)

Functions

This section is empty.

Types

type Transport

type Transport struct {
	Base     http.RoundTripper
	Retries  int
	WaitTime time.Duration

	RetryWaitFn WaitTimeFn
}

func New

func New(r int, wt time.Duration) Transport

New creates a new rate limited Transport that retries for "r" retries waiting a maximum of "wt" duration, and falls back to the default http.DefaultTransport as a round tripper.

func Wrap

func Wrap(t http.RoundTripper, r int, wt time.Duration) Transport

Wrap wraps an existing http.RoundTripper instance with a rate limited Transport that retries for "r" retries waiting for a maximum of "wt" duration.

func (Transport) RoundTrip

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

type WaitTimeFn

type WaitTimeFn func(*http.Response) time.Duration

Jump to

Keyboard shortcuts

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