You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to check if a client is under the limit, without incrementing the count? I can see the cache class has a read method, but it looks like it's only used by fail2ban. My usecases revolve around checking if someone is at or above the limit and rendering a template differently (e.g. deciding to rendering a form with or without a captcha shouldn't count towards the limit).
Through a series of monkeypatches, I've achieved the behavior I'm looking for. Hopefully this can do a decent job of explaining my usecase. This allows me to use it in a controller like like
defshowiflimiter.at_limit?(request)@warning_message="Attempts exceeded."@challenge=new_captcha_formendenddefcreateiflimiter.matched_by?(request)validate_captchaendrest_of_the_actionenddeflimiterlimiter=Rack::Attack.track("requests from bucket",limit: 5,period: 3600)do |req|
bucketendend
monkeypatch:
Rack::Attack::Configuration.class_evaldo# nop'd because otherwise all Rack::Attack::Throttles are incremented# even if the gated functionality is never exerciseddeftracked?(request)endendRack::Attack::Cache.class_evaldodefget_count(unprefixed_key,period)enforce_store_presence!enforce_store_method_presence!(:read)key,expires_in=key_and_expiry(unprefixed_key,period)result=store.read(key)result || 0endendRack::Attack::Track.class_evaldodefat_limit?(request)filter.at_limit?(request)endendRack::Attack::Throttle.class_evaldodefat_limit?(request)discriminator=discriminator_for(request)returnfalseunlessdiscriminatorcurrent_period=period_for(request)current_limit=limit_for(request)count=cache.get_count("#{name}:#{discriminator}",current_period)count > current_limitendend
Activity
6f6d6172 commentedon Dec 1, 2022
Through a series of monkeypatches, I've achieved the behavior I'm looking for. Hopefully this can do a decent job of explaining my usecase. This allows me to use it in a controller like like
monkeypatch: