Class: MonoVM::Whois::Transport::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/monovm/whois/transport/factory.rb

Overview

Chooses a transport for an endpoint.

Client asks for "something that can fetch this endpoint" and gets it, so adding a protocol means registering a transport here and nowhere else. The factory also owns the middleware wrapping, which is why every transport it hands out already caches, throttles and retries consistently — those are decisions about the whole system, not about one protocol.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transports: nil, middleware: []) ⇒ Factory



24
25
26
27
28
29
# File 'lib/monovm/whois/transport/factory.rb', line 24

def initialize(transports: nil, middleware: [])
  @transports = transports || [WhoisSocket.new, RdapHttp.new]
  @middleware = middleware
  @wrapped = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#middlewareObject (readonly)

Returns the value of attribute middleware.



18
19
20
# File 'lib/monovm/whois/transport/factory.rb', line 18

def middleware
  @middleware
end

#transportsObject (readonly)

Returns the value of attribute transports.



18
19
20
# File 'lib/monovm/whois/transport/factory.rb', line 18

def transports
  @transports
end

Instance Method Details

#for(endpoint) ⇒ Base

Returns a transport that can serve endpoint, already wrapped.

Raises:

  • (Error)

    when nothing registered can serve it



34
35
36
37
38
39
40
41
# File 'lib/monovm/whois/transport/factory.rb', line 34

def for(endpoint)
  transport = transports.find { |candidate| candidate.supports?(endpoint) }
  raise Error, "no transport can serve #{endpoint}" if transport.nil?

  # Middleware holds per-host state (throttle clocks, cache entries), so each
  # transport must be wrapped exactly once and shared, not re-wrapped per call.
  @wrapped[transport] || @mutex.synchronize { @wrapped[transport] ||= wrap(transport) }
end