Skip to content

Instantly share code, notes, and snippets.

@raggi
Created January 5, 2013 19:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raggi/4463147 to your computer and use it in GitHub Desktop.
Save raggi/4463147 to your computer and use it in GitHub Desktop.
class Safeware
def initialize(app)
@app = app
end
# This dup pattern is used frequently to avoid race conditions on state stored
# inside this middleware. It's not foolproof, but if you're just using
# single-reference instance variables (instance variables with primitive
# values (not data structures)) then it works well.
def call(env)
dup._call(env)
end
def _call(env)
# before actions
status, headers, body = @app.call(env)
# after actions
return status, headers, body
ensure
# If we're raising an exception, then the body from the app will not be
# passed on to the server or any parent middleware. In order to conform to
# the body close specification, we therefore must close it here under that
# condition. n.b. it's possible that an exception occurs before body is set,
# so we check for body first.
body.close if body && body.respond_to?(:close) && $!
end
# TODO before and after action breakouts for the non-rack async api, with proc
# wrappers in env.
# TODO logging / errors integration
# TODO input rewind example
# ...
end
@shime
Copy link

shime commented Jan 8, 2013

❤️ ❤️

Thanks for descriptive comments!

@bf4
Copy link

bf4 commented May 13, 2014

fwiw, referenced from airbrake/airbrake#153

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment