Class: Plushie::Bridge

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • heartbeat_interval (Numeric, nil) (defaults to: DEFAULT_HEARTBEAT_INTERVAL)

    max seconds between renderer messages before triggering a restart. nil disables the watchdog.



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.

Returns:

  • (:msgpack, :json)

    wire format



44
45
46
# File 'lib/plushie/bridge.rb', line 44

def format
  @format
end

#helloHash? (readonly)

Returns hello response from the current connection.

Returns:

  • (Hash, nil)

    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.

Parameters:

  • data (String)

    encoded message bytes



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.

Parameters:

  • kind (String)

    effect kind

  • response (Object)

    canned response



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.

Parameters:

  • kind (String)

    effect kind



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.

Parameters:

  • settings (Hash) (defaults to: {})

    application settings to send



83
84
85
86
# File 'lib/plushie/bridge.rb', line 83

def start(settings: {})
  @settings = settings
  connect!
end

#stopObject

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