2017-10-08 18:28:03 +00:00
|
|
|
package limiter
|
|
|
|
|
|
|
|
import (
|
2023-10-01 20:45:18 +00:00
|
|
|
"context"
|
2017-10-08 18:28:03 +00:00
|
|
|
"io"
|
2017-12-29 11:43:49 +00:00
|
|
|
"net/http"
|
2017-10-08 18:28:03 +00:00
|
|
|
|
2023-10-01 20:45:18 +00:00
|
|
|
"golang.org/x/time/rate"
|
2017-10-08 18:28:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type staticLimiter struct {
|
2023-10-01 20:45:18 +00:00
|
|
|
upstream *rate.Limiter
|
|
|
|
downstream *rate.Limiter
|
2017-10-08 18:28:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 16:29:58 +00:00
|
|
|
// Limits represents static upload and download limits.
|
|
|
|
// For both, zero means unlimited.
|
|
|
|
type Limits struct {
|
|
|
|
UploadKb int
|
|
|
|
DownloadKb int
|
|
|
|
}
|
|
|
|
|
2017-10-08 18:28:03 +00:00
|
|
|
// NewStaticLimiter constructs a Limiter with a fixed (static) upload and
|
|
|
|
// download rate cap
|
2022-06-22 16:29:58 +00:00
|
|
|
func NewStaticLimiter(l Limits) Limiter {
|
2017-10-08 18:28:03 +00:00
|
|
|
var (
|
2023-10-01 20:45:18 +00:00
|
|
|
upstreamBucket *rate.Limiter
|
|
|
|
downstreamBucket *rate.Limiter
|
2017-10-08 18:28:03 +00:00
|
|
|
)
|
|
|
|
|
2022-06-22 16:29:58 +00:00
|
|
|
if l.UploadKb > 0 {
|
2023-10-01 20:45:18 +00:00
|
|
|
upstreamBucket = rate.NewLimiter(rate.Limit(toByteRate(l.UploadKb)), int(toByteRate(l.UploadKb)))
|
2017-10-08 18:28:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 16:29:58 +00:00
|
|
|
if l.DownloadKb > 0 {
|
2023-10-01 20:45:18 +00:00
|
|
|
downstreamBucket = rate.NewLimiter(rate.Limit(toByteRate(l.DownloadKb)), int(toByteRate(l.DownloadKb)))
|
2017-10-08 18:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return staticLimiter{
|
|
|
|
upstream: upstreamBucket,
|
|
|
|
downstream: downstreamBucket,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l staticLimiter) Upstream(r io.Reader) io.Reader {
|
2018-05-22 18:48:17 +00:00
|
|
|
return l.limitReader(r, l.upstream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l staticLimiter) UpstreamWriter(w io.Writer) io.Writer {
|
|
|
|
return l.limitWriter(w, l.upstream)
|
2017-10-08 18:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l staticLimiter) Downstream(r io.Reader) io.Reader {
|
2018-05-22 18:48:17 +00:00
|
|
|
return l.limitReader(r, l.downstream)
|
2017-10-08 18:28:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 11:46:20 +00:00
|
|
|
func (l staticLimiter) DownstreamWriter(w io.Writer) io.Writer {
|
|
|
|
return l.limitWriter(w, l.downstream)
|
|
|
|
}
|
|
|
|
|
2017-12-29 11:43:49 +00:00
|
|
|
type roundTripper func(*http.Request) (*http.Response, error)
|
|
|
|
|
|
|
|
func (rt roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
return rt(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l staticLimiter) roundTripper(rt http.RoundTripper, req *http.Request) (*http.Response, error) {
|
2021-04-24 09:46:06 +00:00
|
|
|
type readCloser struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2017-12-29 11:43:49 +00:00
|
|
|
if req.Body != nil {
|
2021-04-24 09:46:06 +00:00
|
|
|
req.Body = &readCloser{
|
|
|
|
Reader: l.Upstream(req.Body),
|
|
|
|
Closer: req.Body,
|
2017-12-29 11:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := rt.RoundTrip(req)
|
|
|
|
|
|
|
|
if res != nil && res.Body != nil {
|
2021-04-24 09:46:06 +00:00
|
|
|
res.Body = &readCloser{
|
|
|
|
Reader: l.Downstream(res.Body),
|
|
|
|
Closer: res.Body,
|
2017-12-29 11:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transport returns an HTTP transport limited with the limiter l.
|
|
|
|
func (l staticLimiter) Transport(rt http.RoundTripper) http.RoundTripper {
|
|
|
|
return roundTripper(func(req *http.Request) (*http.Response, error) {
|
|
|
|
return l.roundTripper(rt, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-01 20:45:18 +00:00
|
|
|
func (l staticLimiter) limitReader(r io.Reader, b *rate.Limiter) io.Reader {
|
2017-10-08 18:28:03 +00:00
|
|
|
if b == nil {
|
|
|
|
return r
|
|
|
|
}
|
2023-10-01 20:45:18 +00:00
|
|
|
return &rateLimitedReader{r, b}
|
2017-10-08 18:28:03 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 20:45:18 +00:00
|
|
|
type rateLimitedReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
bucket *rate.Limiter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rateLimitedReader) Read(p []byte) (int, error) {
|
|
|
|
n, err := r.reader.Read(p)
|
|
|
|
if err := consumeTokens(n, r.bucket); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l staticLimiter) limitWriter(w io.Writer, b *rate.Limiter) io.Writer {
|
2018-05-22 18:48:17 +00:00
|
|
|
if b == nil {
|
|
|
|
return w
|
|
|
|
}
|
2023-10-01 20:45:18 +00:00
|
|
|
return &rateLimitedWriter{w, b}
|
|
|
|
}
|
|
|
|
|
|
|
|
type rateLimitedWriter struct {
|
|
|
|
writer io.Writer
|
|
|
|
bucket *rate.Limiter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *rateLimitedWriter) Write(buf []byte) (int, error) {
|
|
|
|
if err := consumeTokens(len(buf), w.bucket); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return w.writer.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func consumeTokens(tokens int, bucket *rate.Limiter) error {
|
|
|
|
// bucket allows waiting for at most Burst() tokens at once
|
|
|
|
maxWait := bucket.Burst()
|
|
|
|
for tokens > maxWait {
|
|
|
|
if err := bucket.WaitN(context.Background(), maxWait); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tokens -= maxWait
|
|
|
|
}
|
|
|
|
return bucket.WaitN(context.Background(), tokens)
|
2018-05-22 18:48:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-08 18:28:03 +00:00
|
|
|
func toByteRate(val int) float64 {
|
|
|
|
return float64(val) * 1024.
|
|
|
|
}
|