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/rate: function Limiter.AllowN does not work as expected #56145

Closed
jiabiaoreal opened this issue Oct 11, 2022 · 1 comment
Closed

x/time/rate: function Limiter.AllowN does not work as expected #56145

jiabiaoreal opened this issue Oct 11, 2022 · 1 comment

Comments

@jiabiaoreal
Copy link

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

$ go version
go version go1.17.8 darwin/arm64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/jiabiao/Library/Caches/go-build"
GOENV="/Users/jiabiao/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/jiabiao/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jiabiao/go"
GOPRIVATE=""
GOPROXY=""
GOROOT="/usr/local/go"
GOSUMDB=""
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.17.8"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/r0/1xtsxl1j5kb0yn0_9zn00kh80000gn/T/go-build2026362806=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

run https://go.dev/play/p/Zf2hlyhvZY0

func main() {
l := rate.NewLimiter(1, 1)
now := time.Now()
fmt.Println(l.AllowN(now, 1))
fmt.Println(l.AllowN(now.Add(time.Second), 1))
fmt.Println(l.AllowN(now, 1))
fmt.Println(l.AllowN(now.Add(time.Second), 1))
}

What did you expect to see?

true
true
false
false

What did you see instead?

true
true
false
true

@gopherbot gopherbot added this to the Unreleased milestone Oct 11, 2022
@seankhliao
Copy link
Member

program execution takes time, which affects the point of evaluation.
Note from the documentation:

in any large enough time interval, the Limiter limits the rate to r tokens per second

Changing the order of the lines will give you different results.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2022
@golang golang locked and limited conversation to collaborators Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants