Skip to content

Instantly share code, notes, and snippets.

@lmumar
Forked from jacaetevha/sequel_reconnector.rb
Created May 31, 2013 05:02
Show Gist options
  • Save lmumar/5683023 to your computer and use it in GitHub Desktop.
Save lmumar/5683023 to your computer and use it in GitHub Desktop.
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