-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
37 lines (29 loc) · 770 Bytes
/
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
package main
import (
"fmt"
"context"
"net/http"
"github.com/go-redis/redis/v8"
)
var redisClient *redis.Client
var ctx = context.Background()
func init() {
redisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
}
func main() {
// Configurations
userId := "user123"
// leakRate := int64(5) // 5 request per second
refillWindow := int64(60) // 60 seconds
// windowLengthInSeconds := int64(60) // window size is 60 seconds
maximumTokens := int64(10) // maximum 10 requests per window
for i := 1; i <= 25; i++ {
isAllowed := TokenBucketRateLimit(ctx, redisClient, userId, refillWindow, maximumTokens)
fmt.Printf("Request %d status - allowed: %t \n", i, isAllowed)
}
http.ListenAndServe(":8080", nil)
}