aboutsummaryrefslogtreecommitdiff
path: root/weed/util/cond_wait.go
diff options
context:
space:
mode:
Diffstat (limited to 'weed/util/cond_wait.go')
-rw-r--r--weed/util/cond_wait.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/weed/util/cond_wait.go b/weed/util/cond_wait.go
new file mode 100644
index 000000000..43b45de7c
--- /dev/null
+++ b/weed/util/cond_wait.go
@@ -0,0 +1,32 @@
+package util
+
+import (
+ "context"
+ "net/http"
+ "sync"
+ "time"
+)
+
+const HttpStatusCancelled = 499
+
+func WaitWithTimeout(ctx context.Context, cond *sync.Cond, timer *time.Timer) int {
+ waitDone := make(chan struct{})
+
+ go func() {
+ cond.L.Lock()
+ defer cond.L.Unlock()
+ cond.Wait()
+ defer close(waitDone)
+ }()
+
+ select {
+ case <-waitDone:
+ return http.StatusOK
+ case <-timer.C:
+ cond.Broadcast()
+ return http.StatusTooManyRequests
+ case <-ctx.Done():
+ cond.Broadcast()
+ return HttpStatusCancelled
+ }
+}