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 generates much more tokens than expected #56263

Closed
jiabiaoreal opened this issue Oct 17, 2022 · 2 comments
Closed

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/WQ3RvLBPzZv

l1 := rate.NewLimiter(1, 1)
l2 := rate.NewLimiter(1, 1)

count1 := 0
count2 := 0
s := time.Now()
for now := time.Now(); now.Sub(s) < time.Second; now = time.Now() {
	if l1.AllowN(now, 1) {
		count1 += 1
	}
	if l1.AllowN(now.Add(time.Second), 1) {
		count1 += 1
	}

	if l2.AllowN(now.Add(time.Second), 1) {
		count2 += 1
	}
	if l2.AllowN(now.Add(time.Second), 1) {
		count2 += 1
	}

	time.Sleep(1 * time.Millisecond)
}
fmt.Printf("count1: %v\ncount2: %v\n", count1, count2)

What did you expect to see?

count1, count2 are closer

What did you see instead?

count1: 1001
count2: 1

There maybe a bug in function advance, line 402.
https://github.com/golang/time/blob/master/rate/rate.go#L402

if t.Before(last) {
	last = t
}

should be

if t.Before(last) {
	t = last
}
@gopherbot gopherbot added this to the Unreleased milestone Oct 17, 2022
@ZekeLu
Copy link
Contributor

ZekeLu commented Oct 17, 2022

if t.Before(last) {
-	last = t
+	t = last
}

The suggested change is incorrect because some of the tests will fail if it's applied. This issue can be considered as another instance of #52584, which has a pending fix, golang/time#22 (https://golang.org/cl/406154).

@seankhliao
Copy link
Member

Duplicate of #52584

@seankhliao seankhliao marked this as a duplicate of #52584 Oct 17, 2022
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2022
@golang golang locked and limited conversation to collaborators Oct 17, 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

4 participants