Class: Gitlab::RackAttack::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/rack_attack/store.rb

Constant Summary collapse

InvalidAmount =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#delete(key, _options = {}) ⇒ Object



34
35
36
# File 'lib/gitlab/rack_attack/store.rb', line 34

def delete(key, _options = {})
  with { |redis| redis.del(namespace(key)) }
end

#increment(key, amount = 1, options = {}) ⇒ Object

The increment method gets called very often. The implementation below aims to minimize the number of Redis calls we make.

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab/rack_attack/store.rb', line 10

def increment(key, amount = 1, options = {})
  # Our code below that prevents calling EXPIRE after every INCR assumes
  # we always increment by 1. This is true in Rack::Attack as of v6.6.1.
  # This guard should alert us if Rack::Attack changes its behavior in a
  # future version.
  raise InvalidAmount unless amount == 1

  with do |redis|
    key = namespace(key)
    new_value = redis.incr(key)
    expires_in = options[:expires_in]
    redis.expire(key, expires_in) if new_value == 1 && expires_in
    new_value
  end
end

#read(key, _options = {}) ⇒ Object



26
27
28
# File 'lib/gitlab/rack_attack/store.rb', line 26

def read(key, _options = {})
  with { |redis| redis.get(namespace(key)) }
end

#write(key, value, options = {}) ⇒ Object



30
31
32
# File 'lib/gitlab/rack_attack/store.rb', line 30

def write(key, value, options = {})
  with { |redis| redis.set(namespace(key), value, ex: options[:expires_in]) }
end