Class: FailToBan::Strategies::BackoffStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/fail_to_ban/strategies/backoff_strategy.rb

Instance Method Summary collapse

Constructor Details

#initialize(key:, storage:, config: {}) ⇒ BackoffStrategy



16
17
18
19
20
# File 'lib/fail_to_ban/strategies/backoff_strategy.rb', line 16

def initialize(key:, storage:, config: {})
  @storage = storage
  @config = default_config.merge(config)
  @id = "#{HEADER}:#{key}"
end

Instance Method Details

#attemptObject



22
23
24
25
26
# File 'lib/fail_to_ban/strategies/backoff_strategy.rb', line 22

def attempt
  return :blocked if blocked?
  increment_failed_attempts
  :ok
end

#blocked?Boolean



28
29
30
# File 'lib/fail_to_ban/strategies/backoff_strategy.rb', line 28

def blocked?
  retry_count >= @config[:permitted_attempts] && Time.now.utc.to_i < unlock_at
end

#resetObject



32
33
34
# File 'lib/fail_to_ban/strategies/backoff_strategy.rb', line 32

def reset
  @storage.del(@id)
end

#unlock_atObject



36
37
38
# File 'lib/fail_to_ban/strategies/backoff_strategy.rb', line 36

def unlock_at
  @storage.hget(@id, 'unlock_at').to_i
end