Documentation
¶
Overview ¶
Package httpthrottle provides a http.RoundTripper to throttle http requests.
Example ¶
package main
import (
"net/http"
"time"
"github.com/jybp/httpthrottle"
"golang.org/x/time/rate"
)
func main() {
client := &http.Client{
Transport: httpthrottle.Default(
// Returns ErrQuotaExceeded if more than 36000 requests occured within an hour.
httpthrottle.NewQuota(time.Hour, 36000),
// Blocks to never exceed 99 requests per second.
rate.NewLimiter(99, 1),
),
}
resp, err := client.Get("https://golang.org/")
if err == httpthrottle.ErrQuotaExceeded {
// Handle err.
}
if err != nil {
// Handle err.
}
_ = resp // Do something with resp.
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrQuotaExceeded = errors.New("quota exceeded")
Functions ¶
This section is empty.
Types ¶
type MultiLimiter ¶
type MultiLimiter struct {
// contains filtered or unexported fields
}
MultiLimiter allows to enforce multiple RateLimiter.
func MultiLimiters ¶
func MultiLimiters(limiters ...RateLimiter) *MultiLimiter
MultiLimiters creates a MultiLimiter from limiters.
type Quota ¶
A Quota controls how much events can happen within a timeframe. It is useful to enforce long-term rate limits where failing is more appropriate than blocking. The timeframe starts when Wait is called for the first time.
type RateLimiter ¶
RateLimiter interface compatible with golang.org/x/time/rate.
type Transport ¶
type Transport struct {
Transport http.RoundTripper // Used to make actual requests.
Limiter RateLimiter
}
Transport implements http.RoundTripper.
func Custom ¶
func Custom(t http.RoundTripper, r ...RateLimiter) *Transport
Custom uses t to make actual requests.
func Default ¶
func Default(r ...RateLimiter) *Transport
Default returns a RoundTripper capable of rate limiting http requests.
Click to show internal directories.
Click to hide internal directories.