-
-
Save lmumar/5683023 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SequelReconnector | |
RETRY_LIMIT = 10 # Match this to your connection pool size. | |
def initialize(app, db) | |
@app = app | |
@db = db | |
end | |
def call(env) | |
retries = 0 | |
connected = false | |
begin | |
@db.synchronize do | |
@db.run(@db.select(1).sql) | |
connected = true | |
@app.call(env) | |
end | |
rescue Sequel::DatabaseDisconnectError | |
if connected || retries > RETRY_LIMIT | |
raise | |
else | |
if retries > 0 | |
@db.loggers.each do |logger| | |
logger.warn("SequelReconnector: Retrying connection...") | |
end | |
end | |
retries += 1 | |
retry | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment