| Index: client/internal/logdog/butler/bundler/timeoutCond.go
|
| diff --git a/client/internal/logdog/butler/bundler/timeoutCond.go b/client/internal/logdog/butler/bundler/timeoutCond.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..dfa7168660bc654d6b6ef87434aa960f6b9e182a
|
| --- /dev/null
|
| +++ b/client/internal/logdog/butler/bundler/timeoutCond.go
|
| @@ -0,0 +1,73 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package bundler
|
| +
|
| +import (
|
| + "sync"
|
| + "time"
|
| +
|
| + "github.com/luci/luci-go/common/clock"
|
| +)
|
| +
|
| +type timeoutCond struct {
|
| + *sync.Cond
|
| +
|
| + clock clock.Clock
|
| +}
|
| +
|
| +func newTimeoutCond(c clock.Clock, l sync.Locker) *timeoutCond {
|
| + return &timeoutCond{
|
| + Cond: sync.NewCond(l),
|
| + clock: c,
|
| + }
|
| +}
|
| +
|
| +func (c *timeoutCond) waitTimeout(d time.Duration) (timeout bool) {
|
| + if d <= 0 {
|
| + // We've already expired, so don't bother waiting.
|
| + return
|
| + }
|
| +
|
| + // Kick off a timer to Broadcast when it has completed.
|
| + doneC := make(chan struct{})
|
| + finishedC := make(chan bool)
|
| + defer func() {
|
| + close(doneC)
|
| + timeout = <-finishedC
|
| + }()
|
| +
|
| + go func() {
|
| + timerExpired := false
|
| + defer func() {
|
| + finishedC <- timerExpired
|
| + }()
|
| +
|
| + t := c.clock.NewTimer()
|
| + defer t.Stop()
|
| + t.Reset(d)
|
| +
|
| + // Quickly own our lock. This ensures that we have entered our Wait()
|
| + // method, which in turn ensures that our Broadcast will wake it if
|
| + // necessary.
|
| + func() {
|
| + c.L.Lock()
|
| + defer c.L.Unlock()
|
| + }()
|
| +
|
| + select {
|
| + case <-doneC:
|
| + break
|
| +
|
| + case <-t.GetC():
|
| + timerExpired = true
|
| + c.Broadcast()
|
| + }
|
| + }()
|
| +
|
| + // Wait on our Cond. This will be released when either something else
|
| + // Broadcasts. That "something else" may be our timer expiring.
|
| + c.Wait()
|
| + return
|
| +}
|
|
|