Class: Raptor::Binder

Inherits:
Object
  • Object
show all
Defined in:
lib/raptor/binder.rb

Overview

Manages binding to network addresses and creating listening sockets.

Binder handles parsing URI bind specifications, creating TCP, Unix, and SSL server sockets, and managing socket options for optimal performance. It supports binding to multiple addresses simultaneously.

Examples:

TCP binding

binder = Binder.new(["tcp://0.0.0.0:3000", "tcp://[::1]:3000"])
puts binder.addresses #=> ["0.0.0.0:3000", "[::1]:3000"]
binder.close

Unix socket binding

binder = Binder.new(["unix:///tmp/raptor.sock"])
puts binder.addresses #=> ["/tmp/raptor.sock"]
binder.close

SSL binding

binder = Binder.new(["ssl://0.0.0.0:443?cert=/path/to.crt&key=/path/to.key"])
puts binder.addresses #=> ["ssl://0.0.0.0:443"]
binder.close

Localhost binding

binder = Binder.new(["tcp://localhost:8080"])
# Binds to both IPv4 and IPv6 loopback addresses

Defined Under Namespace

Classes: SslListener, UnknownBindSchemeError

Constant Summary collapse

SOCKET_BACKLOG =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bind_uris) ⇒ void

Creates a new Binder with the specified bind URIs.

Parses the provided bind URIs and creates listening sockets for each one. Supports tcp://, unix://, and ssl:// schemes. Localhost is expanded to all available loopback addresses (both IPv4 and IPv6).

Examples:

binder = Binder.new(["tcp://0.0.0.0:3000", "unix:///tmp/raptor.sock"])

Parameters:

  • bind_uris (Array<String>)

    array of URI strings to bind to

Raises:



80
81
82
83
84
# File 'lib/raptor/binder.rb', line 80

def initialize(bind_uris)
  @bind_uris = bind_uris
  @listeners = nil
  parse
end

Instance Attribute Details

#listenersArray<TCPServer, UNIXServer, SslListener> (readonly)

Array of listening sockets.

Returns:

  • (Array<TCPServer, UNIXServer, SslListener>)

    the server sockets



64
65
66
# File 'lib/raptor/binder.rb', line 64

def listeners
  @listeners
end

Instance Method Details

#addressesArray<String>

Returns the bound addresses as strings.

TCP listeners are returned as “host:port”, Unix listeners as the socket path, and SSL listeners as “ssl://host:port”.

Examples:

binder.addresses #=> ["127.0.0.1:3000", "/tmp/raptor.sock", "ssl://0.0.0.0:443"]

Returns:

  • (Array<String>)

    address strings for each bound listener



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/raptor/binder.rb', line 97

def addresses
  @listeners.map do |listener|
    case listener
    when UNIXServer
      listener.path
    when SslListener
      address = listener.local_address
      "ssl://#{address.ip_address}:#{address.ip_port}"
    else
      address = listener.local_address
      "#{address.ip_address}:#{address.ip_port}"
    end
  end
end

#closevoid

This method returns an undefined value.

Closes all listening sockets.



132
133
134
# File 'lib/raptor/binder.rb', line 132

def close
  @listeners.each(&:close)
end

#server_portInteger

Returns the port number of the first TCP or SSL listener.

Used to populate SERVER_PORT in the Rack environment. Returns 0 if no TCP or SSL listener is configured (e.g., Unix socket only).

Returns:

  • (Integer)

    the port number, or 0 if no TCP listener exists



120
121
122
123
124
125
# File 'lib/raptor/binder.rb', line 120

def server_port
  tcp_listener = @listeners.find { |listener| listener.is_a?(TCPServer) || listener.is_a?(SslListener) }
  return 0 unless tcp_listener

  tcp_listener.local_address.ip_port
end