aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/action_cable.rb10
-rw-r--r--lib/action_cable/channel/base.rb7
-rw-r--r--lib/action_cable/connection/base.rb10
-rw-r--r--lib/action_cable/connection/tagged_logger_proxy.rb12
-rw-r--r--lib/action_cable/server/worker/active_record_connection_management.rb2
-rw-r--r--lib/assets/javascripts/cable.coffee11
-rw-r--r--lib/assets/javascripts/cable.coffee.erb8
-rw-r--r--lib/assets/javascripts/cable/connection.coffee15
-rw-r--r--lib/assets/javascripts/cable/connection_monitor.coffee2
-rw-r--r--lib/assets/javascripts/cable/subscriptions.coffee2
10 files changed, 44 insertions, 35 deletions
diff --git a/lib/action_cable.rb b/lib/action_cable.rb
index 89ffa1fda7..3919812161 100644
--- a/lib/action_cable.rb
+++ b/lib/action_cable.rb
@@ -5,6 +5,16 @@ require 'action_cable/version'
module ActionCable
extend ActiveSupport::Autoload
+ INTERNAL = {
+ identifiers: {
+ ping: '_ping'.freeze
+ },
+ message_types: {
+ confirmation: 'confirm_subscription'.freeze,
+ rejection: 'reject_subscription'.freeze
+ }
+ }
+
# Singleton instance of the server
module_function def server
@server ||= ActionCable::Server::Base.new
diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb
index b0e112ce38..ca903a810d 100644
--- a/lib/action_cable/channel/base.rb
+++ b/lib/action_cable/channel/base.rb
@@ -89,9 +89,6 @@ module ActionCable
include Naming
include Broadcasting
- SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE = 'confirm_subscription'.freeze
- SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE = 'reject_subscription'.freeze
-
attr_reader :params, :connection, :identifier
delegate :logger, to: :connection
@@ -258,7 +255,7 @@ module ActionCable
def transmit_subscription_confirmation
unless subscription_confirmation_sent?
logger.info "#{self.class.name} is transmitting the subscription confirmation"
- connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE)
+ connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: ActionCable::INTERNAL[:message_types][:confirmation])
@subscription_confirmation_sent = true
end
end
@@ -270,7 +267,7 @@ module ActionCable
def transmit_subscription_rejection
logger.info "#{self.class.name} is transmitting the subscription rejection"
- connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE)
+ connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: ActionCable::INTERNAL[:message_types][:rejection])
end
end
end
diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb
index a988060d56..b93b6a8a50 100644
--- a/lib/action_cable/connection/base.rb
+++ b/lib/action_cable/connection/base.rb
@@ -56,7 +56,7 @@ module ActionCable
def initialize(server, env)
@server, @env = server, env
- @logger = new_tagged_logger || server.logger
+ @logger = new_tagged_logger
@websocket = ActionCable::Connection::WebSocket.new(env)
@subscriptions = ActionCable::Connection::Subscriptions.new(self)
@@ -119,7 +119,7 @@ module ActionCable
end
def beat
- transmit ActiveSupport::JSON.encode(identifier: '_ping', message: Time.now.to_i)
+ transmit ActiveSupport::JSON.encode(identifier: ActionCable::INTERNAL[:identifiers][:ping], message: Time.now.to_i)
end
@@ -194,10 +194,8 @@ module ActionCable
# Tags are declared in the server but computed in the connection. This allows us per-connection tailored tags.
def new_tagged_logger
- if server.logger.respond_to?(:tagged)
- TaggedLoggerProxy.new server.logger,
- tags: server.config.log_tags.map { |tag| tag.respond_to?(:call) ? tag.call(request) : tag.to_s.camelize }
- end
+ TaggedLoggerProxy.new server.logger,
+ tags: server.config.log_tags.map { |tag| tag.respond_to?(:call) ? tag.call(request) : tag.to_s.camelize }
end
def started_request_message
diff --git a/lib/action_cable/connection/tagged_logger_proxy.rb b/lib/action_cable/connection/tagged_logger_proxy.rb
index 34063c1d42..e5319087fb 100644
--- a/lib/action_cable/connection/tagged_logger_proxy.rb
+++ b/lib/action_cable/connection/tagged_logger_proxy.rb
@@ -16,6 +16,15 @@ module ActionCable
@tags = @tags.uniq
end
+ def tag(logger)
+ if logger.respond_to?(:tagged)
+ current_tags = tags - logger.formatter.current_tags
+ logger.tagged(*current_tags) { yield }
+ else
+ yield
+ end
+ end
+
%i( debug info warn error fatal unknown ).each do |severity|
define_method(severity) do |message|
log severity, message
@@ -24,8 +33,7 @@ module ActionCable
protected
def log(type, message)
- current_tags = tags - @logger.formatter.current_tags
- @logger.tagged(*current_tags) { @logger.send type, message }
+ tag(@logger) { @logger.send type, message }
end
end
end
diff --git a/lib/action_cable/server/worker/active_record_connection_management.rb b/lib/action_cable/server/worker/active_record_connection_management.rb
index 1ede0095f8..ecece4e270 100644
--- a/lib/action_cable/server/worker/active_record_connection_management.rb
+++ b/lib/action_cable/server/worker/active_record_connection_management.rb
@@ -12,7 +12,7 @@ module ActionCable
end
def with_database_connections
- ActiveRecord::Base.logger.tagged(*connection.logger.tags) { yield }
+ connection.logger.tag(ActiveRecord::Base.logger) { yield }
ensure
ActiveRecord::Base.clear_active_connections!
end
diff --git a/lib/assets/javascripts/cable.coffee b/lib/assets/javascripts/cable.coffee
deleted file mode 100644
index fca5e095b5..0000000000
--- a/lib/assets/javascripts/cable.coffee
+++ /dev/null
@@ -1,11 +0,0 @@
-#= require_self
-#= require cable/consumer
-
-@Cable =
- PING_IDENTIFIER: "_ping"
- INTERNAL_MESSAGES:
- SUBSCRIPTION_CONFIRMATION: 'confirm_subscription'
- SUBSCRIPTION_REJECTION: 'reject_subscription'
-
- createConsumer: (url) ->
- new Cable.Consumer url
diff --git a/lib/assets/javascripts/cable.coffee.erb b/lib/assets/javascripts/cable.coffee.erb
new file mode 100644
index 0000000000..8498233c11
--- /dev/null
+++ b/lib/assets/javascripts/cable.coffee.erb
@@ -0,0 +1,8 @@
+#= require_self
+#= require cable/consumer
+
+@Cable =
+ INTERNAL: <%= ActionCable::INTERNAL.to_json %>
+
+ createConsumer: (url) ->
+ new Cable.Consumer url
diff --git a/lib/assets/javascripts/cable/connection.coffee b/lib/assets/javascripts/cable/connection.coffee
index b6b99413dc..b2abe8dcb2 100644
--- a/lib/assets/javascripts/cable/connection.coffee
+++ b/lib/assets/javascripts/cable/connection.coffee
@@ -1,4 +1,7 @@
# Encapsulate the cable connection held by the consumer. This is an internal class not intended for direct user manipulation.
+
+{message_types} = Cable.INTERNAL
+
class Cable.Connection
@reopenDelay: 500
@@ -54,17 +57,13 @@ class Cable.Connection
message: (event) ->
{identifier, message, type} = JSON.parse(event.data)
- if type?
- @handleTypeMessage(type)
- else
- @consumer.subscriptions.notify(identifier, "received", message)
-
- onTypeMessage: (type) ->
switch type
- when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_CONFIRMATION
+ when message_types.confirmation
@consumer.subscriptions.notify(identifier, "connected")
- when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_REJECTION
+ when message_types.rejection
@consumer.subscriptions.reject(identifier)
+ else
+ @consumer.subscriptions.notify(identifier, "received", message)
open: ->
@disconnected = false
diff --git a/lib/assets/javascripts/cable/connection_monitor.coffee b/lib/assets/javascripts/cable/connection_monitor.coffee
index bf99dee34d..435efcc361 100644
--- a/lib/assets/javascripts/cable/connection_monitor.coffee
+++ b/lib/assets/javascripts/cable/connection_monitor.coffee
@@ -7,7 +7,7 @@ class Cable.ConnectionMonitor
@staleThreshold: 6 # Server::Connections::BEAT_INTERVAL * 2 (missed two pings)
- identifier: Cable.PING_IDENTIFIER
+ identifier: Cable.INTERNAL.identifiers.ping
constructor: (@consumer) ->
@consumer.subscriptions.add(this)
diff --git a/lib/assets/javascripts/cable/subscriptions.coffee b/lib/assets/javascripts/cable/subscriptions.coffee
index 13db32eb2c..7955565f06 100644
--- a/lib/assets/javascripts/cable/subscriptions.coffee
+++ b/lib/assets/javascripts/cable/subscriptions.coffee
@@ -63,7 +63,7 @@ class Cable.Subscriptions
sendCommand: (subscription, command) ->
{identifier} = subscription
- if identifier is Cable.PING_IDENTIFIER
+ if identifier is Cable.INTERNAL.identifiers.ping
@consumer.connection.isOpen()
else
@consumer.send({command, identifier})