Class: Archaeo::RateLimiter

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

Overview

Thread-safe request rate limiter.

Enforces a minimum interval between requests to avoid hitting Wayback Machine rate limits. Supports per-host limiting and adaptive backoff when 429 responses are received.

Constant Summary collapse

DEFAULT_MIN_INTERVAL =
0

Instance Method Summary collapse

Constructor Details

#initialize(min_interval: DEFAULT_MIN_INTERVAL) ⇒ RateLimiter

Returns a new instance of RateLimiter.



12
13
14
15
16
17
# File 'lib/archaeo/rate_limiter.rb', line 12

def initialize(min_interval: DEFAULT_MIN_INTERVAL)
  @min_interval = min_interval.to_f
  @mutex = Mutex.new
  @last_request_at = 0.0
  @host_last = {}
end

Instance Method Details

#backoff(host: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/archaeo/rate_limiter.rb', line 31

def backoff(host: nil)
  @mutex.synchronize do
    if host
      key = host.to_sym
      current = @host_last[key] || @min_interval
      @host_last[key] = [current * 2, 60].min
    else
      @min_interval = [(@min_interval * 2).clamp(0, 60), 60].min
    end
  end
  wait(host: host)
end

#intervalObject



55
56
57
# File 'lib/archaeo/rate_limiter.rb', line 55

def interval
  @mutex.synchronize { @min_interval }
end

#reset(host: nil) ⇒ Object



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

def reset(host: nil)
  @mutex.synchronize do
    if host
      @host_last.delete(host.to_sym)
    else
      @last_request_at = 0.0
      @host_last.clear
    end
  end
end

#wait(host: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/archaeo/rate_limiter.rb', line 19

def wait(host: nil)
  return if @min_interval <= 0

  @mutex.synchronize do
    if host
      wait_for_host(host)
    else
      wait_global
    end
  end
end