Class: Async::IO::UNIXEndpoint

Inherits:
AddressEndpoint show all
Defined in:
lib/async/io/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

#address

Attributes inherited from Endpoint

#options

Instance Method Summary collapse

Methods inherited from AddressEndpoint

#connect

Methods inherited from Endpoint

#accept, #bound, composite, #connected, each, #each, #hostname, #linger, #local_address, parse, #reuse_address, #reuse_port, socket, ssl, tcp, #timeout, try_convert, udp, unix, #with

Constructor Details

#initialize(path, type, **options) ⇒ UNIXEndpoint

Returns a new instance of UNIXEndpoint.



13
14
15
16
17
18
# File 'lib/async/io/unix_endpoint.rb', line 13

def initialize(path, type, **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.



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

def path
  @path
end

Instance Method Details

#bind(&block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/async/io/unix_endpoint.rb', line 34

def bind(&block)
	Socket.bind(@address, **@options, &block)
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

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/async/io/unix_endpoint.rb', line 26

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

#to_sObject



20
21
22
# File 'lib/async/io/unix_endpoint.rb', line 20

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