Class: Lich::InternalAPI::ActiveSessions::Server
- Inherits:
-
Object
- Object
- Lich::InternalAPI::ActiveSessions::Server
- Defined in:
- documented/internal_api/active_sessions/server.rb
Overview
Read-only/query plus lifecycle write server for the active sessions API.
The transport is intentionally local-only TCP to keep behavior consistent across Linux, macOS, and Windows. The server delegates all state changes to Registry; it does not own lifecycle policy beyond request routing and thread cleanup.
Constant Summary collapse
- READ_TIMEOUT =
Maximum number of seconds to wait for the first request line from a connected client before abandoning the handler.
1
Instance Attribute Summary collapse
-
#auth_token ⇒ Object
readonly
Returns the value of attribute auth_token.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #initialize(host:, port:, registry:, auth_token:, server_factory: nil, accept_thread_factory: nil, client_thread_factory: nil) ⇒ void constructor
-
#running? ⇒ Boolean
Indicates whether the server thread is active.
-
#start ⇒ Boolean
Starts the TCP server and accept loop.
-
#stop ⇒ void
Stops the server and its accept thread.
Constructor Details
#initialize(host:, port:, registry:, auth_token:, server_factory: nil, accept_thread_factory: nil, client_thread_factory: nil) ⇒ void
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'documented/internal_api/active_sessions/server.rb', line 42 def initialize(host:, port:, registry:, auth_token:, server_factory: nil, accept_thread_factory: nil, client_thread_factory: nil) @host = host @port = port @registry = registry @auth_token = auth_token @server_factory = server_factory || ->(bind_host, bind_port) { TCPServer.new(bind_host, bind_port) } @accept_thread_factory = accept_thread_factory || ->(&block) { Thread.new(&block) } @client_thread_factory = client_thread_factory || ->(socket, &block) { Thread.new(socket, &block) } @server = nil @thread = nil @mutex = Mutex.new @client_threads = [] @stopping = false @run_id = Object.new end |
Instance Attribute Details
#auth_token ⇒ Object (readonly)
Returns the value of attribute auth_token.
32 33 34 |
# File 'documented/internal_api/active_sessions/server.rb', line 32 def auth_token @auth_token end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
31 32 33 |
# File 'documented/internal_api/active_sessions/server.rb', line 31 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
31 32 33 |
# File 'documented/internal_api/active_sessions/server.rb', line 31 def port @port end |
Instance Method Details
#running? ⇒ Boolean
Indicates whether the server thread is active.
117 118 119 |
# File 'documented/internal_api/active_sessions/server.rb', line 117 def running? @thread&.alive? || false end |
#start ⇒ Boolean
Starts the TCP server and accept loop.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'documented/internal_api/active_sessions/server.rb', line 61 def start @mutex.synchronize do return true if running? @stopping = false @server = @server_factory.call(@host, @port) @server.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1) rescue nil @port = @server.addr[1] run_id = @run_id = Object.new @thread = @accept_thread_factory.call do Lich.log("info: ActiveSessions accept thread started pid=#{Process.pid} port=#{@port}") if defined?(Lich) && Lich.respond_to?(:log) accept_loop(run_id) end end true rescue StandardError stop false end |
#stop ⇒ void
This method returns an undefined value.
Stops the server and its accept thread.
Client handler threads are joined with a short timeout so shutdown does not leak long-lived handler threads when the owning process exits.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'documented/internal_api/active_sessions/server.rb', line 87 def stop thread = nil server = nil client_threads = [] @mutex.synchronize do thread = @thread server = @server client_threads = @client_threads.dup @client_threads.clear @stopping = true @thread = nil @server = nil end server&.close rescue nil if thread&.alive? thread.join(0.1) thread.kill if thread.alive? end client_threads.each do |client_thread| next unless client_thread.respond_to?(:join) client_thread.join(0.25) client_thread.kill if client_thread.respond_to?(:alive?) && client_thread.alive? end end |