超时实现 select 计时器

 

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
}

 

 

posted @ 2023-11-14 17:37  papering  阅读(6)  评论(0编辑  收藏  举报