超时实现 select 计时器

发布时间 2023-11-14 17:43:20作者: papering

 

github.com\eclipse\paho.mqtt.golang@v1.4.3\token.go

// WaitTimeout implements the Token WaitTimeout method.
func (b *baseToken) WaitTimeout(d time.Duration) bool {
    timer := time.NewTimer(d)
    select {
    case <-b.complete:
        if !timer.Stop() {
            <-timer.C
        }
        return true
    case <-timer.C:
    }

    return false
}