-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (40 loc) · 1.39 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
package main
import (
"github.com/althk/ratelimiter"
rlhttp "github.com/althk/ratelimiter/http"
"github.com/redis/go-redis/v9"
"log"
"net/http"
"time"
)
// ping simply responds with a "pong" plain text response for all requests
func ping(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "plain/text")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("pong\n"))
}
func main() {
// wrap the ping handler with a redis backed sliding window limiter
// configure the limiter to 1 request per sec per IP
opts := &ratelimiter.LimiterOptions{
// rate limiting algorithm
Algo: ratelimiter.SlidingWindow,
// the storage backend for the rate limiter
StoreType: ratelimiter.Redis,
// The max burst rate, or limit per window/bucket
ReqLimit: 1,
// TokenBucketRate is needed when Algo is set to ratelimiter.TokenBucket
// TokenBucketRate: 0,
// SlidingWindowDuration is needed when Algo is set to ratelimiter.SlidingWindow
SlidingWindowDuration: time.Second,
// RedisOpts is needed when StoreType is ratelimiter.Redis
RedisOpts: &redis.Options{Addr: "localhost:6379"},
}
limitedPing := rlhttp.WithLimiter(ping, opts)
http.HandleFunc("/unlimitedping", ping)
// configure the route /limitedping to be handled by the rate limiter handler
http.Handle("/limitedping", limitedPing)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}