Skip to content

Commit

Permalink
Add Rack timeout to timeout requests that take too long
Browse files Browse the repository at this point in the history
  • Loading branch information
Redande committed Oct 5, 2024
1 parent 3f34c9d commit 1d7d141
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
23 changes: 8 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -615,4 +608,4 @@ DEPENDENCIES
xml-simple (~> 1.1, >= 1.1.9)

BUNDLED WITH
2.5.16
2.5.21
1 change: 0 additions & 1 deletion app/assets/stylesheets/submissions.css
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions config/initializers/rack_timeout.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1d7d141

Please sign in to comment.