}
func (cfg *ClientConfig) setRateLimiterBursts() {
- // Create a helper for rate limiters to avoid mistakes? What if the limit is greater than what
- // can be represented by int?
- if cfg.UploadRateLimiter.Limit() != rate.Inf && cfg.UploadRateLimiter.Burst() == 0 {
- // What about chunk size?
- cfg.UploadRateLimiter.SetBurst(cfg.MaxAllocPeerRequestDataPerConn)
- }
- if cfg.DownloadRateLimiter.Limit() != rate.Inf && cfg.DownloadRateLimiter.Burst() == 0 {
- // 64 KiB used to be a rough default buffer for sockets on Windows. I'm sure it's bigger
- // these days. What about the read buffer size mentioned elsewhere?
- cfg.DownloadRateLimiter.SetBurst(min(int(cfg.DownloadRateLimiter.Limit()), 1<<16))
- }
+ // What about chunk size?
+ setRateLimiterBurstIfZero(cfg.UploadRateLimiter, cfg.MaxAllocPeerRequestDataPerConn)
+ setRateLimiterBurstIfZero(
+ cfg.DownloadRateLimiter,
+ min(
+ int(cfg.DownloadRateLimiter.Limit()),
+ defaultDownloadRateLimiterBurst))
}
--- /dev/null
+package torrent
+
+import (
+ "golang.org/x/time/rate"
+)
+
+// 64 KiB used to be a rough default buffer for sockets on Windows. I'm sure it's bigger
+// these days. What about the read buffer size mentioned elsewhere? Note this is also used for
+// webseeding since that shares the download rate limiter by default.
+const defaultDownloadRateLimiterBurst = 1 << 16
+
+// Sets rate limiter burst if it's set to zero which is used to request the default by our API.
+func setRateLimiterBurstIfZero(l *rate.Limiter, def int) {
+ if l.Burst() == 0 && l.Limit() != rate.Inf {
+ // What if the limit is greater than what can be represented by int?
+ l.SetBurst(def)
+ }
+}
for _, opt := range opts {
opt(&ws.client)
}
+ setRateLimiterBurstIfZero(ws.client.ResponseBodyRateLimiter, defaultDownloadRateLimiterBurst)
ws.client.ResponseBodyWrapper = func(r io.Reader) io.Reader {
return &rateLimitedReader{
l: ws.client.ResponseBodyRateLimiter,