Add :fix_window_per_key algorithm for ETS and Atomic backends#183
Merged
Conversation
A fixed-window variant whose window is anchored to each key's first hit instead of a globally-aligned wall-clock epoch. Keeps the same one-entry-per-key memory profile as :fix_window. The 2x boundary burst is still possible per key, but boundaries are no longer globally synchronized, so they cannot be exploited deterministically. Same semantics as the common Redis INCR + EXPIRE NX pattern. Closes #181.
…es in ETS and Atomic backends (#184)
- Add allow_or_deny/3 to ETS and Atomic FixWindowPerKey backends - Extract do_inc/4 in Atomic backend, mirroring do_hit/5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #181.
Summary
Adds
:fix_window_per_keyto both the ETS and Atomic backends. It's a fixed-window variant whose window is anchoredto each key's first hit instead of a globally-aligned wall-clock epoch (multiples of
scalesince Unix epoch,like
:fix_windowuses today).Same one-entry-per-key memory profile as
:fix_window. The 2× boundary burst is still theoretically possible perkey, but boundaries are no longer globally synchronized — they can't be timed deterministically by an attacker, and
a key has to wait a full
scalebetween burst opportunities. Same semantics as the common RedisINCR + EXPIRE NXpattern.
Naming chosen deliberately over
:rolling_window— the algorithm is mechanically a fixed window, just with aper-key epoch. The docstring is explicit that the 2× burst is still possible, so users reaching for this won't
expect
:sliding_window-level precision.What's included
Hammer.ETS.FixWindowPerKeyandHammer.Atomic.FixWindowPerKeyHammer.ETSandHammer.Atomic(incl.before_cleanalgorithm name)clean_fix_windowpath (identical row layout)fix_windowtests, plus distinguishing tests for per-key window anchoring,expiry behavior, and the atomic race condition
before_cleancallback tests in bothclean_test.exsfiles## UnreleasedentryTest plan
mix test— 129/129 passingmix credo --strict— cleanmix format --check-formatted— clean