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

Closed
mh47838704 opened this issue Aug 28, 2020 · 1 comment
Closed

unexpected excute result #41083

mh47838704 opened this issue Aug 28, 2020 · 1 comment

Comments

@mh47838704
Copy link

mh47838704 commented Aug 28, 2020

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

$ go version
1.13.6

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
	sync.RWMutex
	globalLimiter *rate.Limiter
}

func (httpLimiter *HttpLimiter) SetNewLimitValue(value int) {
	//httpLimiter.Lock()
	//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 {
	//httpLimiter.RLock()
	//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.SetNewLimitValue(limitValue)
	//这种方式生效太慢
	//httpLimiter.globalLimiter.SetLimit(1000)
	//httpLimiter.globalLimiter = nil
	for ; ; {
		continue
	}
}

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)
			continue
		}
		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
		//<-time.After(reserve.Delay())
		//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?

@davecheney
Copy link
Contributor

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:

@golang golang locked and limited conversation to collaborators Aug 28, 2021
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