Class: IO::Endpoint::UNIXEndpoint

Inherits:
AddressEndpoint show all
Defined in:
lib/io/endpoint/unix_endpoint.rb

Overview

This class doesn’t exert ownership over the specified unix socket and ensures exclusive access by using ‘flock` where possible.

Instance Attribute Summary collapse

Attributes inherited from AddressEndpoint

#The network address for this endpoint., #address

Attributes inherited from Generic

#options

Instance Method Summary collapse

Methods inherited from AddressEndpoint

#connect

Methods inherited from Generic

#accept, #bound, #connect, #connected, #each, #hostname, #linger, #local_address, parse, #reuse_address?, #reuse_port?, #timeout, #with, #wrapper

Constructor Details

#initialize(path, type = Socket::SOCK_STREAM, **options) ⇒ UNIXEndpoint

Initialize a new UNIX domain socket endpoint.



15
16
17
18
19
20
# File 'lib/io/endpoint/unix_endpoint.rb', line 15

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



35
36
37
# File 'lib/io/endpoint/unix_endpoint.rb', line 35

def path
  @path
end

#The path to the UNIX socket.(pathtotheUNIXsocket.) ⇒ Object (readonly)



35
# File 'lib/io/endpoint/unix_endpoint.rb', line 35

attr :path

Instance Method Details

#bindObject

Bind the UNIX socket, handling stale socket files.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/io/endpoint/unix_endpoint.rb', line 54

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

#bound?Boolean

Check if the socket is currently bound and accepting connections.

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
# File 'lib/io/endpoint/unix_endpoint.rb', line 39

def bound?
  self.connect do
    return true
  end
rescue Errno::ECONNREFUSED
  return false
rescue Errno::ENOENT
  return false
end

#inspectObject

Get a detailed string representation of the UNIX endpoint.



30
31
32
# File 'lib/io/endpoint/unix_endpoint.rb', line 30

def inspect
  "\#<#{self.class} path=#{@path.inspect}>"
end

#to_sObject

Get a string representation of the UNIX endpoint.



24
25
26
# File 'lib/io/endpoint/unix_endpoint.rb', line 24

def to_s
  "unix:#{@path}"
end