Class: ReactorSDK::RateLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor_sdk/rate_limiter.rb

Constant Summary collapse

MAX_REQUESTS_PER_MINUTE =

Adobe’s documented rate limit per access token

120
INTERVAL_SECONDS =

The window over which the limit applies

60.0

Instance Method Summary collapse

Constructor Details

#initializeRateLimiter

Initializes a full token bucket. The first MAX_REQUESTS_PER_MINUTE calls proceed without any delay.



30
31
32
33
34
# File 'lib/reactor_sdk/rate_limiter.rb', line 30

def initialize
  @mutex     = Mutex.new
  @tokens    = MAX_REQUESTS_PER_MINUTE
  @last_tick = Time.now.utc
end

Instance Method Details

#acquirevoid

This method returns an undefined value.

Acquires one token from the bucket, blocking if the bucket is empty. Refills the bucket based on elapsed time before checking availability.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/reactor_sdk/rate_limiter.rb', line 42

def acquire
  @mutex.synchronize do
    refill
    if @tokens >= 1
      @tokens -= 1
    else
      sleep(seconds_per_token)
      refill
      @tokens -= 1
    end
  end
end

#available_tokensInteger

Returns the number of tokens currently available. Useful for monitoring and testing — not used internally by acquire.

Returns:

  • (Integer)

    Current token count



61
62
63
64
65
66
# File 'lib/reactor_sdk/rate_limiter.rb', line 61

def available_tokens
  @mutex.synchronize do
    refill
    @tokens.floor
  end
end