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: Allow() returns unexpected result #57074

Closed
cscps opened this issue Dec 4, 2022 · 1 comment
Closed

x/time/rate: Allow() returns unexpected result #57074

cscps opened this issue Dec 4, 2022 · 1 comment

Comments

@cscps
Copy link

cscps commented Dec 4, 2022

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

$ go version
go version go1.19 linux/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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ccssrryy.cache/go-build"
GOENV="/home/ccssrryy/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ccssrryy/go/pkg/mod"
GOOS="linux"
GOPATH="/home/ccssrryygo"
GOROOT="/home/ccssrryy/sdk/go1.19"
GOSUMDB="sum.golang.google.cn"
GOTMPDIR=""
GOTOOLDIR="/home/ccssrryy/sdk/go1.19/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/tmp/std/go.mod"
GOWORK=""
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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build125839981=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
        "log"
        "sync"
        "sync/atomic"
        "time"

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

func main() {
        var limitCount = 1000
        var count int32
        threads := 200
        stdLimiter := rate.NewLimiter(rate.Limit(limitCount), limitCount)
        var wg sync.WaitGroup

        startgs := func(threads int) {
                start := time.Now()
                for i := 0; i < threads; i++ {
                        go func() {
                                defer wg.Done()
                                for {
                                        if time.Since(start) > time.Second {
                                                return
                                        }
                                        if stdLimiter.Allow() {
                                                atomic.AddInt32(&count, 1)
                                        }
                                }
                        }()
                }
        }
        wg.Add(threads)
        startgs(threads)
        wg.Wait()
        if atomic.LoadInt32(&count) > int32(3*limitCount) {
                log.Fatalf("fail, unexpected count: %d \n", count)
        } else {
                log.Println("success")
        }
}

What did you expect to see?

success

What did you see instead?

fail, unexpected count: 115876

Why

Allow() is getting current time out of the mu lock.

@cscps cscps changed the title golang.org/x/time/rate: golang.org/x/time/rate: Allow() return unexpected result Dec 4, 2022
@cscps cscps changed the title golang.org/x/time/rate: Allow() return unexpected result golang.org/x/time/rate: Allow() returns unexpected result Dec 4, 2022
@seankhliao seankhliao changed the title golang.org/x/time/rate: Allow() returns unexpected result x/time/rate: Allow() returns unexpected result Dec 4, 2022
@gopherbot gopherbot added this to the Unreleased milestone Dec 4, 2022
@seankhliao
Copy link
Member

Duplicate of #45245

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