Sam Stern
Sam Stern

Reputation: 25134

ActiveRecord + EventMachine + Postgres + Sinatra

I am trying to write a Sinatra app that has websockets. My Sinatra app uses Postgres as a backend (runs perfectly without websockets). I added eventmachine and em-websocket to give my app ws support. Everything works great except for that after x number of requests, I get an ActiveRecord ConnectionPool timeout. It seems like EventMachine is not releasing DB connections, but I have never used EM before so I am not exactly sure what to do. What is the best way to us these four (AR + EM + PG + Sinatra) technologies together without everything going up in flames? Ideally looking for some sort of ConnectionPool gem that will cause EM to release the DB connection at the end of the request, or something of that nature.

Upvotes: 2

Views: 534

Answers (1)

Ilia Khokhriakov
Ilia Khokhriakov

Reputation: 3687

ActiveRecord has a middleware ActiveRecord::ConnectionAdapters::ConnectionManagement that closes connections properly after request. You can activate it in config.ru (I assume that you start Sinatra app with a config.ru file. If you are unfamiliar with using Sinatra with config.ru there is some information about it in the docs). Place this line below require ... and above run ...:

use ActiveRecord::ConnectionAdapters::ConnectionManagement

Upvotes: 1

Related Questions