/
gateway_rate_limiter_config.go
43 lines (36 loc) · 1.24 KB
/
gateway_rate_limiter_config.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
package gateway
import (
"log/slog"
)
// DefaultRateLimiterConfig returns a RateLimiterConfig with sensible defaults.
func DefaultRateLimiterConfig() *RateLimiterConfig {
return &RateLimiterConfig{
Logger: slog.Default(),
CommandsPerMinute: CommandsPerMinute,
}
}
// RateLimiterConfig lets you configure your Gateway instance.
type RateLimiterConfig struct {
Logger *slog.Logger
CommandsPerMinute int
}
// RateLimiterConfigOpt is a type alias for a function that takes a RateLimiterConfig and is used to configure your Server.
type RateLimiterConfigOpt func(config *RateLimiterConfig)
// Apply applies the given RateLimiterConfigOpt(s) to the RateLimiterConfig
func (c *RateLimiterConfig) Apply(opts []RateLimiterConfigOpt) {
for _, opt := range opts {
opt(c)
}
}
// WithRateLimiterLogger sets the Logger for the Gateway.
func WithRateLimiterLogger(logger *slog.Logger) RateLimiterConfigOpt {
return func(config *RateLimiterConfig) {
config.Logger = logger
}
}
// WithCommandsPerMinute sets the number of commands per minute that the Gateway will allow.
func WithCommandsPerMinute(commandsPerMinute int) RateLimiterConfigOpt {
return func(config *RateLimiterConfig) {
config.CommandsPerMinute = commandsPerMinute
}
}