pic
(Nicola Piccinini)
November 10, 2020, 10:04pm
1
Hi,
I’ve seen some discussion about this, for example this:
Is there any plan to get rate limiting into caddy v2 as a default module or external module? I can help with migration.
and especially this:
opened 01:23PM - 02 Jun 20 UTC
closed 06:11PM - 28 Jan 21 UTC
feature
plugin
Please consider adding rate limit or limit connection per IP feature on Caddy 2.… Thanks!!
I’ve taken over the work by sk0g and I’ve published this:
It has to support also Header like the limit_by_header
directive of caddy1 plugin.
What do you think? Thanks
2 Likes
matt
(Matt Holt)
November 10, 2020, 10:46pm
2
Cool! Thanks for publishing that.
I’d recommend making this a caddy.Duration
type, if possible, to be consistent with other timeframes in Caddy configuration:
// register a plugin that can load the Caddyfile when Caddy starts
httpcaddyfile.RegisterHandlerDirective("rate_limit", parseRateLimit)
}
// rateLimitOptions stores options detailing how rate limiting should be applied,
// as well as the current and previous window's key:requestCount mapping
type RateLimit struct {
ByHeader string `json:"by_header,omitempty"`
// window length for request rate checking (>= 1 minute)
WindowLength int64 `json:"window_length"`
// max request that should be processed per key in a given windowDuration
MaxRequests int64 `json:"max_requests"`
// current window's request count per key
currentWindow *RequestCountTracker
// previous window's request count per key
previousWindow *RequestCountTracker
}
Consider expanding on the godoc as that is what users will see when they look for documentation.
(Other than this, I haven’t given it any code review. Just a quick glance.)
Great start!
pic
(Nicola Piccinini)
November 18, 2020, 10:29am
3
Thanks for the feedback Matt.
Do you have any code sample about using caddy.Duration in a plugin? It would help me a lot. Thanks
You just need to use the caddy.Duration
type instead of int64
:
HTTPPort int `json:"http_port,omitempty"`
// HTTPSPort specifies the port to use for HTTPS, which is used when
// solving the ACME TLS-ALPN challenges, or whenever HTTPS is needed
// but no specific port number is given. Default: 443.
HTTPSPort int `json:"https_port,omitempty"`
// GracePeriod is how long to wait for active connections when shutting
// down the server. Once the grace period is over, connections will
// be forcefully closed.
GracePeriod caddy.Duration `json:"grace_period,omitempty"`
// Servers is the list of servers, keyed by arbitrary names chosen
// at your discretion for your own convenience; the keys do not
// affect functionality.
Servers map[string]*Server `json:"servers,omitempty"`
servers []*http.Server
h3servers []*http3.Server
h3listeners []net.PacketConn
And parse it like this for Caddyfile support:
https://github.com/francislavoie/caddy/blob/caddyfile-server-options/caddyconfig/httpcaddyfile/serveroptions.go#L90-L97
Also, I strongly suggest to use ,omitempty
in your json tags, because otherwise, running caddy adapt
will output "window_length": 0
even if the user never explicitly configured that value.
1 Like
pic
(Nicola Piccinini)
November 27, 2020, 8:40am
5
Thanks for the help.
I’ve updated the plugin with your suggestion: Use duration for WindowLength · hundertzehn/caddy-ratelimit@45bd8b1 · GitHub
Best regards
2 Likes
system
(system)
Closed
December 27, 2020, 8:40am
6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.