From 1d7d141f95a0b5910569967b341fdb41c5f58866 Mon Sep 17 00:00:00 2001 From: Antti Leinonen Date: Sat, 5 Oct 2024 09:33:26 +0300 Subject: [PATCH] Add Rack timeout to timeout requests that take too long --- Gemfile | 1 + Gemfile.lock | 23 ++++++++--------------- app/assets/stylesheets/submissions.css | 1 - app/controllers/application_controller.rb | 8 ++++++++ config.ru | 3 +++ config/initializers/rack_timeout.rb | 7 +++++++ 6 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 config/initializers/rack_timeout.rb diff --git a/Gemfile b/Gemfile index ec82e9eb8..7b413d3cb 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,7 @@ gem 'pg', '~> 1.5', '>= 1.5.6' gem 'pghero', '~> 3.5' gem 'rack-attack', '~> 6.7', require: false gem 'rack-cors', '~> 2.0', '>= 2.0.2' +gem 'rack-timeout', '~> 0.7.0' gem 'rack-mini-profiler', '~> 3.3', '>= 3.3.1' gem 'rake', '~> 13.2', '>= 13.2.1' gem 'responders', '~> 3.1', '>= 3.1.1' diff --git a/Gemfile.lock b/Gemfile.lock index 97ae0023b..8f3137e0e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -171,16 +171,7 @@ GEM faraday-patron (1.0.0) faraday-rack (1.0.0) faraday-retry (1.0.3) - ffi (1.17.0-aarch64-linux-gnu) - ffi (1.17.0-aarch64-linux-musl) - ffi (1.17.0-arm-linux-gnu) - ffi (1.17.0-arm-linux-musl) - ffi (1.17.0-arm64-darwin) - ffi (1.17.0-x86-linux-gnu) - ffi (1.17.0-x86-linux-musl) - ffi (1.17.0-x86_64-darwin) - ffi (1.17.0-x86_64-linux-gnu) - ffi (1.17.0-x86_64-linux-musl) + ffi (1.17.0) ffi-compiler (1.3.2) ffi (>= 1.15.5) rake @@ -346,6 +337,7 @@ GEM rack (< 3) rack-test (2.1.0) rack (>= 1.3) + rack-timeout (0.7.0) rackup (1.0.0) rack (< 3) webrick @@ -538,17 +530,17 @@ GEM PLATFORMS aarch64-linux - aarch64-linux-gnu + aarch64-linux aarch64-linux-musl arm-linux - arm-linux-gnu + arm-linux arm-linux-musl arm64-darwin x86-linux - x86-linux-gnu + x86-linux x86-linux-musl x86_64-darwin - x86_64-linux-gnu + x86_64-linux x86_64-linux-musl DEPENDENCIES @@ -591,6 +583,7 @@ DEPENDENCIES rack-attack (~> 6.7) rack-cors (~> 2.0, >= 2.0.2) rack-mini-profiler (~> 3.3, >= 3.3.1) + rack-timeout (~> 0.7.0) railroady (~> 1.6) rails (~> 7.1, >= 7.1.3.4) rails-controller-testing (~> 1.0, >= 1.0.5) @@ -615,4 +608,4 @@ DEPENDENCIES xml-simple (~> 1.1, >= 1.1.9) BUNDLED WITH - 2.5.16 + 2.5.21 diff --git a/app/assets/stylesheets/submissions.css b/app/assets/stylesheets/submissions.css index d425ea9f9..f05003263 100644 --- a/app/assets/stylesheets/submissions.css +++ b/app/assets/stylesheets/submissions.css @@ -16,7 +16,6 @@ user-select: unset; } } - /* 2022-01-01 BUG: Mozilla inserts newlines when copying text before div blocks. FIX: set display: inline-block on div.hljs-ln-line diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0e41547c4..ea090aef7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -42,6 +42,14 @@ class ApplicationController < ActionController::Base respond_with_error('File not found', 404, exception) end + rescue_from Rack::Timeout::RequestTimeoutException do |exception| + respond_with_error('Request timed out', 503, exception) # status code 408 would be the correct status code, but when receiving 408 Firefox retries 10 times and then states the connection was reset instead of showing our error + end + + rescue_from Rack::Timeout::RequestTimeoutError do |exception| + respond_with_error('Request timed out', 503, exception) # status code 408 would be the correct status code, but when receiving 408 Firefox retries 10 times and then states the connection was reset instead of showing our error + end + before_action :set_default_url_options before_action :check_api_version before_action :set_bare_layout diff --git a/config.ru b/config.ru index b259ed7d9..a9347ec35 100644 --- a/config.ru +++ b/config.ru @@ -5,5 +5,8 @@ require ::File.expand_path('../config/environment', __FILE__) run TmcServer::Application +require 'rack-timeout' +Rack::Timeout + require 'rack/attack' use Rack::Attack diff --git a/config/initializers/rack_timeout.rb b/config/initializers/rack_timeout.rb new file mode 100644 index 000000000..a21217bae --- /dev/null +++ b/config/initializers/rack_timeout.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# set the timeout to 2 minutes +ENV['RACK_TIMEOUT_SERVICE_TIMEOUT'] = '120' + +# rack-timeout is too verbose by default, only log errors +# Rack::Timeout::Logger.level = Logger::ERROR