Do not follow this link

~mariusor/ratelimit

A package to rate limit HTTP responses, compatible with the Go standard library http.RoundTripper API.
Stop cloning the request, as theoretically we don't read the body
Store the parent round tripper into its individual property
Abstract the time and duration extraction from header values

refs

master
browse  log 

clone

read-only
https://git.sr.ht/~mariusor/ratelimit
read/write
git@git.sr.ht:~mariusor/ratelimit

You can also use your local clone with git send-email.

#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))
}
Do not follow this link