-
Notifications
You must be signed in to change notification settings - Fork 4
/
rate-limiting.go
51 lines (42 loc) · 1.12 KB
/
rate-limiting.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
package main
import "fmt"
import "time"
// https://golang.org/pkg/time/#Tick
//
// func Tick(d Duration) <-chan Time
//
// Tick is a convenience wrapper for NewTicker providing access to the ticking channel only.
// While Tick is useful for clients that have no need to shut down the Ticker,
// be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks".
func main() {
requests := make(chan int, 5)
for i := 1; i <= 5; i++ {
requests <- i
}
close(requests)
limiter := time.Tick(time.Millisecond * 200)
for req := range requests {
<-limiter // wait ticks
fmt.Println("request", req, time.Now())
}
fmt.Println("")
burstyLimiter := make(chan time.Time, 3)
for i := 0; i < 3; i++ {
burstyLimiter <- time.Now()
}
go func() {
// use Ticker control time interval
for t := range time.Tick(time.Millisecond * 200) {
burstyLimiter <- t
}
}()
burstyRequests := make(chan int, 5)
for i := 1; i <= 5; i++ {
burstyRequests <- i
}
close(burstyRequests)
for req := range burstyRequests {
<-burstyLimiter
fmt.Println("request", req, time.Now())
}
}