aboutsummaryrefslogtreecommitdiff
path: root/weed/util/cond_wait.go
blob: 43b45de7c2536a6b86b7da2d5a85cd474bf75200 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
	}
}