Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/time: when rate is 0.7692307692307693 (10/13), Allow() returns false #34861

Closed
on99 opened this issue Oct 12, 2019 · 4 comments
Labels
Milestone

Comments

@on99
Copy link

@on99 on99 commented Oct 12, 2019

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

$ go version
go version go1.12.7 darwin/amd64

Does this issue reproduce with the latest release?

Yes, it reproduces with go 1.13.1

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/on99/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/on99/.gvm/pkgsets/go1.12.7/global"
GOPROXY=""
GORACE=""
GOROOT="/Users/on99/.gvm/gos/go1.12.7"
GOTMPDIR=""
GOTOOLDIR="/Users/on99/.gvm/gos/go1.12.7/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/18/tghhmy8j77bgv7c0p4zqgw440000gp/T/go-build762921926=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://play.golang.org/p/mtyMTtDj5vj

What did you expect to see?

I expect it returns true.

What did you see instead?

It returns false.

@gopherbot gopherbot added this to the Unreleased milestone Oct 12, 2019
@ericlagergren

This comment has been minimized.

Copy link
Contributor

@ericlagergren ericlagergren commented Oct 13, 2019

I'm not too familiar with the rate limiting algorithm, but the bug seems to come from a floating-point imprecision.

First, Limiter.advance calls tokensFromDuration(1299999999), which computes

1.299999999 * 0.7692307692307693 = 0.9999999992307692 (current)
1.299999999 * 0.7692307692307693 = 0.9999999992307693207692307 (actual)

That result (after subtracting n on line 320, which is 1) then skews durationFromTokens

(7.692307901763229e-10 / 0.7692307692307693)*1e9 = 1.0000000272292198 (current)
(7.692306792307693e-10 / 0.7692307692307693)*1e9 = 0.9999998830000000000000105300000000 (actual)

Since that result is truncated (converted to an int64), the comparison on line 329 evaluates to false since waitDuration (1) <= maxFutureReserve (0).

Manually fixing the inaccuracy inside tokensFromDuration fixes the bug.

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Oct 13, 2019

Change https://golang.org/cl/200917 mentions this issue: rate: fix truncated seconds in tokensFromDuration

@on99

This comment has been minimized.

Copy link
Author

@on99 on99 commented Oct 14, 2019

@ericlagergren Thank you, I got it.

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Oct 23, 2019

Change https://golang.org/cl/200900 mentions this issue: rate: fix rounding error in tokensFromDuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.