Skip to content

Instantly share code, notes, and snippets.

@arthurnn
Created October 23, 2013 18:49
Show Gist options
  • Save arthurnn/7124345 to your computer and use it in GitHub Desktop.
Save arthurnn/7124345 to your computer and use it in GitHub Desktop.
class MongoidConnectionMiddleware
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
ensure
clean_connection
end
def clean_connection
id = Thread.current.object_id
session = Mongoid.default_session
pool = session.cluster.seeds.first.send(:pool)
conn = pool.send(:pinned)[id]
conn.expire
pool.send(:unpinned).push(pool.send(:pinned).delete(id))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment