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
We always think timeout as the time that one service has on response. In this case, timeout 's meaning should be ** we don't receive any message from c channel for 5 seconds.
so maybe the right code is :
timeout:=time.After(5*time.Second)
for {
select {
cases:=<-c:
fmt.Println(s)
timeout=time.After(5*time.Second) // update timeout timecase<-timeout:
fmt.Println("channel has no response for 5 seconds")
return
}
}
the origin code seems that you want to receive from every channel for same time, like linux cfs scheduler. so maybe in that case we should use ticker
I think this example does what it intends to, which is to run for a duration of timeout from the start , as the select is wrapped in an infinite loop.
If the intention is to give an example of waiting for a fixed timeout for each request(such as get a string from boring goroutine), he will write
for {
select {
cases:=<-c:
fmt.Println(s)
case<-time.After(5*time.Second):
fmt.Println("channel has no response for 5 seconds, you talk too slow")
return
}
}
which does refresh the timer in each select.
inferred from the example code fmt.Println("You talk too much.") , this is more likely to run for a fixed period of time and
get a bunch of responses before quit, but not an example of timeout for each single request, cause that would happen at the first request, which make the talk too much part nonsense
go-concurrency-patterns/6-select-timeout/main.go
Lines 25 to 35 in 2c16586
We always think
timeout
as the time that one service has on response. In this case,timeout
's meaning should be ** we don't receive any message fromc
channel for 5 seconds.so maybe the right code is :
the origin code seems that you want to receive from every channel for same time, like linux cfs scheduler. so maybe in that case we should use ticker
and it's output is
The text was updated successfully, but these errors were encountered: