Skip to content

Commit ae11543

Browse files
authored
Add httpate.Key(string) helper for static keys (#45)
Useful to create compounds with a custom prefix
1 parent 5e681e3 commit ae11543

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

httprate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ func LimitByRealIP(requestLimit int, windowLength time.Duration) func(next http.
3535
return Limit(requestLimit, windowLength, WithKeyFuncs(KeyByRealIP))
3636
}
3737

38+
func Key(key string) func(r *http.Request) (string, error) {
39+
return func(r *http.Request) (string, error) {
40+
return key, nil
41+
}
42+
}
43+
3844
func KeyByIP(r *http.Request) (string, error) {
3945
ip, _, err := net.SplitHostPort(r.RemoteAddr)
4046
if err != nil {

limiter.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ func NewRateLimiter(requestLimit int, windowLength time.Duration, options ...Opt
3333
}
3434

3535
if rl.keyFn == nil {
36-
rl.keyFn = func(r *http.Request) (string, error) {
37-
return "*", nil
38-
}
36+
rl.keyFn = Key("*")
3937
}
4038

4139
if rl.limitCounter == nil {

0 commit comments

Comments
 (0)