about summary refs log tree commit homepage
path: root/lib/unicorn/http_server.rb
diff options
context:
space:
mode:
authorBlake Williams <blake@blakewilliams.me>2020-12-08 16:47:16 -0500
committerEric Wong <bofh@yhbt.net>2020-12-09 21:58:33 +0000
commit673c15e3f020bccc0336838617875b26c9a45f4e (patch)
tree79112011c77d624338a47cea0316684031322fad /lib/unicorn/http_server.rb
parent2c347116305338710331d238fefa23f00e98cf54 (diff)
downloadunicorn-673c15e3f020bccc0336838617875b26c9a45f4e.tar.gz
This adds `rack.after_reply` functionality which allows rack middleware
to pass lambdas that will be executed after the client connection has
been closed.

This was driven by a need to perform actions in a request that shouldn't
block the request from completing but also don't make sense as
background jobs.

There is prior art of this being supported found in a few gems, as well
as this functionality existing in other rack based servers (e.g. Puma).

[ew: check if `env' is set in ensure statement]

Acked-by: Eric Wong <e@80x24.org>
Diffstat (limited to 'lib/unicorn/http_server.rb')
-rw-r--r--lib/unicorn/http_server.rb4
1 files changed, 4 insertions, 0 deletions
diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index 05dad99..c0f14ba 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -629,6 +629,8 @@ class Unicorn::HttpServer
       end
     end
 
+    env["rack.after_reply"] = []
+
     status, headers, body = @app.call(env)
 
     begin
@@ -651,6 +653,8 @@ class Unicorn::HttpServer
     end
   rescue => e
     handle_error(client, e)
+  ensure
+    env["rack.after_reply"].each(&:call) if env
   end
 
   def nuke_listeners!(readers)