Skip to content

Commit

Permalink
feat: print warning if db pool is less than rpc pool
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 13, 2023
1 parent 057cbb5 commit 5a5758c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][])

Expand Down
3 changes: 2 additions & 1 deletion lib/anycable/rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions lib/anycable/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/integrations/rails_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5a5758c

Please sign in to comment.