-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (63 loc) · 1.79 KB
/
main.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"log"
"sync"
"time"
"github.com/2asm/ratelimiter/leakyBucket"
"github.com/2asm/ratelimiter/slidingWindow"
"github.com/2asm/ratelimiter/tokenBucket"
)
func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
// window size = 1 second (1000 milisecond)
// events per window = 2
limiter1 := slidingwindow.NewLimiter(2, time.Millisecond*1000)
fmt.Printf("sliding bucket\n")
log.Printf("%v\n", limiter1.Allow())
log.Printf("%v\n", limiter1.Allow())
log.Printf("%v\n", limiter1.Allow())
log.Printf("%v\n", limiter1.Allow())
log.Printf("%v\n", limiter1.Allow())
// 2 events per second (frequency)
// burst size = 5
limiter2 := tokenbucket.NewLimiter(2, 5)
fmt.Printf("\ntoken bucket\n")
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
time.Sleep(time.Second * 1)
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
time.Sleep(time.Second * 3)
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
log.Printf("%v\n", limiter2.Allow())
// 2 events per second
// bucket size = 4
wg := sync.WaitGroup{}
limiter := leakyBucket.NewLimiter(2, 4)
fmt.Printf("\nleaky bucket\n")
for i := 1; i < 10; i++ {
t, ok := limiter.Allow()
if ok {
wg.Add(1)
go func(i int, t time.Duration) {
defer wg.Done()
time.Sleep(t)
log.Printf("Event %d completed\n", i)
}(i, t)
} else {
log.Printf("ERROR: Rate limit exceeded\n")
}
}
wg.Wait()
}