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

Add limiter type #136

Merged
merged 1 commit into from
Dec 29, 2023
Merged

Add limiter type #136

merged 1 commit into from
Dec 29, 2023

Conversation

dhh
Copy link
Member

@dhh dhh commented Dec 29, 2023

A limiter is a specialized form of a counter that can be checked whether it has been exceeded and is provided fail safe. This means it can be used to guard login screens from brute force attacks without denying access in case Redis is offline.

It will usually be used as an expiring limiter. Note that the limiter expires in total after the expires_in time used upon the first poke.

It offers no guarentee that you can't poke yourself above the limit. You're responsible for checking #exceeded? yourself first, and this may produce a race condition. So only use this when the exact number of pokes is not critical.

limiter = Kredis.limiter "mylimit", limit: 3, expires_in: 5.seconds
0 == limiter.value              # => GET "limiter"
limiter.poke                    # => SET limiter 0 NX + INCRBY limiter 1
limiter.poke                    # => SET limiter 0 NX + INCRBY limiter 1
limiter.poke                    # => SET limiter 0 NX + INCRBY limiter 1
false == limiter.exceeded?      # => GET "limiter"
limiter.poke                    # => SET limiter 0 NX + INCRBY limiter 1
true == limiter.exceeded?       # => GET "limiter"
sleep 6
limiter.poke                    # => SET limiter 0 NX + INCRBY limiter 1
limiter.poke                    # => SET limiter 0 NX + INCRBY limiter 1
limiter.poke                    # => SET limiter 0 NX + INCRBY limiter 1
false == limiter.exceeded?      # => GET "limiter"

@dhh dhh merged commit 4e7154a into main Dec 29, 2023
34 checks passed
@dhh dhh deleted the add-limiter branch December 29, 2023 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant