aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/server
diff options
context:
space:
mode:
Diffstat (limited to 'lib/action_cable/server')
-rw-r--r--lib/action_cable/server/base.rb5
-rw-r--r--lib/action_cable/server/broadcasting.rb6
-rw-r--r--lib/action_cable/server/configuration.rb5
-rw-r--r--lib/action_cable/server/connections.rb15
-rw-r--r--lib/action_cable/server/worker.rb12
-rw-r--r--lib/action_cable/server/worker/active_record_connection_management.rb (renamed from lib/action_cable/server/worker/clear_database_connections.rb)4
6 files changed, 40 insertions, 7 deletions
diff --git a/lib/action_cable/server/base.rb b/lib/action_cable/server/base.rb
index 43849928b9..f1585dc776 100644
--- a/lib/action_cable/server/base.rb
+++ b/lib/action_cable/server/base.rb
@@ -1,3 +1,5 @@
+require 'em-hiredis'
+
module ActionCable
module Server
# A singleton ActionCable::Server instance is available via ActionCable.server. It's used by the rack process that starts the cable server, but
@@ -18,6 +20,7 @@ module ActionCable
# Called by rack to setup the server.
def call(env)
+ setup_heartbeat_timer
config.connection_class.new(self, env).process
end
@@ -65,5 +68,7 @@ module ActionCable
config.connection_class.identifiers
end
end
+
+ ActiveSupport.run_load_hooks(:action_cable, Base.config)
end
end
diff --git a/lib/action_cable/server/broadcasting.rb b/lib/action_cable/server/broadcasting.rb
index 037b98951e..6e0fbae387 100644
--- a/lib/action_cable/server/broadcasting.rb
+++ b/lib/action_cable/server/broadcasting.rb
@@ -1,3 +1,5 @@
+require 'redis'
+
module ActionCable
module Server
# Broadcasting is how other parts of your application can send messages to the channel subscribers. As explained in Channel, most of the time, these
@@ -44,9 +46,9 @@ module ActionCable
def broadcast(message)
server.logger.info "[ActionCable] Broadcasting to #{broadcasting}: #{message}"
- server.broadcasting_redis.publish broadcasting, message.to_json
+ server.broadcasting_redis.publish broadcasting, ActiveSupport::JSON.encode(message)
end
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/action_cable/server/configuration.rb b/lib/action_cable/server/configuration.rb
index ac9fa7b085..b22de273b8 100644
--- a/lib/action_cable/server/configuration.rb
+++ b/lib/action_cable/server/configuration.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/hash/indifferent_access'
+
module ActionCable
module Server
# An instance of this configuration object is available via ActionCable.server.config, which allows you to tweak the configuration points
@@ -6,6 +8,7 @@ module ActionCable
attr_accessor :logger, :log_tags
attr_accessor :connection_class, :worker_pool_size
attr_accessor :redis_path, :channels_path
+ attr_accessor :disable_request_forgery_protection, :allowed_request_origins
def initialize
@logger = Rails.logger
@@ -16,6 +19,8 @@ module ActionCable
@redis_path = Rails.root.join('config/redis/cable.yml')
@channels_path = Rails.root.join('app/channels')
+
+ @disable_request_forgery_protection = false
end
def channel_paths
diff --git a/lib/action_cable/server/connections.rb b/lib/action_cable/server/connections.rb
index 15d7c3c8c7..47dcea8c20 100644
--- a/lib/action_cable/server/connections.rb
+++ b/lib/action_cable/server/connections.rb
@@ -4,6 +4,8 @@ module ActionCable
# you can't use this collection as an full list of all the connections established against your application. Use RemoteConnections for that.
# As such, this is primarily for internal use.
module Connections
+ BEAT_INTERVAL = 3
+
def connections
@connections ||= []
end
@@ -16,9 +18,20 @@ module ActionCable
connections.delete connection
end
+ # WebSocket connection implementations differ on when they'll mark a connection as stale. We basically never want a connection to go stale, as you
+ # then can't rely on being able to receive and send to it. So there's a 3 second heartbeat running on all connections. If the beat fails, we automatically
+ # disconnect.
+ def setup_heartbeat_timer
+ EM.next_tick do
+ @heartbeat_timer ||= EventMachine.add_periodic_timer(BEAT_INTERVAL) do
+ EM.next_tick { connections.map(&:beat) }
+ end
+ end
+ end
+
def open_connections_statistics
connections.map(&:statistics)
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/action_cable/server/worker.rb b/lib/action_cable/server/worker.rb
index d7823ecf93..e063b2a2e1 100644
--- a/lib/action_cable/server/worker.rb
+++ b/lib/action_cable/server/worker.rb
@@ -1,3 +1,6 @@
+require 'celluloid'
+require 'active_support/callbacks'
+
module ActionCable
module Server
# Worker used by Server.send_async to do connection work in threads. Only for internal use.
@@ -5,10 +8,13 @@ module ActionCable
include ActiveSupport::Callbacks
include Celluloid
+ attr_reader :connection
define_callbacks :work
- include ClearDatabaseConnections
+ include ActiveRecordConnectionManagement
def invoke(receiver, method, *args)
+ @connection = receiver
+
run_callbacks :work do
receiver.send method, *args
end
@@ -20,6 +26,8 @@ module ActionCable
end
def run_periodic_timer(channel, callback)
+ @connection = channel.connection
+
run_callbacks :work do
callback.respond_to?(:call) ? channel.instance_exec(&callback) : channel.send(callback)
end
@@ -31,4 +39,4 @@ module ActionCable
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/action_cable/server/worker/clear_database_connections.rb b/lib/action_cable/server/worker/active_record_connection_management.rb
index 722d363a41..1ede0095f8 100644
--- a/lib/action_cable/server/worker/clear_database_connections.rb
+++ b/lib/action_cable/server/worker/active_record_connection_management.rb
@@ -2,7 +2,7 @@ module ActionCable
module Server
class Worker
# Clear active connections between units of work so the long-running channel or connection processes do not hoard connections.
- module ClearDatabaseConnections
+ module ActiveRecordConnectionManagement
extend ActiveSupport::Concern
included do
@@ -12,7 +12,7 @@ module ActionCable
end
def with_database_connections
- yield
+ ActiveRecord::Base.logger.tagged(*connection.logger.tags) { yield }
ensure
ActiveRecord::Base.clear_active_connections!
end