Module: NNQ::Transport::IPC

Defined in:
lib/nnq/transport/ipc.rb

Overview

IPC transport using Unix domain sockets.

Supports both file-based paths and Linux abstract namespace (paths starting with @). Wire format is identical to TCP: SP/TCP greeting followed by framed messages, so Protocol::SP handles it verbatim.

Defined Under Namespace

Classes: Listener

Class Method Summary collapse

Class Method Details

.abstract?(path) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/nnq/transport/ipc.rb', line 57

def abstract?(path)
  path.start_with?("@")
end

.bind(endpoint, engine) ⇒ Listener

Binds an IPC server.

Parameters:

  • endpoint (String)

    e.g. “ipc:///tmp/nnq.sock” or “ipc://@abstract”

  • engine (Engine)

Returns:



22
23
24
25
26
27
28
29
30
# File 'lib/nnq/transport/ipc.rb', line 22

def bind(endpoint, engine)
  path      = parse_path(endpoint)
  sock_path = to_socket_path(path)

  File.delete(sock_path) if !abstract?(path) && File.exist?(sock_path)

  server = UNIXServer.new(sock_path)
  Listener.new(endpoint, server, path, engine)
end

.connect(endpoint, engine) ⇒ void

This method returns an undefined value.

Connects to an IPC endpoint.

Parameters:

  • endpoint (String)
  • engine (Engine)


38
39
40
41
42
43
# File 'lib/nnq/transport/ipc.rb', line 38

def connect(endpoint, engine)
  path      = parse_path(endpoint)
  sock_path = to_socket_path(path)
  sock      = UNIXSocket.new(sock_path)
  engine.handle_connected(IO::Stream::Buffered.wrap(sock), endpoint: endpoint, framing: :ipc)
end

.parse_path(endpoint) ⇒ Object



46
47
48
# File 'lib/nnq/transport/ipc.rb', line 46

def parse_path(endpoint)
  endpoint.sub(%r{\Aipc://}, "")
end

.to_socket_path(path) ⇒ Object

Converts @ prefix to 0 for Linux abstract namespace.



52
53
54
# File 'lib/nnq/transport/ipc.rb', line 52

def to_socket_path(path)
  abstract?(path) ? "\0#{path[1..]}" : path
end