Class: Ruflet::Rails::Protocol::LocalServer

Inherits:
Object
  • Object
show all
Includes:
ConnectionProtocol
Defined in:
lib/ruflet/rails/protocol/local_server.rb

Overview

Thin adapter that runs the shared Ruflet wire protocol (Ruflet::ConnectionProtocol) on sockets hijacked from the Rails server itself — Puma’s threads and process model do the scaling, and there is no second server implementation to maintain.

The Rails-specific behavior lives entirely in the hooks below: session resumption through the Rails session registry, Rails logging, and request-env capture for the registry.

Instance Method Summary collapse

Constructor Details

#initialize(session_registry: Ruflet::Rails.sessions, &app_block) ⇒ LocalServer

Returns a new instance of LocalServer.



19
20
21
22
23
24
# File 'lib/ruflet/rails/protocol/local_server.rb', line 19

def initialize(session_registry: Ruflet::Rails.sessions, &app_block)
  @app_block = app_block
  @session_registry = session_registry
  @sessions = {}
  @sessions_mutex = Mutex.new
end

Instance Method Details

#before_dispatch_event(ws, event) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/ruflet/rails/protocol/local_server.rb', line 45

def before_dispatch_event(ws, event)
  return unless ENV["RUFLET_RAILS_DEBUG_EVENTS"] == "true"

  warn(
    "[ruflet_rails] event socket=#{ws.session_key} " \
    "target=#{event["target"].inspect} name=#{event["name"].inspect} data=#{event["data"].inspect}"
  )
end

#log_connection_error(error) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/ruflet/rails/protocol/local_server.rb', line 54

def log_connection_error(error)
  if defined?(::Rails) && ::Rails.respond_to?(:logger) && ::Rails.logger
    ::Rails.logger.error(
      "RUFLET CRASH: #{error.class}: #{error.message}\n#{Array(error.backtrace).first(10).join("\n")}"
    )
  else
    super
  end
end

#resume_session(session_id) ⇒ Object

– ConnectionProtocol hooks ————————————



28
29
30
# File 'lib/ruflet/rails/protocol/local_server.rb', line 28

def resume_session(session_id)
  @session_registry[session_id]&.page
end

#session_removed(page, ws) ⇒ Object



41
42
43
# File 'lib/ruflet/rails/protocol/local_server.rb', line 41

def session_removed(page, ws)
  @session_registry.remove(page.session_id, connection_key: ws.session_key)
end

#session_stored(page, ws) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/ruflet/rails/protocol/local_server.rb', line 32

def session_stored(page, ws)
  @session_registry.add(
    key: page.session_id,
    page: page,
    env: Context.current_env,
    connection_key: ws.session_key
  )
end