Action Cable の設定オブジェクトをトップレベルへ移動
Action Cable の設定クラスを ActionCable::Server::Configuration から ActionCable::Configuration へ移動し、旧名は互換性エイリアスとして残すことで、設定取得をトップレベルに統一しました。この変更は既存の API 動作を保持しつつ、サーバー実装の差し替えを容易にする設計的整理です。
背景
従来、Action Cable の設定は ActionCable::Server::Configuration に集約されており、サーバー実装の変更と設定取得が同一名前空間に混在していました。モジュラーサーバーリファクタリング (#50979) では、デフォルトの Rack hijack 実装と低レベルランタイム部品を明確に分離する方針が示されました。これにより、外部サーバー(例: Falcon/Async)を差し替える際に内部設定を触る必要がなくなることが期待されました。
本 PR はその流れを受け、設定オブジェクト自体をトップレベルに切り出すことで、サーバー置換フック config.action_cable.server_class と独立させる狙いがあります。レビュー指摘に応じて、本体のサーバー置換ロジックを別 PR (#57803) に分割し、今回の PR では純粋に名前空間の整理だけを行います。結果として、設定取得は ActionCable.config 経由で一元管理でき、後方互換性はエイリアスで保たれます。
以上の背景から、設定クラスをトップレベルへ移動することは、拡張性と保守性を向上させる設計的選択といえます。既存コードはエイリアスによりそのまま動作し、既存ユーザーへの影響は最小化されます。新たな名前空間は将来的な拡張点を提供します。
技術的な変更
新規に actioncable/lib/action_cable/configuration.rb が追加され、従来の ActionCable::Server::Configuration 実装がそのまま移植されています。クラスは ActionCable::Configuration として定義され、属性アクセサや初期化ロジックがそのまま保持されています。pubsub_adapter メソッド等、機能的な差異はありません。
# frozen_string_literal: true
# :markup: markdown
require "rack"
module ActionCable
# # Action Cable Configuration
#
# An instance of this configuration object is available via
# ActionCable.server.config, which allows you to tweak Action Cable
# configuration in a Rails config initializer.
class Configuration
attr_accessor :logger, :log_tags
attr_accessor :connection_class, :worker_pool_size, :executor_pool_size
attr_accessor :disable_request_forgery_protection, :allowed_request_origins, :allow_same_origin_as_host, :filter_parameters
attr_accessor :cable, :url, :mount_path
attr_accessor :precompile_assets
attr_accessor :health_check_path, :health_check_application
attr_writer :pubsub_adapter
def initialize
@log_tags = []
@connection_class = -> { ActionCable::Connection::Base }
@worker_pool_size = 4
@executor_pool_size = 10
@disable_request_forgery_protection = false
@allow_same_origin_as_host = true
@filter_parameters = []
@health_check_application = ->(env) {
[200, { Rack::CONTENT_TYPE => "text/html", "date" => Time.now.httpdate }, []]
}
end
def pubsub_adapter
return @pubsub_adapter.constantize if @pubsub_adapter
adapter = (cable.fetch("adapter") { "redis" })
# ...
end
end
end
actioncable/lib/action_cable/server/configuration.rb は大幅に削減され、require "action_cable/configuration" のみを残し、Configuration = ActionCable::Configuration というエイリアス定義に置き換えられました。これにより、旧名を参照したコードは依然として同一オブジェクトを取得します。削除された 66 行の実装は新ファイルに集約されています。
require "action_cable/configuration"
module ActionCable
module Server
Configuration = ActionCable::Configuration
end
end
テスト actioncable/test/server/base_test.rb には、エイリアスとデフォルトサーバー設定オブジェクトの整合性を検証するテストが追加されました。assert_same ActionCable::Configuration, ActionCable::Server::Configuration によりクラス同一性を、assert_instance_of ActionCable::Configuration, ActionCable::Server::Base.config によりインスタンス型を確認しています。これにより、振る舞いが変わらないことが自動的に保証されます。
+ test "server configuration is available from ActionCable" do
+ assert_same ActionCable::Configuration, ActionCable::Server::Configuration
+ assert_instance_of ActionCable::Configuration, ActionCable::Server::Base.config
+ end
設計判断
今回のリファクタリングは、後方互換性 を最優先にしつつ名前空間を整理する設計判断が反映されています。ActionCable::Server::Configuration をエイリアスとして残すことで、既存コードは変更不要で動作し、同時に新しい ActionCable::Configuration が公式の設定入口となります。エイリアスは一行で実装でき、ランタイムオーバーヘッドもほぼゼロです。
このアプローチは、設定取得の一元化とサーバー置換フックの独立性を同時に実現し、将来的なサーバーモジュールの追加や削除を安全に行える基盤を提供します。開発者は従来通り ActionCable.server.config を利用でき、設定ファイルの場所が変更される心配はありません。結果として、Rails 本体の拡張性が高まります。
まとめ
ActionCable::Configuration への移行は、設定取得をトップレベルに統一しつつ既存コードへの影響を最小化する、慎重かつ実用的なリファクタリングです。エイリアスにより後方互換性が保たれ、テストで動作保証が追加されたことで安全性が確保されています。今後のサーバー実装差し替えや機能拡張が容易になる基盤として、Rails のリアルタイム機能の進化に寄与します。