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: alternating AllowN before last and after last can create new tokens #56287

Closed
tentree opened this issue Oct 18, 2022 · 7 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@tentree
Copy link

tentree commented Oct 18, 2022

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

$ go version
go version go1.18 windows/amd64

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
PS D:\code\src\yangr\rate> go env
set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\yangr\AppData\Local\go-build
set GOENV=C:\Users\yangr\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\yangr\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\yangr\go
set GOPRIVATE=
set GOPROXY=https://goproxy.io
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\code\src\yangr\rate\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\yangr\AppData\Local\Temp\go-build38909240=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
	"fmt"
	"golang.org/x/time/rate"

	"time"
)

func main() {
	limiter := rate.NewLimiter(rate.Every(time.Second/1), 10)

	t0 := time.Now()
	t1 := time.Now().Add(-time.Second * 2)

	t := t0
	for limiter.AllowN(t, 1) {
		if t == t0 {
			t = t1
		} else {
			t = t0
		}
	}

	fmt.Println("exit")
}

What did you expect to see?

Exit loop

What did you see instead?

Always loop.

mark:
i also use the fixed(ZekeLu/time@127e7a4) of #52584, but it did not work.

@gopherbot gopherbot added this to the Unreleased milestone Oct 18, 2022
@tentree tentree changed the title x/time/rate: x/time/rate: alternate of befoer last and after last can create new tokens Oct 18, 2022
@dr2chase dr2chase changed the title x/time/rate: alternate of befoer last and after last can create new tokens x/time/rate: alternating AllowN before last and after last can create new tokens Oct 18, 2022
@dr2chase
Copy link
Contributor

dr2chase commented Oct 18, 2022

@bcmills , @ianlancetaylor can you give this a look?

@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 18, 2022
@bcmills
Copy link
Contributor

bcmills commented Oct 18, 2022

x/time/rate is more @Sajmani...

@ZekeLu
Copy link
Contributor

ZekeLu commented Oct 19, 2022

I think this is work as designed. It's covered by the test TestLimiterJumpBackwards (https://github.com/golang/time/blob/f3bd1da661afd6357b957af27c50ccdb248b7dd3/rate/rate_test.go#L124-L138). On line 127, it jumps backward from t1 to t0. And then on line 131, it jumps forward from t0 to t1, and it got a new token.

In your demo, every time it jumps from t1 to t0, it gets two new tokens. That's why the available tokens never reduce to 0.

By the way, @tentree please change your comment to put your code into a fenced code block:

```go
package main

import (
	"fmt"
	"time"

	"golang.org/x/time/rate"
)

func main() {
	limiter := rate.NewLimiter(1, 10)

	t0 := time.Now()
	t1 := time.Now().Add(-time.Second * 2)

	t := t0
	for limiter.AllowN(t, 1) {
		if t == t0 {
			t = t1
		} else {
			t = t0
		}
	}

	fmt.Println("exit")
}
```

@tentree
Copy link
Author

tentree commented Oct 19, 2022

I think this is work as designed. It's covered by the test TestLimiterJumpBackwards (https://github.com/golang/time/blob/f3bd1da661afd6357b957af27c50ccdb248b7dd3/rate/rate_test.go#L124-L138). On line 127, it jumps backward from t1 to t0. And then on line 131, it jumps forward from t0 to t1, and it got a new token.

In your demo, every time it jumps from t1 to t0, it gets two new tokens. That's why the available tokens never reduce to 0.

By the way, @tentree please change your comment to put your code into a fenced code block:

package main

import (
	"fmt"
	"time"

	"golang.org/x/time/rate"
)

func main() {
	limiter := rate.NewLimiter(1, 10)

	t0 := time.Now()
	t1 := time.Now().Add(-time.Second * 2)

	t := t0
	for limiter.AllowN(t, 1) {
		if t == t0 {
			t = t1
		} else {
			t = t0
		}
	}

	fmt.Println("exit")
}

since t1 to t0 could got new tokens, i think t0 back to t1 also could reduce tokens. This is what I'm confused about.

@ZekeLu
Copy link
Contributor

ZekeLu commented Oct 19, 2022

@tentree Please don't quote an entire comment unnecessarily (edit your comment to remove it). Jumping backward won't reduce the tokens. It's also covered by that same test (line 127, jump back to t0, two tokens remain).

@tentree
Copy link
Author

tentree commented Oct 19, 2022

@ZekeLu thanks , i may understand the designe of limiter. back in the past just like fixed clock. the func with N(like AllowN) is just for testing easily, Is my understanding biased?

@ZekeLu
Copy link
Contributor

ZekeLu commented Oct 19, 2022

In my opinion, the time.Time parameter in the *N funcs is just for testing. Regarding whether the tokens should be dropped when it jumps backwards, there are some interesting comments here: https://go-review.googlesource.com/c/time/+/16672/2..6/rate/rate.go#b65

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. Please close this issue if there is not bug. See https://github.com/golang/go/wiki/Questions.

@tentree tentree closed this as completed Oct 19, 2022
@golang golang locked and limited conversation to collaborators Oct 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants