From b07e99d346ae561ce14e207acc4e2cdb6478fc47 Mon Sep 17 00:00:00 2001 From: Justin Bull Date: Wed, 22 Apr 2020 10:25:28 -0400 Subject: [PATCH] Ensure Rack::ContentLength is loaded as middleware 6.0.5 removed Sidekiq::WebApplication's own Content-Length header calculation, expecting Rack to do it instead via some middleware. This was to fix an issue where New Relic would inject content into Sidekiq's Web UI after the hand crafted content length was calculated. While such middleware exists, it's not enabled by default (at least in a Rails app) and so the HTTP response doesn't have the header at all. The absence of the header has caused unexpected behaviour in an nginx and HAProxy setup, causing each page load to take exactly 20 seconds. The suspicion is that, without the Content-Length response header, HAProxy waits a preset amount of time before deciding the body must have surely been finished and then reponds. --- Changes.md | 1 + lib/sidekiq/web.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/Changes.md b/Changes.md index 9ebdf5498..530b0a140 100644 --- a/Changes.md +++ b/Changes.md @@ -5,6 +5,7 @@ Unreleased --------- +- Ensure `Rack::ContentLength` is loaded as middleware for correct Web UI responses [#4541] - Avoid exception dumping SSL store in Redis connection logging [#4532] 6.0.7 diff --git a/lib/sidekiq/web.rb b/lib/sidekiq/web.rb index 1fc0c47e3..3422bc510 100644 --- a/lib/sidekiq/web.rb +++ b/lib/sidekiq/web.rb @@ -12,6 +12,7 @@ require "sidekiq/web/application" require "rack/protection" +require "rack/content_length" require "rack/builder" require "rack/file" @@ -172,6 +173,13 @@ def build_sessions middlewares.unshift [[::Rack::Session::Cookie, options], nil] end + + # Since Sidekiq::WebApplication no longer calculates its own + # Content-Length response header, we must ensure that the Rack middleware + # that does this is loaded + unless using? ::Rack::ContentLength + middlewares.unshift [[::Rack::ContentLength], nil] + end end def build