anycable / anycable-rails

AnyCable for Ruby on Rails applications
https://anycable.io
MIT License
495 stars 35 forks source link

What's the best way to close open connections on forking? #183

Closed zedtux closed 1 year ago

zedtux commented 1 year ago

Tell us about your environment

Ruby version: 3.0.4

Rails version: 6.1

anycable gem version: 1.3.0

anycable-rails gem version: 1.3.7

grpc gem version: 1.54.0

What did you do?

I am migrating my app from running ActionCable with Puma to AnyCable. I have the following code in my Puma config file:

# frozen_string_literal: true

# Based on https://www.mongodb.com/docs/mongoid/7.5/reference/configuration/#puma
on_worker_boot do

  Mongoid::Clients.clients.each do |name, client|
    client.close
    client.reconnect
  end
end

# Based on https://www.mongodb.com/docs/mongoid/7.5/reference/configuration/#puma
before_fork do
  Mongoid.disconnect_clients
end

# ...

Which closes open connections to MongoDB on forking (see the MongoDB documentation link for more details).

My questions are:

Thanks for AnyCable !!

Envek commented 1 year ago

No, you don't need to do the same for AnyCable, as there is no forking or child process spawning in AnyCable RPC part as far as I know. Refer to AnyCable architecture for details.

palkan commented 1 year ago

Yep, @Envek is correct. AnyCable RPC server uses a single process, no forking. So, no special attention is required.

zedtux commented 1 year ago

Thank you both for your input 👌

Envek commented 1 year ago

@palkan, I'm curious about Embedded mode though. What happens when AnyCable RPC is embedded into clustered Puma for example? Does it stay in master process? :thinking:

palkan commented 1 year ago

That's a good question.

Does it stay in master process?

Yeah, I think it should, but only if we preload the app.

In a clustered Puma setup, I run an RPC server within a single worker process. Here is an example: https://github.com/anycable/anycable-twilio-hanami-demo/blob/35aa3ad6dbcb47420a0b9c86af1a0290b242675d/kaisen/config/puma.rb#L17-L26