forked from tjhowse/cacheodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRLHTTPClient.go
More file actions
32 lines (27 loc) · 743 Bytes
/
RLHTTPClient.go
File metadata and controls
32 lines (27 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import (
"context"
"net/http"
"golang.org/x/time/rate"
)
// Credit to Melchi Salins for the original code
//https://medium.com/mflow/rate-limiting-in-golang-http-client-a22fba15861a
// RLHTTPClient Rate Limited HTTP Client
type RLHTTPClient struct {
client *http.Client
Ratelimiter *rate.Limiter
}
// Do dispatches the HTTP request to the network
func (c *RLHTTPClient) Do(req *http.Request) (*http.Response, error) {
// Comment out the below 5 lines to turn off ratelimiting
ctx := context.Background()
err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}