选择忽略的慢速情况?

选择忽略的慢速情况?

问题描述:

I'm using such a select on multiple cases:

for {
    select {
    case data:= <- highFreqChan:
        // do something:
    case <- time.After(time.Second * 5):
        // send some heartbeat like data...
    }
}

I find that, if highFreqChan got huge data, the heartbeat case will not enter, and when stop sending data to highFreqChan, the heartbeat case alive again, how to make it always enter the heartbeat case while the highFreqChan still working?

You're running time.After as part of the select condition, which means it's evaluated at the time the select is reached... which means that it will expire 5 seconds after the select is reached. So you will only enter that case if highFreqChan doesn't have any data for 5 seconds straight.

If you want to do something every 5 seconds, use a time.Ticker instead, like so:

heartbeat := time.NewTicker(5 * time.Second)
defer heartbeat.Stop()
for {
    select {
    case data:= <- highFreqChan:
        // do something:
    case <- heartbeat.C:
        // send some heartbeat like data...
    }
}