Class: MonoVM::Whois::Transport::Middleware::Throttle
- Inherits:
-
Middleware::Base
- Object
- Middleware::Base
- MonoVM::Whois::Transport::Middleware::Throttle
- 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
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
Instance Method Summary collapse
- #fetch(query:, endpoint:) ⇒ Object
-
#initialize(app, interval: DEFAULT_INTERVAL, clock: nil, sleeper: nil) ⇒ Throttle
constructor
A new instance of Throttle.
Constructor Details
#initialize(app, interval: DEFAULT_INTERVAL, clock: nil, sleeper: nil) ⇒ Throttle
Returns a new instance of Throttle.
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
#interval ⇒ Object (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 |