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

unexpected excute result #41083

mh47838704 opened this issue Aug 28, 2020 · 1 comment

unexpected excute result #41083

mh47838704 opened this issue Aug 28, 2020 · 1 comment


Copy link

@mh47838704 mh47838704 commented Aug 28, 2020

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

$ go version

Does this issue reproduce with the latest release?

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

go env Output
$ go env

What did you do?

type HttpLimiter struct {
	value int
	globalLimiter *rate.Limiter

func (httpLimiter *HttpLimiter) SetNewLimitValue(value int) {
	//defer httpLimiter.Unlock()
	httpLimiter.globalLimiter = rate.NewLimiter(rate.Limit(value), value)
	**fmt.Println("has set new limiter") // 注释掉这句话,该函数不起作用(comments this line, the test result is not the same )**

func (httpLimiter *HttpLimiter) GetGlobalLimiter() *rate.Limiter {
	//defer httpLimiter.RUnlock()
	return httpLimiter.globalLimiter

// execute this for limiter change test
func TestDynamicLimiter(t *testing.T) {
	limitValue := 1
	threadCount := 10
	httpLimiter := &HttpLimiter{
		globalLimiter: rate.NewLimiter(rate.Limit(limitValue), limitValue),
		value:         1,
	for i := 1; i < threadCount; i++ {
		go mockHttpRequest(i, httpLimiter)
	time.Sleep(10 * time.Second)
	fmt.Println("reset limiter value")
	limitValue = 100
	fmt.Println("limit value:", limitValue)
	//httpLimiter.globalLimiter = nil
	for ; ; {

func mockHttpRequest(id int, limiter *HttpLimiter) {
	for ; ; {
		time.Sleep(100 * time.Millisecond)
		globalLimiter := limiter.GetGlobalLimiter()
		//bust := globalLimiter.Burst()
		if globalLimiter.Allow() {
			fmt.Printf("——%d:request pass\n", id)
		fmt.Printf("——%d:too many request\n", id)
		//// reserve的方式会增加某一个请求的超时时间
		//reserve := getGlobalLimiter().Reserve()
		//if !reserve.OK() {
		//	fmt.Println(id, "too many request")
		//	continue
		//// wait for the specific time
		//fmt.Println(id, "reserve request pass")

What did you expect to see?

"func (httpLimiter *HttpLimiter) SetNewLimitValue(value int) " this method set a new limiter, but it not effect when just have one line“httpLimiter.globalLimiter = rate.NewLimiter(rate.Limit(value), value)”, but take effect when add a "print" line , this is wired, i doubt the go compiler optimization the code when compile

What did you see instead?

Copy link

@davecheney davecheney commented Aug 28, 2020

Thank you for raising this issue. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@davecheney davecheney closed this Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.