golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.98k stars 17.53k forks source link

x/time/rate: token bucket is not initially full #23482

Open liggitt opened 6 years ago

liggitt commented 6 years ago

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.9.2

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/jliggitt/.gvm/pkgsets/go1.9.2/global"
GORACE=""
GOROOT="/Users/jliggitt/.gvm/gos/go1.9.2"
GOTOOLDIR="/Users/jliggitt/.gvm/gos/go1.9.2/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/lw/0m4r59zx3_b56vbmr2j0zkbr0000gn/T/go-build585916590=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

Created a new limiter, and attempted to take a token at t=0:

l := rate.NewLimiter(1, 1)
l.AllowN(time.Time{}, 1)

What did you expect to see?

AllowN(...any time..., 1) should succeed if the token bucket is initially full as documented.

What did you see instead?

AllowN returns false

It appears that the token bucket is not initially full.

The limiter depends on the elapsed time from the zero value of last to the value of now the first time AllowN is called being long enough to fill the bucket.

liggitt commented 6 years ago

proposed fix in https://go-review.googlesource.com/c/time/+/88435

bradfitz commented 6 years ago

/cc @Sajmani @ianlancetaylor @rsc

gopherbot commented 6 years ago

Change https://golang.org/cl/88435 mentions this issue: rate: ensure token bucket is initially full