Class: Lich::InternalAPI::ActiveSessions::Client Private

Inherits:
Object
  • Object
show all
Defined in:
documented/internal_api/active_sessions/client.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Thin JSON client for the local active sessions service.

The client intentionally knows nothing about lifecycle semantics. It is only responsible for packaging JSON commands, reading JSON responses, and normalizing failures into a predictable { ok: false, error: ... } shape for higher-level callers.

Constant Summary collapse

CONNECT_TIMEOUT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maximum number of seconds to wait while opening the loopback socket.

Returns:

1
READ_TIMEOUT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maximum number of seconds to wait for a server response before treating the request as failed.

Returns:

1

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, auth_token:, socket_factory: nil) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • host (String)
  • port (Integer)
  • auth_token (String)
  • socket_factory (#call) (defaults to: nil) —

    builds a connected client socket



41
42
43
44
45
46
47
48
# File 'documented/internal_api/active_sessions/client.rb', line 41

def initialize(host:, port:, auth_token:, socket_factory: nil)
  @host = host
  @port = port
  @auth_token = auth_token
  @socket_factory = socket_factory || lambda do |connect_host, connect_port|
    Socket.tcp(connect_host, connect_port, connect_timeout: CONNECT_TIMEOUT)
  end
end

Instance Method Details

#ping ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends a lightweight health probe to the service.

Returns:

  • (Boolean) —

    true when the service responds with ok: true



74
75
76
# File 'documented/internal_api/active_sessions/client.rb', line 74

def ping
  request('ping').fetch(:ok, false)
end

#remove(pid) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes a session by pid over the transport.

Parameters:

  • pid (Integer)

Returns:



90
91
92
# File 'documented/internal_api/active_sessions/client.rb', line 90

def remove(pid)
  request('remove', pid: pid)
end

#request(command, payload = {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends a raw command payload to the active sessions service.

Parameters:

  • command (String) —

    protocol command name

  • payload (Hash) (defaults to: {}) —

    request-specific payload

Returns:

  • (Hash) —

    parsed response payload or a normalized error hash



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'documented/internal_api/active_sessions/client.rb', line 55

def request(command, payload = {})
  socket = @socket_factory.call(@host, @port)
  socket.write(JSON.dump(command: command, auth: @auth_token, payload: payload) + "\n")
  raw = read_response(socket)
  return { ok: false, error: 'read timeout' } unless raw

  response = JSON.parse(raw.to_s, symbolize_names: true)
  return { ok: false, error: 'invalid response type' } unless response.is_a?(Hash)

  response
rescue StandardError => e
  { ok: false, error: e.message }
ensure
  socket&.close rescue nil
end

#snapshot ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Requests the current active sessions snapshot.

Returns:



97
98
99
# File 'documented/internal_api/active_sessions/client.rb', line 97

def snapshot
  request('snapshot')
end

#upsert(payload) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Registers or updates a session over the transport.

Parameters:

Returns:



82
83
84
# File 'documented/internal_api/active_sessions/client.rb', line 82

def upsert(payload)
  request('upsert', payload)
end