Class: Parse::Agent::MCPRackApp::ListeningStreamBody Private
- Inherits:
-
Object
- Object
- Parse::Agent::MCPRackApp::ListeningStreamBody
- Defined in:
- lib/parse/agent/mcp_rack_app.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.
Rack body for the long-lived GET listening stream that carries
notifications/resources/updated to a subscribing client.
On #each it registers a delivery callback with the Parse::Agent::MCPSubscriptions::Manager keyed by the session id, then blocks reading from an internal queue and yields SSE-formatted notification events as they are published by the LiveQuery bridge. A periodic SSE comment heartbeat keeps the connection warm and surfaces a dead socket as a write error so the Rack server invokes #close.
#close detaches the listener and tears down every LiveQuery
subscription bound to the session — so a dropped stream leaves no
LiveQuery sockets behind. Re-opening the stream requires the client to
re-issue its resources/subscribe calls (subscriptions do not survive a
listening-stream disconnect in this single-process implementation).
The publish callback runs on a LiveQuery dispatcher / debounce thread
and only pushes to the thread-safe queue; all yields happen on the
Rack I/O thread driving #each, mirroring SSEBody's threading model.
Constant Summary collapse
- DONE =
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.
:__listening_done__
Instance Method Summary collapse
-
#close ⇒ Object
private
Terminate the stream: stop heartbeats, detach the listener, and tear down the session's LiveQuery subscriptions.
-
#each {|String| ... } ⇒ Object
private
Rack body interface — called once by the Rack server.
-
#initialize(manager, session_id, heartbeat_interval, logger, revalidate: nil, revalidate_interval: nil) ⇒ ListeningStreamBody
constructor
private
A new instance of ListeningStreamBody.
Constructor Details
#initialize(manager, session_id, heartbeat_interval, logger, revalidate: nil, revalidate_interval: nil) ⇒ ListeningStreamBody
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.
Returns a new instance of ListeningStreamBody.
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 |
# File 'lib/parse/agent/mcp_rack_app.rb', line 1981 def initialize(manager, session_id, heartbeat_interval, logger, revalidate: nil, revalidate_interval: nil) @manager = manager @session_id = session_id @heartbeat_interval = heartbeat_interval @logger = logger @revalidate = revalidate @revalidate_interval = revalidate_interval @revalidator_thread = nil @queue = Queue.new @heartbeat = nil @closed = false @counted = false @close_mutex = Mutex.new @close_signal = ConditionVariable.new end |
Instance Method Details
#close ⇒ Object
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.
Terminate the stream: stop heartbeats, detach the listener, and tear down the session's LiveQuery subscriptions. Idempotent.
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 |
# File 'lib/parse/agent/mcp_rack_app.rb', line 2041 def close counted = @close_mutex.synchronize do return if @closed @closed = true # Wake the revalidator so it exits at once instead of after its # next interval. @close_signal.broadcast @counted end # Balance the #each increment exactly once (close is idempotent via # @closed, and #each counts only under the same lock). MCPRackApp.adjust_listening_stream_count(-1) if counted @heartbeat&.kill @heartbeat = nil # The revalidator is not killed: it may be inside a REST call, and # killing it there can return a pooled connection mid-response. It # was woken above and exits after any check in progress. @revalidator_thread = nil begin # Only a stream that attached detaches: a body closed before # Rack iterated it (or before its attach) never registered a # listener, and detaching would tear down the session's active # stream. Pass this stream's own callback so a reconnect that # already attached a newer stream is not torn down either. # Custom managers with a one-argument detach_listener still work. listener = @close_mutex.synchronize { @listener } if listener.nil? nil elsif @manager.method(:detach_listener).arity != 1 @manager.detach_listener(@session_id, listener) else @manager.detach_listener(@session_id) end rescue StandardError => e line = "[Parse::Agent::MCPRackApp::ListeningStreamBody] detach error: #{e.class}: #{e.}" @logger ? @logger.warn(line) : warn(line) end @queue << DONE rescue nil end |
#each {|String| ... } ⇒ Object
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.
Rack body interface — called once by the Rack server.
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 |
# File 'lib/parse/agent/mcp_rack_app.rb', line 2000 def each queue = @queue # Count this stream against the concurrent-listening-stream soft cap. # Incrementing here (in #each, not the constructor) means a body the # Rack server never iterates — or a client that disconnects before # iteration — never inflates the counter; the matching decrement is in # #close, which #each's `ensure` always runs. # Increment and record it under the close lock, so a close racing # this cannot skip the matching decrement and leak a stream slot. @close_mutex.synchronize do return if @closed MCPRackApp.adjust_listening_stream_count(1) @counted = true end listener = lambda do |notification| queue << format_event(notification) end # Attach under the close lock so a close that races this attach # either runs first (nothing is attached) or sees @listener set # and detaches it. Without the lock a close landing just before # the attach would leave the listener registered with no stream. @close_mutex.synchronize do return if @closed @manager.attach_listener(@session_id, &listener) @listener = listener end # Initial comment flushes response headers and confirms the stream. yield ": connected\n\n" start_heartbeat start_revalidation loop do msg = @queue.pop break if msg == DONE yield msg end ensure close end |