github.com/anacrolix/torrent@v1.61.0/rate.go (about)

     1  package torrent
     2  
     3  import (
     4  	"math"
     5  
     6  	"golang.org/x/time/rate"
     7  )
     8  
     9  // 64 KiB used to be a rough default buffer for sockets on Windows. I'm sure it's bigger these days.
    10  // What about the read buffer size mentioned elsewhere? Note this is also used for webseeding since
    11  // that shares the download rate limiter by default. 1 MiB is the default max read frame size for
    12  // HTTP/2,
    13  const defaultMinDownloadRateLimiterBurst = 1 << 20
    14  
    15  // Sets rate limiter burst if it's set to zero which is used to request the default by our API.
    16  func setRateLimiterBurstIfZero(l *rate.Limiter, def int) {
    17  	// Set it to something reasonable if the limit is Inf, in case the limit is dynamically adjusted
    18  	// and the user doesn't know what value to use. Assume the original limit is in a reasonable
    19  	// ballpark.
    20  	if l != nil && l.Burst() == 0 {
    21  		// What if the limit is greater than what can be represented by int?
    22  		l.SetBurst(def)
    23  	}
    24  }
    25  
    26  // Sets rate limiter burst if it's set to zero which is used to request the default by our API.
    27  func setDefaultDownloadRateLimiterBurstIfZero(l *rate.Limiter) {
    28  	setRateLimiterBurstIfZero(l, int(
    29  		max(
    30  			min(EffectiveDownloadRateLimit(l), math.MaxInt),
    31  			defaultMinDownloadRateLimiterBurst)))
    32  }