Class: Watesan::FixedWindow

Inherits:
Abstract show all
Defined in:
lib/watesan/fixed_window.rb

Instance Method Summary collapse

Methods inherited from Abstract

#init

Constructor Details

#initialize(key:, options: Constants::DEFAULT_TORQUE_OPTIONS, redis_options: Constants::DEFAULT_REDIS_OPTIONS) ⇒ FixedWindow

Returns a new instance of FixedWindow.



6
7
8
9
# File 'lib/watesan/fixed_window.rb', line 6

def initialize(key:, options: Constants::DEFAULT_TORQUE_OPTIONS, redis_options: Constants::DEFAULT_REDIS_OPTIONS)
  init(redis_options, options)
  @key = key
end

Instance Method Details

#callObject

TODO: return with message



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/watesan/fixed_window.rb', line 12

def call
  requests = @redis.get(@key).to_i
  if requests >= @options[:max_requests]
    true
  else
    @redis.multi do |multi|
      multi.incr(@key)
      multi.expire(@key, @options[:window_size])
    end
    false
  end
end

#limit_reached?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/watesan/fixed_window.rb', line 25

def limit_reached?
  call
end