-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter.go
40 lines (32 loc) · 1011 Bytes
/
limiter.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
package routes
import (
"go-graphql-mongo-server/config"
"go-graphql-mongo-server/logger"
"strconv"
"time"
"github.com/sethvargo/go-limiter/httplimit"
"github.com/sethvargo/go-limiter/memorystore"
)
var limiterMiddleware *httplimit.Middleware
func createLimiterMiddleware() {
apiLimitPerSecond, err := strconv.ParseUint(config.Store.APILimitPerSecond, 10, 64)
if err != nil || apiLimitPerSecond == 0 {
logger.Log.Error("Error parsing apiLimitPerSecond: " + err.Error())
return
}
limiterStore, err := memorystore.New(&memorystore.Config{
// Number of API calls allowed per interval
Tokens: apiLimitPerSecond,
// Interval for which the limit is applied
Interval: time.Second,
})
if err != nil {
logger.Log.Error("Error creating limiter store: " + err.Error())
return
}
limiterMiddleware, err = httplimit.NewMiddleware(limiterStore, httplimit.IPKeyFunc("X-Forwarded-For"))
if err != nil {
logger.Log.Error("Error creating limiter middleware: " + err.Error())
return
}
}