class Uninterruptible::Binder
Attributes
Public Class Methods
@param [String] bind_address The config for a server we're returning the socket for
@example "unix:///tmp/server.sock" "tcp://127.0.0.1:8080"
# File lib/uninterruptible/binder.rb, line 11 def initialize(bind_address) @bind_uri = parse_bind_address(bind_address) end
Public Instance Methods
Bind to the TCP or UNIX socket defined in the bind_uri
@return [TCPServer, UNIXServer] Successfully bound server
@raise [Uninterruptible::ConfigurationError] Raised when the URI indicates a non-tcp or unix scheme
# File lib/uninterruptible/binder.rb, line 20 def bind_to_socket case bind_uri.scheme when 'tcp' bind_to_tcp_socket when 'unix' bind_to_unix_socket else raise Uninterruptible::ConfigurationError, "Can only bind to TCP and UNIX sockets" end end
Private Instance Methods
Open a connection to a running FileDesciptorServer on the parent of this server and obtain a file descriptor for the running socket server on there.
@param socket_klass [#for_fd] Class which responds to for_fd and accepts a file descriptor
@return [IO] An IO instance created from the file descriptor received from the file descriptor server
# File lib/uninterruptible/binder.rb, line 77 def bind_from_file_descriptor_server(socket_klass) fds_socket = UNIXSocket.new(ENV[FILE_DESCRIPTOR_SERVER_VAR]) socket_file_descriptor = fds_socket.recv_io(socket_klass) fds_socket.close socket_file_descriptor end
Connect (or reconnect if the FD is set) to a TCP server
@return [TCPServer] Socket server for the configured address and port
# File lib/uninterruptible/binder.rb, line 36 def bind_to_tcp_socket if ENV[FILE_DESCRIPTOR_SERVER_VAR] bind_from_file_descriptor_server(TCPServer) else TCPServer.new(bind_uri.host, bind_uri.port) end end
Connect (or reconnect if FD is set) to a UNIX socket. Will delete existing socket at path if required.
@return [UNIXServer] Socket server for the configured path
# File lib/uninterruptible/binder.rb, line 47 def bind_to_unix_socket if ENV[FILE_DESCRIPTOR_SERVER_VAR] bind_from_file_descriptor_server(UNIXServer) else File.delete(bind_uri.path) if File.exist?(bind_uri.path) UNIXServer.new(bind_uri.path) end end
Parse the bind address in the configuration
@param [String] bind_address The config for a server we're returning the socket for
@return [URI::Generic] Parsed version of the bind_address
@raise [Uninterruptible::ConfigurationError] Raised if the bind_address could not be parsed
# File lib/uninterruptible/binder.rb, line 65 def parse_bind_address(bind_address) URI.parse(bind_address) rescue URI::Error raise Uninterruptible::ConfigurationError, "Couldn't parse the bind address: \"#{bind_address}\"" end