Recently I was building a Service APP using Sinatra, Postgres SQL and Grape. After running the server(puma/webrick) and making API call, the first response was 200. But at the same time when hit next API call I got this error:
ActiveRecord::ConnectionTimeoutError: could not obtain a database connection within 5.000 seconds (waited 5.002 seconds)
After reading and researching I was able to resolve the above problem. The weird problem was due to gem Grape which is lacking thread safety.
I use this step to solve the problem:
Insert this line to config.ru file of your app
use ActiveRecord::ConnectionAdapters::ConnectionManagement
Probably this solution may help you and save your time.