class UNIXEndpoint
This class doesn't exert ownership over the specified unix socket and ensures exclusive access by using flock where possible.
Definitions
def initialize(path, type = Socket::SOCK_STREAM, **options)
Initialize a new UNIX domain socket endpoint.
Signature
-
parameter
pathString The path to the UNIX socket.
-
parameter
typeInteger The socket type (defaults to Socket::SOCK_STREAM).
-
parameter
optionsHash Additional options to pass to the parent class.
Implementation
def initialize(path, type = Socket::SOCK_STREAM, **options)
# I wonder if we should implement chdir behaviour in here if path is longer than 104 characters.
super(Address.unix(path, type), **options)
@path = path
end
def to_s
Get a string representation of the UNIX endpoint.
Signature
-
returns
String A string representation showing the socket path.
Implementation
def to_s
"unix:#{@path}"
end
def inspect
Get a detailed string representation of the UNIX endpoint.
Signature
-
returns
String A detailed string representation including the path.
Implementation
def inspect
"\#<#{self.class} path=#{@path.inspect}>"
end
attr :path
Signature
-
attribute
String The path to the UNIX socket.
def bound?
Check if the socket is currently bound and accepting connections.
Signature
-
returns
Boolean True if the socket is bound and accepting connections, false otherwise.
Implementation
def bound?
self.connect do
return true
end
rescue Errno::ECONNREFUSED
return false
rescue Errno::ENOENT
return false
end
def bind(...)
Bind the UNIX socket, handling stale socket files.
Signature
-
yields
{|socket| ...} If a block is given, yields the bound socket.
-
parameter
socketSocket The bound socket.
-
parameter
-
returns
Array(Socket) The bound socket.
-
raises
Errno::EADDRINUSE If the socket is still in use by another process.
Implementation
def bind(...)
super
rescue Errno::EADDRINUSE
# If you encounter EADDRINUSE from `bind()`, you can check if the socket is actually accepting connections by attempting to `connect()` to it. If the socket is still bound by an active process, the connection will succeed. Otherwise, it should be safe to `unlink()` the path and try again.
if !bound?
File.unlink(@path) rescue nil
retry
else
raise
end
end