Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally delete stale distributed state #55

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ This is an HTTP handler module, so it can be used wherever `http.handlers` modul
}
"distributed": {
"write_interval": "",
"read_interval": ""
"read_interval": "",
"purge_age": ""
},
}
```
Expand Down Expand Up @@ -130,6 +131,7 @@ rate_limit {
distributed {
read_interval <duration>
write_interval <duration>
purge_age <duration>
}
storage <module...>
jitter <percent>
Expand Down
14 changes: 14 additions & 0 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func parseCaddyfile(helper httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, e
// distributed {
// read_interval <duration>
// write_interval <duration>
// purge_age <duration>
// }
// storage <module...>
// jitter <percent>
Expand Down Expand Up @@ -150,6 +151,19 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return d.Errf("invalid write interval '%s': %v", d.Val(), err)
}
h.Distributed.WriteInterval = caddy.Duration(interval)

case "purge_age":
if !d.NextArg() {
return d.ArgErr()
}
if h.Distributed.PurgeAge != 0 {
return d.Errf("purge age already specified: %v", h.Distributed.PurgeAge)
}
age, err := caddy.ParseDuration(d.Val())
if err != nil {
return d.Errf("invalid purge age '%s': %v", d.Val(), err)
}
h.Distributed.PurgeAge = caddy.Duration(age)
}
}

Expand Down
14 changes: 14 additions & 0 deletions distributed.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type DistributedRateLimiting struct {
// Default: 5s
ReadInterval caddy.Duration `json:"read_interval,omitempty"`

// How long to wait before deleting stale states from other instances.
// Default: never
PurgeAge caddy.Duration `json:"purge_age,omitempty"`

instanceID string

otherStates []rlState
Expand Down Expand Up @@ -153,6 +157,16 @@ func (h Handler) syncDistributedRead(ctx context.Context) error {
continue
}

if h.Distributed.PurgeAge != 0 && state.Timestamp.Before(now().Add(-time.Duration(h.Distributed.PurgeAge))) {
mholt marked this conversation as resolved.
Show resolved Hide resolved
err = h.storage.Delete(ctx, instanceFile)
if err != nil {
h.logger.Error("cannot delete rate limiter state file",
mholt marked this conversation as resolved.
Show resolved Hide resolved
zap.String("key", instanceFile),
zap.Error(err))
}
continue
}

otherStates = append(otherStates, state)
}

Expand Down
3 changes: 2 additions & 1 deletion distributed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ func TestDistributed(t *testing.T) {
},
"distributed": {
"write_interval": "3600s",
"read_interval": "3600s"
"read_interval": "3600s",
"purge_age": "7200s"
}
},
{
Expand Down