class Driver
Represents an instance of a locally running driver (usually with a process group).
Definitions
def initialize(**options)
Initialize a driver wrapper.
Signature
-
parameter
optionsHash Driver configuration options.
Implementation
def initialize(**options)
@options = options
@count = 0
@closed = false
end
def concurrency
Signature
-
returns
Integer The number of concurrent sessions the driver can sustain.
Implementation
def concurrency
@options.fetch(:concurrency, 128)
end
attr :status
Signature
-
attribute
Hash The status of the driver after a connection has been established.
def viable?
Signature
-
returns
Boolean Whether the driver can still be used.
Implementation
def viable?
!@closed
end
def closed?
Signature
-
returns
Boolean Whether the driver has been closed.
Implementation
def closed?
@closed
end
def close
Mark the driver as closed.
Implementation
def close
@closed = true
end
def reusable?
Signature
-
returns
Boolean Whether the driver may be returned to a pool.
Implementation
def reusable?
@options.fetch(:reusable, !@closed)
end
def ephemeral_port
Generate a port number for the driver to listen on if it was not specified.
Signature
-
returns
Integer An ephemeral port number.
Implementation
def ephemeral_port
address = ::Addrinfo.tcp("localhost", 0)
address.bind do |socket|
# We assume that it's unlikely the port will be reused any time soon...
return socket.local_address.ip_port
end
end
def port
Signature
-
returns
Integer The port the driver listens on.
Implementation
def port
@port ||= @options.fetch(:port, self.ephemeral_port)
end
def endpoint
Signature
-
returns
Async::HTTP::Endpoint The HTTP endpoint exposed by the driver.
Implementation
def endpoint
Async::HTTP::Endpoint.parse("http://localhost", port: self.port)
end
def client
Signature
-
returns
Client A client connected to the driver endpoint.
Implementation
def client
Client.open(self.endpoint)
end
def start(retries: 100)
Start the driver.
Signature
-
parameter
retriesInteger The number of times to retry before giving up.
Implementation
def start(retries: 100)
endpoint = self.endpoint
Console.debug(self, "Waiting for driver to start...", endpoint: endpoint)
count = 0
Async::HTTP::Client.open(endpoint) do |client|
begin
response = client.get("/status")
@status = JSON.parse(response.read)["value"]
Console.debug(self, "Successfully connected to driver.", status: @status)
rescue Errno::ECONNREFUSED
if count < retries
count += 1
sleep(0.01 * count)
Console.debug(self, "Driver not ready, retrying...")
retry
else
raise
end
end
end
end