-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
httputil: create package and RateLimiter
This change adds a rate limiter for HTTP requests. It's purposefully unconfigurable for ease of use. Signed-off-by: Hank Donnay <hdonnay@redhat.com>
- Loading branch information
Showing
4 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package httputil | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
|
||
"golang.org/x/time/rate" | ||
) | ||
|
||
// RateLimiter wraps the provided RoundTripper with a limiter allowing 10 | ||
// requests/second/host. | ||
// | ||
// It responds to HTTP 429 responses by automatically decreasing the rate. | ||
func RateLimiter(next http.RoundTripper) http.RoundTripper { | ||
return &ratelimiter{ | ||
rt: next, | ||
lm: sync.Map{}, | ||
} | ||
} | ||
|
||
// Ratelimiter implements the limiting by using a concurrent map and Limiter | ||
// structs. | ||
type ratelimiter struct { | ||
rt http.RoundTripper | ||
lm sync.Map | ||
} | ||
|
||
const rateCap = 10 | ||
|
||
// RoundTrip implements http.RoundTripper. | ||
func (r *ratelimiter) RoundTrip(req *http.Request) (*http.Response, error) { | ||
key := req.URL.Host | ||
li, ok := r.lm.Load(key) | ||
if !ok { | ||
// Limiter allows "rateCap" per sec, one at a time. | ||
l := rate.NewLimiter(rate.Limit(rateCap), 1) | ||
li, _ = r.lm.LoadOrStore(key, l) | ||
} | ||
l := li.(*rate.Limiter) | ||
if err := l.Wait(req.Context()); err != nil { | ||
return nil, err | ||
} | ||
res, err := r.rt.RoundTrip(req) | ||
// This seems to be the contract that http.Transport implements. | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch res.StatusCode { | ||
case http.StatusOK: | ||
// Try increasing on OK. | ||
if lim := l.Limit(); lim < rateCap { | ||
l.SetLimit(lim + 1) | ||
} | ||
case http.StatusTooManyRequests: | ||
// Try to allow some requests, eventually. | ||
l.SetLimit(detune(l.Limit())) | ||
} | ||
return res, nil | ||
} | ||
|
||
// Detune reduces the rate. | ||
func detune(in rate.Limit) rate.Limit { | ||
if in <= 1 { | ||
return in / 2 | ||
} | ||
return in - 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package httputil | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRate(t *testing.T) { | ||
const nReq = 20 | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(nReq) | ||
begin := make(chan struct{}) | ||
var last struct { | ||
sync.Mutex | ||
t time.Time | ||
} | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
last.Lock() | ||
last.t = time.Now() | ||
last.Unlock() | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
defer srv.Close() | ||
cl := srv.Client() | ||
cl.Transport = RateLimiter(cl.Transport) | ||
|
||
for i := 0; i < nReq; i++ { | ||
go func() { | ||
defer wg.Done() | ||
<-begin | ||
res, err := cl.Get(srv.URL) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
res.Body.Close() | ||
}() | ||
} | ||
|
||
first := time.Now() | ||
close(begin) | ||
wg.Wait() | ||
|
||
t.Logf("begin: %v", first) | ||
t.Logf("end: %v", last.t) | ||
rate := nReq / last.t.Sub(first).Seconds() | ||
t.Logf("rate: %v", rate) | ||
|
||
if rate < (rateCap-1) || rate > (rateCap+1) { | ||
t.Error("rate outside acceptable bounds") | ||
} | ||
} |