Class: SurrealDB::Connections::ReliableWebSocket

Inherits:
Base
  • Object
show all
Defined in:
lib/surrealdb/connections/reliable_websocket.rb

Overview

Wraps a WebSocket connection with automatic reconnection.

On disconnect, transparently reconnects using the same URL and options, replays state-restoring calls (use, signin, let), re-subscribes live query handlers, and retries the failed request once.

Modeled on the Go SDK's contrib/rews (reliable WebSocket).

Instance Attribute Summary collapse

Attributes inherited from Base

#rpc, #url

Instance Method Summary collapse

Constructor Details

#initialize(inner, **options) ⇒ ReliableWebSocket

Returns a new instance of ReliableWebSocket.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 19

def initialize(inner, **options)
  super(inner.url, **options)
  @inner = inner
  @max_retries = options.fetch(:reconnect_max_retries, 5)
  @reconnect_delay = options.fetch(:reconnect_delay, 1.0)
  @state_mutex = Mutex.new
  @last_ns = nil
  @last_db = nil
  @last_auth_method = nil
  @last_auth_params = nil
  @variables = {}
  @live_subscriptions = {}
end

Instance Attribute Details

#max_retriesInteger (readonly)

Returns max reconnection attempts before giving up.

Returns:

  • (Integer)

    max reconnection attempts before giving up



14
15
16
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 14

def max_retries
  @max_retries
end

#reconnect_delayFloat (readonly)

Returns base delay between reconnection attempts (seconds).

Returns:

  • (Float)

    base delay between reconnection attempts (seconds)



17
18
19
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 17

def reconnect_delay
  @reconnect_delay
end

Instance Method Details

#closeObject



38
39
40
41
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 38

def close
  @inner.close
  @connected = false
end

#connectObject



33
34
35
36
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 33

def connect
  @inner.connect
  @connected = true
end

#connected?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 43

def connected?
  @inner.connected?
end

#on_notification(live_query_id, handler) ⇒ Object



61
62
63
64
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 61

def on_notification(live_query_id, handler)
  @state_mutex.synchronize { @live_subscriptions[live_query_id] = handler }
  @inner.on_notification(live_query_id, handler)
end

#remove_notification_handler(live_query_id) ⇒ Object



66
67
68
69
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 66

def remove_notification_handler(live_query_id)
  @state_mutex.synchronize { @live_subscriptions.delete(live_query_id) }
  @inner.remove_notification_handler(live_query_id)
end

#send_request(method, params = []) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 51

def send_request(method, params = [])
  track_state(method, params)
  @inner.send_request(method, params)
rescue ConnectionError => e
  reconnect!
  log(:info, "Reconnected after: #{e.message}")
  replay_state
  @inner.send_request(method, params)
end

#supports_live_queries?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/surrealdb/connections/reliable_websocket.rb', line 47

def supports_live_queries?
  true
end