rate: simplify code

When a long time has passed since a previous call to the rate limiter,
we can overflow when calculating how many new tokens are available.

To prevent that, we calculated a maximum elapsed amount.
When we hit maxElapsed, we ended up converting a duration into tokens
and then back. This operation was lossy, so we took pains to increase
the precision of the conversion in tokensFromDuration.

An easier fix is to avoid the round trip entirely.
We know the max number of new tokens we should ever have,
so we can cap it directly. And in fact we do cap it already.
Delete the extraneous code.

This lets us revert to the original, simpler tokensFromDuration.

While we're here, tweak durationFromTokens for clarity.

Updates #34861

Change-Id: I9a3b0fd848d772b314aecc43fa675631b8be7f2f
Reviewed-on: https://go-review.googlesource.com/c/time/+/336469
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Eric Lagergren <ericscottlagergren@gmail.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
1 file changed
tree: 29a20f66ce49ba5be461d41be241d8d1ee561e02
  6. README.md
  7. go.mod
  8. rate/

Go Time

Go Reference

This repository provides supplementary Go time packages.


The easiest way to install is to run go get -u golang.org/x/time. You can also manually git clone the repository to $GOPATH/src/golang.org/x/time.

Report Issues / Send Patches

This repository uses Gerrit for code changes. To learn how to submit changes to this repository, see https://golang.org/doc/contribute.html.

The main issue tracker for the time repository is located at https://github.com/golang/go/issues. Prefix your issue with “x/time:” in the subject line, so it is easy to find.