From 673c15e3f020bccc0336838617875b26c9a45f4e Mon Sep 17 00:00:00 2001 From: Blake Williams Date: Tue, 8 Dec 2020 16:47:16 -0500 Subject: Add rack.after_reply functionality 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 --- lib/unicorn/http_server.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib') 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) -- cgit v1.2.3-24-ge0c7