Class: ThrottleMachines::RackMiddleware::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/throttle_machines/rack_middleware/throttle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, &block) ⇒ Throttle

Returns a new instance of Throttle.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 8

def initialize(name, options, &block)
  @name = name
  @block = block

  raise ArgumentError, 'Must pass :limit option' unless options[:limit]
  raise ArgumentError, 'Must pass :period option' unless options[:period]

  @limit = options[:limit]
  @period = options[:period].to_i
  @algorithm = options[:algorithm] || :fixed_window
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



6
7
8
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6

def algorithm
  @algorithm
end

#blockObject (readonly)

Returns the value of attribute block.



6
7
8
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6

def block
  @block
end

#limitObject (readonly)

Returns the value of attribute limit.



6
7
8
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6

def name
  @name
end

#periodObject (readonly)

Returns the value of attribute period.



6
7
8
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 6

def period
  @period
end

Instance Method Details

#matched_by?(request) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/throttle_machines/rack_middleware/throttle.rb', line 20

def matched_by?(request)
  discriminator = discriminator_for(request)

  return false unless discriminator

  key = "#{@name}:#{discriminator}"
  current_limit = limit_for(request)
  current_period = period_for(request)

  # Use ThrottleMachines limiter
  limiter = ThrottleMachines.limiter(
    key,
    limit: current_limit,
    period: current_period,
    algorithm: @algorithm
  )

  # Try to consume the request atomically
  throttled = false

  begin
    # This will either succeed and consume a request, or raise ThrottledError
    limiter.throttle!

    # If we get here, request was allowed
  rescue ThrottledError
    # Request was throttled
    throttled = true
  end

  # Get current state for instrumentation
  data = {
    discriminator: discriminator,
    count: current_limit - limiter.remaining,
    period: current_period,
    limit: current_limit,
    retry_after: limiter.retry_after
  }

  annotate_request_with_throttle_data(request, data)

  if throttled
    annotate_request_with_matched_data(request, data)
    ThrottleMachines::RackMiddleware.instrument(request)
  end

  throttled
end