Skip to content

Commit 1bcf657

Browse files
committed
* upstream sync
1 parent 9a0c98c commit 1bcf657

13 files changed

Lines changed: 116 additions & 32 deletions

File tree

lib/action_cable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module ActionCable
7474
},
7575
default_mount_path: "/cable",
7676
protocols: ["actioncable-v1-json", "actioncable-unsupported"].freeze
77-
}
77+
}.freeze
7878

7979
# Singleton instance of the server
8080
module_function def server

lib/action_cable/channel/base.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def action_methods
135135
public_instance_methods(false) -
136136
# Except the internal methods
137137
internal_methods).uniq
138+
138139
methods.map!(&:name)
139140
methods.to_set
140141
end
@@ -148,15 +149,15 @@ def clear_action_methods! # :doc:
148149
@action_methods = nil
149150
end
150151

151-
def internal_methods
152-
super
153-
end
154-
155152
# Refresh the cached action_methods when a new action_method is added.
156153
def method_added(name) # :doc:
157154
super
158155
clear_action_methods!
159156
end
157+
158+
def internal_methods
159+
super
160+
end
160161
end
161162

162163
def initialize(connection, identifier, params = {})

lib/action_cable/channel/callbacks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Callbacks
3939
extend ActiveSupport::Concern
4040
include ActiveSupport::Callbacks
4141

42-
INTERNAL_METHODS = [:_run_subscribe_callbacks, :_run_unsubscribe_callbacks] # :nodoc:
42+
INTERNAL_METHODS = [:_run_subscribe_callbacks, :_run_unsubscribe_callbacks].freeze # :nodoc:
4343

4444
included do
4545
define_callbacks :subscribe

lib/action_cable/connection/base.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
require "active_support/rescuable"
66

7+
begin
8+
require "active_support/inspect_backport"
9+
rescue LoadError
10+
# ActiveSupport::InspectBackport ships only in Rails 8.2+; older Rails keeps the gem's custom #inspect.
11+
end
12+
713
module ActionCable
814
module Connection
915
# # Action Cable Connection Base
@@ -147,11 +153,19 @@ def beat
147153
transmit type: ActionCable::INTERNAL[:message_types][:ping], message: Time.now.to_i
148154
end
149155

150-
def inspect # :nodoc:
151-
"#<#{self.class.name}:#{'%#016x' % (object_id << 1)}>"
156+
if defined?(ActiveSupport::InspectBackport)
157+
ActiveSupport::InspectBackport.apply(self)
158+
else
159+
def inspect # :nodoc:
160+
"#<#{self.class.name}:#{'%#016x' % (object_id << 1)}>"
161+
end
152162
end
153163

154164
private
165+
def instance_variables_to_inspect # :nodoc:
166+
[].freeze
167+
end
168+
155169
# The cookies of the request that initiated the WebSocket connection. Useful for performing authorization checks.
156170
def cookies # :doc:
157171
request.cookie_jar

lib/action_cable/engine.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class Engine < Rails::Engine # :nodoc:
1212
config.action_cable.mount_path = ActionCable::INTERNAL[:default_mount_path]
1313
config.action_cable.precompile_assets = true
1414

15+
if respond_to?(:guard_load_hooks)
16+
guard_load_hooks(
17+
:action_cable_channel, :action_cable_connection,
18+
:action_cable_test_case, :action_cable_connection_test_case,
19+
)
20+
end
21+
1522
initializer "action_cable.deprecator", before: :load_environment_config do |app|
1623
app.deprecators[:action_cable] = ActionCable.deprecator
1724
end
@@ -83,15 +90,17 @@ class Engine < Rails::Engine # :nodoc:
8390
end
8491
end
8592

93+
app.reloader.before_class_unload do
94+
ActionCable.server.restart
95+
end
96+
end
97+
98+
ActiveSupport.on_load(:action_cable_channel) do
8699
wrap = lambda do |_, inner|
87100
app.executor.wrap(source: "application.action_cable", &inner)
88101
end
89102
ActionCable::Channel::Base.set_callback :subscribe, :around, prepend: true, &wrap
90103
ActionCable::Channel::Base.set_callback :unsubscribe, :around, prepend: true, &wrap
91-
92-
app.reloader.before_class_unload do
93-
ActionCable.server.restart
94-
end
95104
end
96105
end
97106

lib/action_cable/server/base.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module ActionCable
88
module Server
99
# A wrapper over ConcurrentRuby::ThreadPoolExecutor and Concurrent::TimerTask
1010
class ThreadedExecutor # :nodoc:
11-
def initialize(max_size: 10)
11+
def initialize(max_size: 10, name: "server")
1212
@executor = Concurrent::ThreadPoolExecutor.new(
13-
name: "ActionCable-streamer",
13+
name: "ActionCable-#{name}",
1414
min_threads: 1,
1515
max_threads: max_size,
1616
max_queue: 0,
@@ -121,7 +121,7 @@ def worker_pool
121121

122122
# Executor is used by various actions within Action Cable (e.g., pub/sub operations) to run code asynchronously.
123123
def executor
124-
@executor || @mutex.synchronize { @executor ||= ThreadedExecutor.new(max_size: config.executor_pool_size) }
124+
@executor || @mutex.synchronize { @executor ||= ThreadedExecutor.new(max_size: config.executor_pool_size, name: "streamer") }
125125
end
126126

127127
# Adapter used for all streams/broadcasting.
@@ -146,8 +146,9 @@ def new_tagged_logger(request = nil, &block)
146146
def allow_request_origin?(env)
147147
return true if config.disable_request_forgery_protection
148148

149-
proto = Rack::Request.new(env).ssl? ? "https" : "http"
150-
if config.allow_same_origin_as_host && env["HTTP_ORIGIN"] == "#{proto}://#{env['HTTP_HOST']}"
149+
request = ActionDispatch::Request.new(env)
150+
proto = request.ssl? ? "https" : "http"
151+
if config.allow_same_origin_as_host && env["HTTP_ORIGIN"] == "#{proto}://#{request.host_with_port}"
151152
true
152153
elsif Array(config.allowed_request_origins).any? { |allowed_origin| allowed_origin === env["HTTP_ORIGIN"] }
153154
true

lib/action_cable/server/broadcasting.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ module Server
2121
# ActionCable.server.broadcast \
2222
# "web_notifications_1", { title: "New things!", body: "All that's fit for print" }
2323
#
24-
# // Client-side JavaScript, which assumes you've already requested the right to send web notifications:
24+
# # Client-side JavaScript, which assumes you've already requested the right to send web notifications:
2525
# App.cable.subscriptions.create("WebNotificationsChannel", {
2626
# received: function(data) {
27-
# new Notification(data['title'], { body: data['body'] });
27+
# new Notification(data['title'], { body: data['body'] })
2828
# }
29-
# });
29+
# })
3030
module Broadcasting
3131
# Broadcast a hash directly to a named `broadcasting`. This will later be JSON
3232
# encoded.

lib/action_cable/server/socket.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
require "action_dispatch"
44

5+
begin
6+
require "active_support/inspect_backport"
7+
rescue LoadError
8+
# ActiveSupport::InspectBackport ships only in Rails 8.2+; older Rails keeps the gem's custom #inspect.
9+
end
10+
511
module ActionCable
612
module Server
713
# This class encapsulates all the low-level logic of working with the underlying WebSocket conenctions
@@ -108,11 +114,19 @@ def on_close(reason, code) # :nodoc:
108114
send_async :handle_close
109115
end
110116

111-
def inspect # :nodoc:
112-
"#<#{self.class.name}:#{'%#016x' % (object_id << 1)}>"
117+
if defined?(ActiveSupport::InspectBackport)
118+
ActiveSupport::InspectBackport.apply(self)
119+
else
120+
def inspect # :nodoc:
121+
"#<#{self.class.name}:#{'%#016x' % (object_id << 1)}>"
122+
end
113123
end
114124

115125
private
126+
def instance_variables_to_inspect # :nodoc:
127+
[].freeze
128+
end
129+
116130
attr_reader :websocket
117131
attr_reader :message_buffer
118132

lib/action_cable/subscription_adapter/postgresql.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def shutdown
3838
def with_subscriptions_connection(&block) # :nodoc:
3939
# Action Cable is taking ownership over this database connection, and will
4040
# perform the necessary cleanup tasks.
41-
# We purposedly avoid #checkout to not end up with a pinned connection
41+
# We purposely avoid #checkout to not end up with a pinned connection
4242
ar_conn = ActiveRecord::Base.connection_pool.new_connection
4343
pg_conn = ar_conn.raw_connection
4444

test/channel/stream_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def channel.subscribed
253253
threads = []
254254
run_in_eventmachine do
255255
connection = Connection.new(server, socket)
256-
server.pubsub.unsubscribe_latency = 0.1
256+
connection.pubsub.unsubscribe_latency = 0.1
257257

258258
channel = ChatChannel.new connection, "{id: 1}", id: 1
259259
channel.subscribe_to_channel

0 commit comments

Comments
 (0)