-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (143 loc) · 3.68 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package fiber_limiter
import (
"errors"
"net/http"
"strconv"
"time"
"github.com/go-redis/redis/v7"
"github.com/gofiber/fiber"
"github.com/shareed2k/go_limiter"
)
const (
SlidingWindowAlgorithm = go_limiter.SlidingWindowAlgorithm
GCRAAlgorithm = go_limiter.GCRAAlgorithm
DefaultKeyPrefix = "fiber_limiter"
)
// Config ...
type Config struct {
// Rediser
Rediser *redis.Client
// Max number of recent connections
// Default: 10
Max int
// Burst
Burst int
// StatusCode
// Default: 429 Too Many Requests
StatusCode int
// Message
// default: "Too many requests, please try again later."
Message string
// Algorithm
// Default: sliding window
Algorithm uint
// Prefix
// Default:
Prefix string
// SkipOnError
// Default: false
SkipOnError bool
// Period
Period time.Duration
// Filter defines a function to skip middleware.
// Optional. Default: nil
Filter func(*fiber.Ctx) bool
// Key allows to use a custom handler to create custom keys
// Default: func(ctx *fiber.Ctx) string {
// return ctx.IP()
// }
Key func(*fiber.Ctx) string
// Handler is called when a request hits the limit
// Default: func(ctx *fiber.Ctx) {
// ctx.Status(cfg.StatusCode).SendString(cfg.Message)
// }
Handler func(*fiber.Ctx)
// ErrHandler is called when a error happen inside go_limiiter lib
// Default: func(err error, ctx *fiber.Ctx) {
// ctx.Status(http.StatusInternalServerError).SendString(err.Error())
// }
ErrHandler func(error, *fiber.Ctx)
}
// New ...
func New(config Config) func(*fiber.Ctx) {
if config.Rediser == nil {
panic(errors.New("redis client is missing"))
}
if config.Handler == nil {
config.Handler = func(ctx *fiber.Ctx) {
ctx.Status(config.StatusCode).SendString(config.Message)
}
}
if config.ErrHandler == nil {
config.ErrHandler = func(err error, ctx *fiber.Ctx) {
ctx.Status(http.StatusInternalServerError).SendString(err.Error())
}
}
if config.Key == nil {
config.Key = func(ctx *fiber.Ctx) string {
return ctx.IP()
}
}
if config.Algorithm == 0 {
config.Algorithm = SlidingWindowAlgorithm
}
if config.Period == 0 {
config.Period = time.Minute
}
if config.Max == 0 {
config.Max = 10
}
if config.Burst == 0 {
config.Burst = 10
}
if config.Prefix == "" {
config.Prefix = DefaultKeyPrefix
}
if config.Message == "" {
config.Message = "Too many requests, please try again later."
}
if config.StatusCode == 0 {
config.StatusCode = http.StatusTooManyRequests
}
limiter := go_limiter.NewLimiter(config.Rediser)
limit := &go_limiter.Limit{
Period: config.Period,
Algorithm: config.Algorithm,
Rate: int64(config.Max),
Burst: int64(config.Burst),
}
// override default limiter prefix
limiter.Prefix = config.Prefix
return func(ctx *fiber.Ctx) {
// Filter request to skip middleware
if config.Filter != nil && config.Filter(ctx) {
ctx.Next()
return
}
result, err := limiter.Allow(config.Key(ctx), limit)
// if we have error lets just pass the request
if err != nil {
if config.SkipOnError {
ctx.Next()
return
}
config.ErrHandler(err, ctx)
return
}
// Check if hits exceed the max
if !result.Allowed {
// Call Handler func
config.Handler(ctx)
// Return response with Retry-After header
// https://tools.ietf.org/html/rfc6584
ctx.Set("Retry-After", strconv.FormatInt(time.Now().Add(result.RetryAfter).Unix(), 10))
return
}
// We can continue, update RateLimit headers
ctx.Set("X-RateLimit-Limit", strconv.Itoa(config.Max))
ctx.Set("X-RateLimit-Remaining", strconv.FormatInt(result.Remaining, 10))
ctx.Set("X-RateLimit-Reset", strconv.FormatInt(time.Now().Add(result.ResetAfter).Unix(), 10))
// Bye!
ctx.Next()
}
}