Class: Raptor::Binder
- Inherits:
-
Object
- Object
- Raptor::Binder
- 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.
Defined Under Namespace
Classes: SslListener, UnknownBindSchemeError
Constant Summary collapse
- SOCKET_BACKLOG =
1024
Instance Attribute Summary collapse
-
#listeners ⇒ Array<TCPServer, UNIXServer, SslListener>
readonly
Array of listening sockets.
Instance Method Summary collapse
-
#addresses ⇒ Array<String>
Returns the bound addresses as strings.
-
#close ⇒ void
Closes all listening sockets.
-
#initialize(bind_uris) ⇒ void
constructor
Creates a new Binder with the specified bind URIs.
-
#server_port ⇒ Integer
Returns the port number of the first TCP or SSL listener.
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).
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
#listeners ⇒ Array<TCPServer, UNIXServer, SslListener> (readonly)
Array of listening sockets.
64 65 66 |
# File 'lib/raptor/binder.rb', line 64 def listeners @listeners end |
Instance Method Details
#addresses ⇒ Array<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”.
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 |
#close ⇒ void
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_port ⇒ Integer
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).
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 |