-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
41 lines (34 loc) · 897 Bytes
/
example_test.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
package miser_test
import (
"fmt"
"github.com/pantianying/miser"
"github.com/pantianying/miser/store/memstore"
"log"
)
//example:如何使用rateLimiter
func ExampleRateLimiter() {
store, err := memstore.New(65536)
if err != nil {
log.Fatal(err)
}
// Maximum burst of 5 which refills at 1 token per hour.
quota := miser.RateQuota{MaxRate: miser.PerHour(1), MaxBurst: 5}
rateLimiter, err := miser.NewGCRARateLimiter(store, quota)
if err != nil {
log.Fatal(err)
}
for i := 0; i < 20; i++ {
bucket := fmt.Sprintf("by-order:%v", i/10)
limited, result, err := rateLimiter.RateLimit(bucket, 1)
if err != nil {
log.Fatal(err)
}
if limited {
fmt.Printf("Iteration %2v; bucket %v: FAILED. Rate limit exceeded.\n",
i, bucket)
} else {
fmt.Printf("Iteration %2v; bucket %v: Operation successful (remaining=%v).\n",
i, bucket, result.Remaining)
}
}
}