Class: Plushie::Bridge
- Inherits:
-
Object
- Object
- Plushie::Bridge
- Defined in:
- lib/plushie/bridge.rb
Overview
Renderer lifecycle manager.
Wraps a Connection with restart logic. When the renderer crashes, the Bridge reconnects with exponential backoff and notifies the Runtime via the event queue. The Runtime owns the resync flow: it re-sends settings, renders a fresh snapshot, and re-syncs subscriptions after a successful restart.
The Bridge pushes decoded events to the Runtime's event queue:
- +[:renderer_event, msg]+ for normal protocol messages
- +[:renderer_exited, reason]+ when the connection drops
- +[:renderer_restarted]+ after a successful reconnect
Constant Summary collapse
- SDK_LOG_LEVELS =
{ off: Logger::UNKNOWN, error: Logger::ERROR, warning: Logger::WARN, warn: Logger::WARN, info: Logger::INFO, debug: Logger::DEBUG, trace: Logger::DEBUG }.freeze
- BACKOFF_BASE_MS =
Exponential backoff parameters. Shared with the other host SDKs (Elixir, Rust, Gleam, Python, TypeScript) so renderer restart behavior is consistent across implementations.
100- BACKOFF_MAX_MS =
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 backoff delay in milliseconds.
5000- MAX_RETRIES =
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 retry attempts before giving up.
5- DEFAULT_HEARTBEAT_INTERVAL =
Default watchdog interval in seconds. Set to nil to disable.
30
Instance Attribute Summary collapse
-
#format ⇒ :msgpack, :json
readonly
Wire format.
-
#hello ⇒ Hash?
readonly
Hello response from the current connection.
Instance Method Summary collapse
-
#initialize(event_queue:, format: :msgpack, binary: nil, transport: :spawn, log_level: DEFAULT_LOG_LEVEL, token: nil, heartbeat_interval: DEFAULT_HEARTBEAT_INTERVAL) ⇒ Bridge
constructor
A new instance of Bridge.
-
#send_encoded(data) ⇒ Object
Send pre-encoded wire bytes to the renderer.
-
#send_register_effect_stub(kind, response) ⇒ Object
Register an effect stub with the renderer.
-
#send_unregister_effect_stub(kind) ⇒ Object
Remove a previously registered effect stub.
-
#start(settings: {}) ⇒ Object
Start the connection and perform handshake.
-
#stop ⇒ Object
Stop the connection and clean up.
Constructor Details
#initialize(event_queue:, format: :msgpack, binary: nil, transport: :spawn, log_level: DEFAULT_LOG_LEVEL, token: nil, heartbeat_interval: DEFAULT_HEARTBEAT_INTERVAL) ⇒ Bridge
Returns a new instance of Bridge.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/plushie/bridge.rb', line 61 def initialize(event_queue:, format: :msgpack, binary: nil, transport: :spawn, log_level: DEFAULT_LOG_LEVEL, token: nil, heartbeat_interval: DEFAULT_HEARTBEAT_INTERVAL) @event_queue = event_queue @format = format @binary = binary @transport = transport @log_level = renderer_log_level(log_level) @token = token @connection = nil @retry_count = 0 @settings = {} @heartbeat_interval = heartbeat_interval @heartbeat_timer = nil @forwarder_thread = nil @conn_queue = nil @logger = Logger.new($stderr, level: sdk_log_level(log_level), progname: "plushie") end |
Instance Attribute Details
#format ⇒ :msgpack, :json (readonly)
Returns wire format.
44 45 46 |
# File 'lib/plushie/bridge.rb', line 44 def format @format end |
#hello ⇒ Hash? (readonly)
Returns hello response from the current connection.
47 48 49 |
# File 'lib/plushie/bridge.rb', line 47 def hello @hello end |
Instance Method Details
#send_encoded(data) ⇒ Object
Send pre-encoded wire bytes to the renderer. Thread-safe.
91 92 93 |
# File 'lib/plushie/bridge.rb', line 91 def send_encoded(data) @connection&.send_encoded(data) end |
#send_register_effect_stub(kind, response) ⇒ Object
Register an effect stub with the renderer.
99 100 101 102 103 |
# File 'lib/plushie/bridge.rb', line 99 def send_register_effect_stub(kind, response) @connection&.send_encoded( Protocol::Encode.encode_register_effect_stub(kind, response, @format) ) end |
#send_unregister_effect_stub(kind) ⇒ Object
Remove a previously registered effect stub.
108 109 110 111 112 |
# File 'lib/plushie/bridge.rb', line 108 def send_unregister_effect_stub(kind) @connection&.send_encoded( Protocol::Encode.encode_unregister_effect_stub(kind, @format) ) end |
#start(settings: {}) ⇒ Object
Start the connection and perform handshake.
83 84 85 86 |
# File 'lib/plushie/bridge.rb', line 83 def start(settings: {}) @settings = settings connect! end |
#stop ⇒ Object
Stop the connection and clean up.
115 116 117 118 119 120 121 122 123 |
# File 'lib/plushie/bridge.rb', line 115 def stop cancel_heartbeat_timer stop_thread(@forwarder_thread, timeout: 1) @forwarder_thread = nil @conn_queue&.close @connection&.close @connection = nil @retry_count = 0 end |