-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunlimitedLimiter.go
More file actions
55 lines (45 loc) · 1.04 KB
/
unlimitedLimiter.go
File metadata and controls
55 lines (45 loc) · 1.04 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package limiter
import (
"context"
"time"
)
type UnlimitedLimiter struct {
}
func NewUnlimitedLimiter() *UnlimitedLimiter {
return &UnlimitedLimiter{}
}
type UnlimitedLimiterReservation struct {
ctx context.Context
}
func (u *UnlimitedLimiter) Wait() <-chan bool {
m := u.Reserve(context.Background(), nil)
return m.Wait()
}
func (u *UnlimitedLimiter) Reserve(ctx context.Context, options interface{}) Reservation {
return &UnlimitedLimiterReservation{
ctx: ctx,
}
}
//cancels the reservation.
func (ulr *UnlimitedLimiterReservation) Cancel() {
//no-op as unlimited doesent require cancelations
}
//Delay returns the delay from now before the reservation completes.
func (ulr *UnlimitedLimiterReservation) Delay() time.Duration {
return 0
}
//Ok returns if the reservation has reached its done state.
func (ulr *UnlimitedLimiterReservation) Ok() bool {
return true
}
func (u *UnlimitedLimiterReservation) Wait() <-chan bool {
m := make(chan bool)
go func() {
select {
case <-u.ctx.Done():
case m <- true:
}
close(m)
}()
return m
}