Class: MonoVM::Whois::Transport::Middleware::Throttle

Inherits:
Middleware::Base
  • Object
show all
Defined in:
lib/monovm/whois/transport/middleware/throttle.rb

Overview

Keeps a minimum gap between consecutive queries to the same host.

This is the politeness layer, and it is what makes concurrent bulk checks safe. Registries publish query-rate limits and enforce them by blocking clients; without a throttle, pointing eight threads at a list of .com names sends eight simultaneous queries to one Verisign host and gets the caller's IP throttled — after which every response is a refusal and no domain can be checked at all.

The gap is per host, not global, so a batch spanning many TLDs still runs in parallel across them.

Constant Summary collapse

DEFAULT_INTERVAL =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, interval: DEFAULT_INTERVAL, clock: nil, sleeper: nil) ⇒ Throttle

Returns a new instance of Throttle.

Parameters:

  • (defaults to: DEFAULT_INTERVAL)

    minimum seconds between queries to one host

  • (defaults to: nil)

    monotonic seconds, injectable for specs

  • (defaults to: nil)

    receives seconds to wait, injectable for specs



28
29
30
31
32
33
34
35
# File 'lib/monovm/whois/transport/middleware/throttle.rb', line 28

def initialize(app, interval: DEFAULT_INTERVAL, clock: nil, sleeper: nil)
  @interval = interval
  @clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
  @sleeper = sleeper || ->(seconds) { sleep(seconds) }
  @last_seen = {}
  @mutex = Mutex.new
  super(app)
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



23
24
25
# File 'lib/monovm/whois/transport/middleware/throttle.rb', line 23

def interval
  @interval
end

Instance Method Details

#fetch(query:, endpoint:) ⇒ Object



37
38
39
40
# File 'lib/monovm/whois/transport/middleware/throttle.rb', line 37

def fetch(query:, endpoint:)
  wait_for(endpoint.host)
  app.fetch(query: query, endpoint: endpoint)
end