9 "github.com/anacrolix/chansync"
10 "github.com/anacrolix/log"
13 type Reservation struct {
18 granted chansync.SetOnce
19 cancelled chansync.SetOnce
22 // Releases the alloc claim if the reservation has been granted. Does nothing if it was cancelled.
24 func (me *Reservation) Release() {
30 case me.cancelled.IsSet():
32 case me.granted.IsSet():
34 me.releaseOnce.Do(func() {
39 // Cancel the reservation, returns false if it was already granted. You must still release if that's
40 // the case. See Drop.
41 func (me *Reservation) Cancel() bool {
44 if me.granted.IsSet() {
47 if me.cancelled.Set() {
53 // If the reservation is granted, release it, otherwise cancel the reservation.
54 func (me *Reservation) Drop() {
57 if me.granted.IsSet() {
58 me.releaseOnce.Do(func() {
63 if me.cancelled.Set() {
68 func (me *Reservation) wake() bool {
71 if me.cancelled.IsSet() {
74 return me.granted.Set()
77 func (me *Reservation) Wait(ctx context.Context) error {
81 fmt.Errorf("reservation for %v exceeds limiter max %v", me.n, me.l.Max),
86 case <-me.granted.Done():
87 case <-me.cancelled.Done():
92 case me.granted.IsSet():
94 case me.cancelled.IsSet():
95 return errors.New("reservation cancelled")
96 case ctx.Err() != nil: