anycable / anycable-rails

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

How to control RPC max threads? #203

Closed 0xMostafa closed 3 weeks ago

0xMostafa commented 3 weeks ago

Tell us about your environment

Ruby version: 2.7

Rails version: 5.2

anycable gem version: ~> 0.6.0

anycable-rails gem version: 0.6.5

grpc gem version: ~> 1.17

What did you do?

I ran rpc process

bundle exec anycable

check the number of threads: ps -o thcount <RPC-PID>

THCNT
  45

This number is the same locally, on staging and on prod

What did you expect to happen?

Use less threads or let me control it, like with puma's RAILS_MAX_THREADS or sidekiq's concurrency

What actually happened?

I'm load testing a chat app, it works fine for small number of users but when i increase number of concurrent users, latency increases, up to 3-4s (depending on number of concurrent users)

I've a trace of my chat code

Screenshot from 2024-08-17 08-10-10

As you see, it takes ~500ms to start executing method's code (on ruby channel class).

I'm not sure what blank sections in trace signify but I'm guessing it could be waiting to acquire GVL to start executing. But i cannot verify it, since GVL instrumentation API was added in Ruby 3.2.0 and I'm on Ruby 2.7.

I'm trying to use less threads to check if latency decreases (waits are shorter),

how can i control bundle exec anycable's thread count?

palkan commented 3 weeks ago

You can set the thread pool size via the rpc_pool_size configuration parameter (e.g., in your anycable.yml). See https://docs.anycable.io/ruby/configuration?id=concurrency-settings

0xMostafa commented 3 weeks ago

Thank you