From 5a5758c6e7bcc88c50b01f4cc2a06f354922c6e4 Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Fri, 13 Oct 2023 16:37:20 -0700 Subject: [PATCH] feat: print warning if db pool is less than rpc pool --- CHANGELOG.md | 4 +++- lib/anycable/rails/config.rb | 3 ++- lib/anycable/rails/railtie.rb | 19 +++++++++++++++++++ spec/integrations/rails_integration_spec.rb | 9 +++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c541b8a..4e3bc81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ ## master -- Add support for broadcast options (e.g., `exclude_socket`). ([@palkan][]) +- Print warning if the database pool size is less than RPC pool size. ([@palkan][]) + +- Add support for broadcast options (e.g., `exclude_socket`) and `broadcast ..., to_others: true`. ([@palkan][]) - Add `batch_broadcasts` option to automatically batch broadcasts for code wrapped in Rails executor. ([@palkan][]) diff --git a/lib/anycable/rails/config.rb b/lib/anycable/rails/config.rb index 4ec0e2e..3639b3a 100644 --- a/lib/anycable/rails/config.rb +++ b/lib/anycable/rails/config.rb @@ -17,6 +17,7 @@ embedded: false, http_rpc_mount_path: nil, batch_broadcasts: false, - socket_id_header: "X-Socket-ID" + socket_id_header: "X-Socket-ID", + disable_rpc_pool_size_warning: false ) AnyCable::Config.ignore_options :access_logs_disabled, :persistent_session_enabled diff --git a/lib/anycable/rails/railtie.rb b/lib/anycable/rails/railtie.rb index 825dde0..bb969ef 100644 --- a/lib/anycable/rails/railtie.rb +++ b/lib/anycable/rails/railtie.rb @@ -108,6 +108,25 @@ class Railtie < ::Rails::Railtie # :nodoc: end end + initializer "anycable.verify_pool_sizes" do + next if AnyCable.config.disable_rpc_pool_size_warning? + # Skip if non-gRPC server is used + next unless AnyCable.config.respond_to?(:rpc_pool_size) + + # Log current db vs. gRPC pool sizes + AnyCable.configure_server do + db_pool_size = ActiveRecord::Base.connection_pool.size + rpc_pool_size = AnyCable.config.rpc_pool_size + + if rpc_pool_size > db_pool_size + ::Kernel.warn( + "\n⛔️ WARNING: AnyCable RPC pool size (#{rpc_pool_size}) is greater than DB pool size (#{db_pool_size})\n" \ + "Please, consider adjusting the database pool size to avoid connection wait times and increase throughput of your RPC server\n\n" + ) + end + end + end + # Since Rails 6.1 if respond_to?(:server) server do diff --git a/spec/integrations/rails_integration_spec.rb b/spec/integrations/rails_integration_spec.rb index 857de5b..088b61f 100644 --- a/spec/integrations/rails_integration_spec.rb +++ b/spec/integrations/rails_integration_spec.rb @@ -60,4 +60,13 @@ def app expect(context[:payload]).to be_a(Hash) end end + + context "database pool check" do + before { allow(Kernel).to receive(:warn) } + + specify do + AnyCable.server_callbacks.last.call + expect(Kernel).to have_received(:warn).with(/is greater than DB pool size/) + end + end end