Class: Parse::Agent::MCPSubscriptions::LocalNotifier
- Inherits:
-
Object
- Object
- Parse::Agent::MCPSubscriptions::LocalNotifier
- Defined in:
- lib/parse/agent/mcp_subscriptions.rb
Overview
In-process Notifier. Routes a published notification straight to the listening-stream callback registered under the same session id. This is the single-process implementation; the contract it satisfies is the seam a clustered (e.g. Redis pub/sub) implementation slots into.
Contract (any Notifier must honor):
register(session_id) { |notification_hash| ... }— install the delivery callback for a listening stream. Replacing an existing registration is allowed (last writer wins).unregister(session_id)— remove it. Idempotent.publish(session_id, notification_hash)— deliver to the registered callback if one exists; a no-op (dropped) when no listener is attached. Returns whether a listener received it.
Thread-safety: publish may run on a LiveQuery dispatcher thread while
register/unregister run on Rack I/O threads, so all three guard the
listener table with a mutex. The delivery callback itself is invoked
outside the lock so a slow consumer can't block registry mutation.
The Manager calls register and unregister while holding its own
lock, so attach and detach stay atomic with its session table. An
implementation must therefore return promptly and must not call back
into the Manager (that would raise a recursive-lock ThreadError). A
clustered notifier should hand network I/O (subscribing a pub/sub
channel) to a background thread rather than doing it inline.
Instance Method Summary collapse
-
#initialize ⇒ LocalNotifier
constructor
A new instance of LocalNotifier.
-
#listener?(session_id) ⇒ Boolean
Whether a listening stream is attached.
-
#publish(session_id, notification_hash) ⇒ Boolean
True if a listener received the notification.
- #register(session_id) {|notification_hash| ... } ⇒ Object
- #unregister(session_id) ⇒ Object
Constructor Details
#initialize ⇒ LocalNotifier
Returns a new instance of LocalNotifier.
228 229 230 231 |
# File 'lib/parse/agent/mcp_subscriptions.rb', line 228 def initialize @listeners = {} @mutex = Mutex.new end |
Instance Method Details
#listener?(session_id) ⇒ Boolean
Returns whether a listening stream is attached.
253 254 255 |
# File 'lib/parse/agent/mcp_subscriptions.rb', line 253 def listener?(session_id) @mutex.synchronize { @listeners.key?(session_id) } end |
#publish(session_id, notification_hash) ⇒ Boolean
Returns true if a listener received the notification.
245 246 247 248 249 250 |
# File 'lib/parse/agent/mcp_subscriptions.rb', line 245 def publish(session_id, notification_hash) callback = @mutex.synchronize { @listeners[session_id] } return false unless callback callback.call(notification_hash) true end |
#register(session_id) {|notification_hash| ... } ⇒ Object
234 235 236 237 |
# File 'lib/parse/agent/mcp_subscriptions.rb', line 234 def register(session_id, &callback) return if session_id.nil? || callback.nil? @mutex.synchronize { @listeners[session_id] = callback } end |
#unregister(session_id) ⇒ Object
239 240 241 242 |
# File 'lib/parse/agent/mcp_subscriptions.rb', line 239 def unregister(session_id) return if session_id.nil? @mutex.synchronize { @listeners.delete(session_id) } end |