You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
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?
The text was updated successfully, but these errors were encountered: