跳至內容 跳至搜尋

Action Cable Server Base

可以透過 ActionCable.server 取得單例 ActionCable::Server 實例。 啟動 Action Cable 伺服器的 Rack 程序會使用它,使用者也可以使用它來取得 RemoteConnections 物件,該物件用於在所有伺服器上尋找和斷開連線。

此外,這也是用於廣播的伺服器實例。 詳細資訊請參閱 Broadcasting

方法
C
D
E
L
N
P
R
W
已包含模組

屬性

[R] config
[R] mutex

類別公開方法

logger()

# File actioncable/lib/action_cable/server/base.rb, line 26
def self.logger; config.logger; end

new(config: self.class.config)

# File actioncable/lib/action_cable/server/base.rb, line 31
def initialize(config: self.class.config)
  @config = config
  @mutex = Monitor.new
  @remote_connections = @event_loop = @worker_pool = @pubsub = nil
end

實例公開方法

call(env)

由 Rack 呼叫以設定伺服器。

# File actioncable/lib/action_cable/server/base.rb, line 38
def call(env)
  return config.health_check_application.call(env) if env["PATH_INFO"] == config.health_check_path
  setup_heartbeat_timer
  config.connection_class.call.new(self, env).process
end

connection_identifiers()

套用至此伺服器相關聯的連線類別的所有識別碼。

# File actioncable/lib/action_cable/server/base.rb, line 102
def connection_identifiers
  config.connection_class.call.identifiers
end

disconnect(identifiers)

透過 RemoteConnections 在此伺服器或任何其他伺服器上斷開由 identifiers 識別的所有連線。

# File actioncable/lib/action_cable/server/base.rb, line 46
def disconnect(identifiers)
  remote_connections.where(identifiers).disconnect
end

event_loop()

# File actioncable/lib/action_cable/server/base.rb, line 71
def event_loop
  @event_loop || @mutex.synchronize { @event_loop ||= ActionCable::Connection::StreamEventLoop.new }
end

pubsub()

所有串流/廣播使用的轉接器。

# File actioncable/lib/action_cable/server/base.rb, line 96
def pubsub
  @pubsub || @mutex.synchronize { @pubsub ||= config.pubsub_adapter.new(self) }
end

remote_connections()

RemoteConnections 的閘道器。 詳細資訊請參閱該類別。

# File actioncable/lib/action_cable/server/base.rb, line 67
def remote_connections
  @remote_connections || @mutex.synchronize { @remote_connections ||= RemoteConnections.new(self) }
end

restart()

# File actioncable/lib/action_cable/server/base.rb, line 50
def restart
  connections.each do |connection|
    connection.close(reason: ActionCable::INTERNAL[:disconnect_reasons][:server_restart])
  end

  @mutex.synchronize do
    # Shutdown the worker pool
    @worker_pool.halt if @worker_pool
    @worker_pool = nil

    # Shutdown the pub/sub adapter
    @pubsub.shutdown if @pubsub
    @pubsub = nil
  end
end

worker_pool()

工作線程池是我們執行連線回調和通道動作的地方。 我們在伺服器的主線程上盡可能少做事情。 工作線程池是一個執行器服務,它由一個從任務隊列工作的線程池支持。 默認情況下,線程池大小最多為 4 個工作線程。 使用 config.action_cable.worker_pool_size 自行調整大小。

在通道動作中使用 Active Record、Redis 等表示您將從工作線程池中的每個線程獲得單獨的連線。 相應地規劃您的部署:5 個伺服器,每個伺服器運行 5 個 Puma 工作進程,每個進程運行一個 8 線程工作線程池,這意味著至少 200 個資料庫連線。

此外,請確保您的資料庫連線池大小至少與您的工作線程池大小一樣大。 否則,工作線程可能會超額訂閱資料庫連線池並在等待其他工作線程釋放其連線時阻塞。 請改用較小的工作線程池或較大的資料庫連線池。

# File actioncable/lib/action_cable/server/base.rb, line 91
def worker_pool
  @worker_pool || @mutex.synchronize { @worker_pool ||= ActionCable::Server::Worker.new(max_size: config.worker_pool_size) }
end