aws-sdk-railsでSQSをActive Jobのアダプターとして使っていたら、DBのPoolを消費しまくっていた件

aws-sdk-railsでSQSをActive Jobのアダプターとして使っていたら、

ActiveRecord::ConnectionTimeoutError (could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use):

というように、DBのプールを使い切ってしまった。

これを治すには、 bundle exec aws_sqs_active_job --queue default --threads=2 のように --threads を指定する必要がある。

原因は、この aws_sqs_active_jobデフォルト設定 にある。

{
  threads: 2*Concurrent.processor_count,
  max_messages: 10,
  shutdown_timeout: 15,
  backpressure: 10
}

CPUプロセッサ数の2倍が設定されているため、12コア24スレッドのRyzen 9 5900Xを利用していた私は、24並列でプログラムを動かし、DBのプールを食いつぶしていたという訳です。