9 "github.com/anacrolix/chansync"
12 type Reservation struct {
17 granted chansync.SetOnce
18 cancelled chansync.SetOnce
21 // Releases the alloc claim if the reservation has been granted. Does nothing if it was cancelled.
23 func (me *Reservation) Release() {
29 case me.cancelled.IsSet():
31 case me.granted.IsSet():
33 me.releaseOnce.Do(func() {
38 // Cancel the reservation, returns false if it was already granted. You must still release if that's
39 // the case. See Drop.
40 func (me *Reservation) Cancel() bool {
43 if me.granted.IsSet() {
46 if me.cancelled.Set() {
52 // If the reservation is granted, release it, otherwise cancel the reservation.
53 func (me *Reservation) Drop() {
56 if me.granted.IsSet() {
57 me.releaseOnce.Do(func() {
62 if me.cancelled.Set() {
67 func (me *Reservation) wake() bool {
70 if me.cancelled.IsSet() {
73 return me.granted.Set()
76 func (me *Reservation) Wait(ctx context.Context) error {
78 return fmt.Errorf("reservation for %v exceeds limiter max %v", me.n, me.l.Max)
82 case <-me.granted.Done():
83 case <-me.cancelled.Done():
88 case me.granted.IsSet():
90 case me.cancelled.IsSet():
91 return errors.New("reservation cancelled")
92 case ctx.Err() != nil: