Module: FastMcpPubsub::RackTransportPatch

Defined in:
lib/fast_mcp_pubsub/rack_transport_patch.rb

Overview

Lazy patch application - applies patch when FastMcp transport is first accessed

Class Method Summary collapse

Class Method Details

.add_basic_methodsObject



36
37
38
39
40
41
# File 'lib/fast_mcp_pubsub/rack_transport_patch.rb', line 36

def self.add_basic_methods
  FastMcp::Transports::RackTransport.class_eval do
    alias_method :send_local_message, :send_message unless method_defined?(:send_local_message)
    define_method(:running?) { @running } unless method_defined?(:running?)
  end
end

.add_fallback_methodObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fast_mcp_pubsub/rack_transport_patch.rb', line 59

def self.add_fallback_method
  FastMcp::Transports::RackTransport.class_eval do
    return if method_defined?(:broadcast_with_fallback)

    define_method(:broadcast_with_fallback) do |message, client_id = nil|
      FastMcpPubsub.logger.debug "RackTransport: Broadcasting message via PostgreSQL PubSub"
      FastMcpPubsub::Service.broadcast(message, client_id)
    rescue StandardError => e
      FastMcpPubsub.logger.error "RackTransport: Error broadcasting message: #{e.message}"
      # An addressed message stays addressed even when the cluster hop fails.
      # Falling back to the fan-out would answer every other open session with
      # this client's data, which is the failure the addressing exists to end.
      client_id ? send_local_message_to(client_id, message) : send_local_message(message)
    end
  end
end

.add_send_message_overrideObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fast_mcp_pubsub/rack_transport_patch.rb', line 43

def self.add_send_message_override
  FastMcp::Transports::RackTransport.class_eval do
    return if method_defined?(:send_message_with_pubsub)

    alias_method :send_message_original, :send_message if method_defined?(:send_message)

    define_method(:send_message) do |message|
      return send_local_message(message) unless FastMcpPubsub.config.enabled

      broadcast_with_fallback(message, FastMcpPubsub::CurrentClient.id)
    end

    alias_method :send_message_with_pubsub, :send_message
  end
end

.apply_patch!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fast_mcp_pubsub/rack_transport_patch.rb', line 11

def self.apply_patch!
  if @patch_applied
    FastMcpPubsub.logger.debug "FastMcpPubsub: RackTransport patch already applied, skipping"
    return
  end

  unless defined?(FastMcp::Transports::RackTransport)
    FastMcpPubsub.logger.debug "FastMcpPubsub: FastMcp::Transports::RackTransport not defined yet, skipping patch"
    return
  end

  FastMcpPubsub.logger.info "FastMcpPubsub: Patching FastMcp::Transports::RackTransport for PostgreSQL PubSub support"

  patch_transport_class
  @patch_applied = true
  FastMcpPubsub.logger.info "FastMcpPubsub: RackTransport patch applied successfully"
end

.patch_applied?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/fast_mcp_pubsub/rack_transport_patch.rb', line 76

def self.patch_applied?
  @patch_applied
end

.patch_transport_classObject



29
30
31
32
33
34
# File 'lib/fast_mcp_pubsub/rack_transport_patch.rb', line 29

def self.patch_transport_class
  add_basic_methods
  add_send_message_override
  add_fallback_method
  AddressingPatch.apply!
end