github.com/lrita/ratelimit@v0.0.0-20190723030019-81504bd89bc5/ratelimit.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the LGPLv3 with static-linking exception.
     3  // See LICENCE file for details.
     4  
     5  // Package ratelimit provides an efficient token bucket implementation
     6  // that can be used to limit the rate of arbitrary things.
     7  // See http://en.wikipedia.org/wiki/Token_bucket.
     8  package ratelimit
     9  
    10  import (
    11  	"math"
    12  	"sync"
    13  	"time"
    14  	_ "unsafe" // for go:linkname
    15  )
    16  
    17  // The algorithm that this implementation uses does computational work
    18  // only when tokens are removed from the bucket, and that work completes
    19  // in short, bounded-constant time (Bucket.Wait benchmarks at 175ns on
    20  // my laptop).
    21  //
    22  // Time is measured in equal measured ticks, a given interval
    23  // (fillInterval) apart. On each tick a number of tokens (quantum) are
    24  // added to the bucket.
    25  //
    26  // When any of the methods are called the bucket updates the number of
    27  // tokens that are in the bucket, and it records the current tick
    28  // number too. Note that it doesn't record the current time - by
    29  // keeping things in units of whole ticks, it's easy to dish out tokens
    30  // at exactly the right intervals as measured from the start time.
    31  //
    32  // This allows us to calculate the number of tokens that will be
    33  // available at some time in the future with a few simple arithmetic
    34  // operations.
    35  //
    36  // The main reason for being able to transfer multiple tokens on each tick
    37  // is so that we can represent rates greater than 1e9 (the resolution of the Go
    38  // time package) tokens per second, but it's also useful because
    39  // it means we can easily represent situations like "a person gets
    40  // five tokens an hour, replenished on the hour".
    41  
    42  // Bucket represents a token bucket that fills at a predetermined rate.
    43  // Methods on Bucket may be called concurrently.
    44  type Bucket struct {
    45  	// startTime holds the moment when the bucket was
    46  	// first created and ticks began.
    47  	startTime int64
    48  
    49  	// capacity holds the overall capacity of the bucket.
    50  	// This represents the max rate spike/burst.
    51  	capacity int64
    52  
    53  	// quantum holds how many tokens are added on
    54  	// each tick.
    55  	quantum int64
    56  
    57  	// fillInterval holds the interval between each tick.
    58  	fillInterval time.Duration
    59  
    60  	// mu guards the fields below it.
    61  	mu sync.Mutex
    62  
    63  	// availableTokens holds the number of available
    64  	// tokens as of the associated latestTick.
    65  	// It will be negative when there are consumers
    66  	// waiting for tokens.
    67  	availableTokens int64
    68  
    69  	// latestTick holds the latest tick for which
    70  	// we know the number of tokens in the bucket.
    71  	latestTick int64
    72  }
    73  
    74  // NewBucket returns a new token bucket that fills at the
    75  // rate of one token every fillInterval, up to the given
    76  // maximum capacity. Both arguments must be
    77  // positive. The bucket is initially full.
    78  func NewBucket(fillInterval time.Duration, capacity int64) *Bucket {
    79  	return NewBucketWithQuantum(fillInterval, capacity, 1)
    80  }
    81  
    82  // rateMargin specifes the allowed variance of actual
    83  // rate from specified rate. 1% seems reasonable.
    84  const rateMargin = 0.01
    85  
    86  // NewBucketWithRate returns a token bucket that fills the bucket
    87  // at the rate of rate tokens per second up to the given
    88  // maximum capacity. Because of limited clock resolution,
    89  // at high rates, the actual rate may be up to 1% different from the
    90  // specified rate.
    91  func NewBucketWithRate(rate float64, capacity int64) *Bucket {
    92  	// Use the same bucket each time through the loop
    93  	// to save allocations.
    94  	tb := NewBucketWithQuantum(1, capacity, 1)
    95  	tb.fillInterval, tb.quantum = calcQuantum(rate)
    96  	if tb.capacity < tb.quantum {
    97  		tb.capacity = tb.quantum
    98  	}
    99  	return tb
   100  }
   101  
   102  func calcQuantum(rate float64) (fillInterval time.Duration, quantum int64) {
   103  	margin := 1.1
   104  	for {
   105  		for quantum = int64(1); quantum < 1<<50; quantum = nextQuantum(quantum, margin) {
   106  			fillInterval = time.Duration(1e9 * float64(quantum) / rate)
   107  			if fillInterval <= 0 {
   108  				continue
   109  			}
   110  			r0 := 1e9 * float64(quantum) / float64(fillInterval)
   111  			if diff := math.Abs(r0 - rate); diff/rate <= rateMargin {
   112  				return
   113  			}
   114  		}
   115  		margin = 1.0 + margin*0.9
   116  	}
   117  }
   118  
   119  // nextQuantum returns the next quantum to try after q.
   120  // We grow the quantum exponentially, but slowly, so we
   121  // get a good fit in the lower numbers.
   122  func nextQuantum(q int64, m float64) int64 {
   123  	q1 := int64(float64(q) * m)
   124  	if q1 <= q {
   125  		q1 = q + 1
   126  	}
   127  	return q1
   128  }
   129  
   130  // NewBucketWithQuantum is similar to NewBucket, but allows
   131  // the specification of the quantum size - quantum tokens
   132  // are added every fillInterval.
   133  func NewBucketWithQuantum(fillInterval time.Duration, capacity, quantum int64) *Bucket {
   134  	if fillInterval <= 0 {
   135  		panic("token bucket fill interval is not > 0")
   136  	}
   137  	if capacity <= 0 {
   138  		panic("token bucket capacity is not > 0")
   139  	}
   140  	if quantum <= 0 {
   141  		panic("token bucket quantum is not > 0")
   142  	}
   143  	if capacity < quantum {
   144  		panic("token capacity need large than quantum")
   145  	}
   146  	return &Bucket{
   147  		startTime:       nanotime(),
   148  		latestTick:      0,
   149  		fillInterval:    fillInterval,
   150  		capacity:        capacity,
   151  		quantum:         quantum,
   152  		availableTokens: capacity,
   153  	}
   154  }
   155  
   156  // ResetRate reset current rate limit and capacity.
   157  func (tb *Bucket) ResetRate(rate float64, capacity int64) {
   158  	tb.mu.Lock()
   159  	defer tb.mu.Unlock()
   160  	tb.capacity = capacity
   161  	tb.fillInterval, tb.quantum = calcQuantum(rate)
   162  	tb.latestTick = tb.currentTick(nanotime())
   163  	if tb.capacity < tb.quantum {
   164  		tb.capacity = tb.quantum
   165  	}
   166  }
   167  
   168  // Wait takes count tokens from the bucket, waiting until they are
   169  // available.
   170  func (tb *Bucket) Wait(count int64) {
   171  	if d := tb.Take(count); d > 0 {
   172  		time.Sleep(d)
   173  	}
   174  }
   175  
   176  // WaitMaxDuration is like Wait except that it will
   177  // only take tokens from the bucket if it needs to wait
   178  // for no greater than maxWait. It reports whether
   179  // any tokens have been removed from the bucket
   180  // If no tokens have been removed, it returns immediately.
   181  func (tb *Bucket) WaitMaxDuration(count int64, maxWait time.Duration) bool {
   182  	d, ok := tb.TakeMaxDuration(count, maxWait)
   183  	if d > 0 {
   184  		time.Sleep(d)
   185  	}
   186  	return ok
   187  }
   188  
   189  const infinityDuration time.Duration = 0x7fffffffffffffff
   190  
   191  // Take takes count tokens from the bucket without blocking. It returns
   192  // the time that the caller should wait until the tokens are actually
   193  // available.
   194  //
   195  // Note that if the request is irrevocable - there is no way to return
   196  // tokens to the bucket once this method commits us to taking them.
   197  func (tb *Bucket) Take(count int64) time.Duration {
   198  	tb.mu.Lock()
   199  	d, _ := tb.take(nanotime(), count, infinityDuration)
   200  	tb.mu.Unlock()
   201  	return d
   202  }
   203  
   204  // TakeMaxDuration is like Take, except that
   205  // it will only take tokens from the bucket if the wait
   206  // time for the tokens is no greater than maxWait.
   207  //
   208  // If it would take longer than maxWait for the tokens
   209  // to become available, it does nothing and reports false,
   210  // otherwise it returns the time that the caller should
   211  // wait until the tokens are actually available, and reports
   212  // true.
   213  func (tb *Bucket) TakeMaxDuration(count int64, maxWait time.Duration) (time.Duration, bool) {
   214  	tb.mu.Lock()
   215  	d, ok := tb.take(nanotime(), count, maxWait)
   216  	tb.mu.Unlock()
   217  	return d, ok
   218  }
   219  
   220  // TakeAvailable takes up to count immediately available tokens from the
   221  // bucket. It returns the number of tokens removed, or zero if there are
   222  // no available tokens. It does not block.
   223  func (tb *Bucket) TakeAvailable(count int64) int64 {
   224  	tb.mu.Lock()
   225  	n := tb.takeAvailable(nanotime(), count)
   226  	tb.mu.Unlock()
   227  	return n
   228  }
   229  
   230  // takeAvailable is the internal version of TakeAvailable - it takes the
   231  // current time as an argument to enable easy testing.
   232  func (tb *Bucket) takeAvailable(now, count int64) int64 {
   233  	if count <= 0 {
   234  		return 0
   235  	}
   236  	tb.adjustavailableTokens(tb.currentTick(now))
   237  	if tb.availableTokens <= 0 {
   238  		return 0
   239  	}
   240  	if count > tb.availableTokens {
   241  		count = tb.availableTokens
   242  	}
   243  	tb.availableTokens -= count
   244  	return count
   245  }
   246  
   247  // Available returns the number of available tokens. It will be negative
   248  // when there are consumers waiting for tokens. Note that if this
   249  // returns greater than zero, it does not guarantee that calls that take
   250  // tokens from the buffer will succeed, as the number of available
   251  // tokens could have changed in the meantime. This method is intended
   252  // primarily for metrics reporting and debugging.
   253  func (tb *Bucket) Available() int64 {
   254  	return tb.available(nanotime())
   255  }
   256  
   257  // available is the internal version of available - it takes the current time as
   258  // an argument to enable easy testing.
   259  func (tb *Bucket) available(now int64) int64 {
   260  	tb.mu.Lock()
   261  	tb.adjustavailableTokens(tb.currentTick(now))
   262  	avail := tb.availableTokens
   263  	tb.mu.Unlock()
   264  	return avail
   265  }
   266  
   267  // Capacity returns the capacity that the bucket was created with.
   268  func (tb *Bucket) Capacity() int64 {
   269  	return tb.capacity
   270  }
   271  
   272  // Rate returns the fill rate of the bucket, in tokens per second.
   273  func (tb *Bucket) Rate() float64 {
   274  	return 1e9 * float64(tb.quantum) / float64(tb.fillInterval)
   275  }
   276  
   277  // take is the internal version of Take - it takes the current time as
   278  // an argument to enable easy testing.
   279  func (tb *Bucket) take(now, count int64, maxWait time.Duration) (time.Duration, bool) {
   280  	if count <= 0 {
   281  		return 0, true
   282  	}
   283  
   284  	tick := tb.currentTick(now)
   285  	tb.adjustavailableTokens(tick)
   286  	avail := tb.availableTokens - count
   287  	if avail >= 0 {
   288  		tb.availableTokens = avail
   289  		return 0, true
   290  	}
   291  	// Round up the missing tokens to the nearest multiple
   292  	// of quantum - the tokens won't be available until
   293  	// that tick.
   294  
   295  	// endTick holds the tick when all the requested tokens will
   296  	// become available.
   297  	endTick := tick + (-avail+tb.quantum-1)/tb.quantum
   298  	endTime := tb.startTime + (endTick * int64(tb.fillInterval))
   299  	waitTime := time.Duration(endTime - now)
   300  	if waitTime > maxWait {
   301  		return 0, false
   302  	}
   303  	tb.availableTokens = avail
   304  	return waitTime, true
   305  }
   306  
   307  // currentTick returns the current time tick, measured
   308  // from tb.startTime.
   309  func (tb *Bucket) currentTick(now int64) int64 {
   310  	return (now - tb.startTime) / int64(tb.fillInterval)
   311  }
   312  
   313  // adjustavailableTokens adjusts the current number of tokens
   314  // available in the bucket at the given time, which must
   315  // be in the future (positive) with respect to tb.latestTick.
   316  func (tb *Bucket) adjustavailableTokens(tick int64) {
   317  	if tb.availableTokens >= tb.capacity {
   318  		return
   319  	}
   320  	tb.availableTokens += (tick - tb.latestTick) * tb.quantum
   321  	if tb.availableTokens > tb.capacity {
   322  		tb.availableTokens = tb.capacity
   323  	}
   324  	tb.latestTick = tick
   325  	return
   326  }
   327  
   328  //go:linkname nanotime runtime.nanotime
   329  func nanotime() int64