Class: Lich::InternalAPI::ActiveSessions::Client Private
- Inherits:
-
Object
- Object
- Lich::InternalAPI::ActiveSessions::Client
- 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.
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.
1
Instance Method Summary collapse
- #initialize(host:, port:, auth_token:, socket_factory: nil) ⇒ void constructor private
-
#ping ⇒ Boolean
private
Sends a lightweight health probe to the service.
-
#remove(pid) ⇒ Hash
private
Removes a session by pid over the transport.
-
#request(command, payload = {}) ⇒ Hash
private
Sends a raw command payload to the active sessions service.
-
#snapshot ⇒ Hash
private
Requests the current active sessions snapshot.
-
#upsert(payload) ⇒ Hash
private
Registers or updates a session over the transport.
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.
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.
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.
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.
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. } 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.
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.
82 83 84 |
# File 'documented/internal_api/active_sessions/client.rb', line 82 def upsert(payload) request('upsert', payload) end |