Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1037)

Unified Diff: client/internal/logdog/butler/bundler/timeoutCond.go

Issue 1412063008: logdog: Add bundler library. (Closed) Base URL: https://github.com/luci/luci-go@logdog-review-streamserver
Patch Set: Updated from comments. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
+}
« no previous file with comments | « client/internal/logdog/butler/bundler/textParser_test.go ('k') | client/internal/logdog/butler/bundler/timeoutCond_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698