Class: Aikido::Zen::RateLimiter::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/rate_limiter/result.rb

Overview

Holds the stats after checking if a request should be rate limited, which will be added to the Rack env.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(throttled:, discriminator:, current_requests:, max_requests:, time_remaining:) ⇒ Result

Returns a new instance of Result.



18
19
20
21
22
23
24
# File 'lib/aikido/zen/rate_limiter/result.rb', line 18

def initialize(throttled:, discriminator:, current_requests:, max_requests:, time_remaining:)
  @throttled = throttled
  @discriminator = discriminator
  @current_requests = current_requests
  @max_requests = max_requests
  @time_remaining = time_remaining
end

Instance Attribute Details

#current_requestsInteger (readonly)

Returns number of requests for the client in the current window.

Returns:

  • (Integer)

    number of requests for the client in the current window.



10
11
12
# File 'lib/aikido/zen/rate_limiter/result.rb', line 10

def current_requests
  @current_requests
end

#discriminatorString (readonly)

Returns the output of the configured discriminator block, used to uniquely identify a client (e.g. the remote IP).

Returns:

  • (String)

    the output of the configured discriminator block, used to uniquely identify a client (e.g. the remote IP).



7
8
9
# File 'lib/aikido/zen/rate_limiter/result.rb', line 7

def discriminator
  @discriminator
end

#max_requestsInteger (readonly)

Returns configured max number of requests per client.

Returns:

  • (Integer)

    configured max number of requests per client.



13
14
15
# File 'lib/aikido/zen/rate_limiter/result.rb', line 13

def max_requests
  @max_requests
end

#time_remainingInteger (readonly)

Returns number of seconds remaining until the window resets.

Returns:

  • (Integer)

    number of seconds remaining until the window resets.



16
17
18
# File 'lib/aikido/zen/rate_limiter/result.rb', line 16

def time_remaining
  @time_remaining
end

Class Method Details

.from_json(data) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/aikido/zen/rate_limiter/result.rb', line 41

def self.from_json(data)
  new(
    throttled: data["throttled"],
    discriminator: data["discriminator"],
    current_requests: data["current_requests"],
    max_requests: data["max_requests"],
    time_remaining: data["time_remaining"]
  )
end

Instance Method Details

#as_jsonObject



31
32
33
34
35
36
37
38
39
# File 'lib/aikido/zen/rate_limiter/result.rb', line 31

def as_json
  {
    "throttled" => @throttled,
    "discriminator" => @discriminator,
    "current_requests" => @current_requests,
    "max_requests" => @max_requests,
    "time_remaining" => @time_remaining
  }
end

#throttled?Boolean

Returns whether the current request was throttled or not.

Returns:

  • (Boolean)

    whether the current request was throttled or not.



27
28
29
# File 'lib/aikido/zen/rate_limiter/result.rb', line 27

def throttled?
  @throttled
end