Skip to content

Commit 1d7d141

Browse files
committed
Add Rack timeout to timeout requests that take too long
1 parent 3f34c9d commit 1d7d141

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ gem 'pg', '~> 1.5', '>= 1.5.6'
2626
gem 'pghero', '~> 3.5'
2727
gem 'rack-attack', '~> 6.7', require: false
2828
gem 'rack-cors', '~> 2.0', '>= 2.0.2'
29+
gem 'rack-timeout', '~> 0.7.0'
2930
gem 'rack-mini-profiler', '~> 3.3', '>= 3.3.1'
3031
gem 'rake', '~> 13.2', '>= 13.2.1'
3132
gem 'responders', '~> 3.1', '>= 3.1.1'

Gemfile.lock

+8-15
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,7 @@ GEM
171171
faraday-patron (1.0.0)
172172
faraday-rack (1.0.0)
173173
faraday-retry (1.0.3)
174-
ffi (1.17.0-aarch64-linux-gnu)
175-
ffi (1.17.0-aarch64-linux-musl)
176-
ffi (1.17.0-arm-linux-gnu)
177-
ffi (1.17.0-arm-linux-musl)
178-
ffi (1.17.0-arm64-darwin)
179-
ffi (1.17.0-x86-linux-gnu)
180-
ffi (1.17.0-x86-linux-musl)
181-
ffi (1.17.0-x86_64-darwin)
182-
ffi (1.17.0-x86_64-linux-gnu)
183-
ffi (1.17.0-x86_64-linux-musl)
174+
ffi (1.17.0)
184175
ffi-compiler (1.3.2)
185176
ffi (>= 1.15.5)
186177
rake
@@ -346,6 +337,7 @@ GEM
346337
rack (< 3)
347338
rack-test (2.1.0)
348339
rack (>= 1.3)
340+
rack-timeout (0.7.0)
349341
rackup (1.0.0)
350342
rack (< 3)
351343
webrick
@@ -538,17 +530,17 @@ GEM
538530

539531
PLATFORMS
540532
aarch64-linux
541-
aarch64-linux-gnu
533+
aarch64-linux
542534
aarch64-linux-musl
543535
arm-linux
544-
arm-linux-gnu
536+
arm-linux
545537
arm-linux-musl
546538
arm64-darwin
547539
x86-linux
548-
x86-linux-gnu
540+
x86-linux
549541
x86-linux-musl
550542
x86_64-darwin
551-
x86_64-linux-gnu
543+
x86_64-linux
552544
x86_64-linux-musl
553545

554546
DEPENDENCIES
@@ -591,6 +583,7 @@ DEPENDENCIES
591583
rack-attack (~> 6.7)
592584
rack-cors (~> 2.0, >= 2.0.2)
593585
rack-mini-profiler (~> 3.3, >= 3.3.1)
586+
rack-timeout (~> 0.7.0)
594587
railroady (~> 1.6)
595588
rails (~> 7.1, >= 7.1.3.4)
596589
rails-controller-testing (~> 1.0, >= 1.0.5)
@@ -615,4 +608,4 @@ DEPENDENCIES
615608
xml-simple (~> 1.1, >= 1.1.9)
616609

617610
BUNDLED WITH
618-
2.5.16
611+
2.5.21

app/assets/stylesheets/submissions.css

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
user-select: unset;
1717
}
1818
}
19-
2019
/* 2022-01-01
2120
BUG: Mozilla inserts newlines when copying text before div blocks.
2221
FIX: set display: inline-block on div.hljs-ln-line

app/controllers/application_controller.rb

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ class ApplicationController < ActionController::Base
4242
respond_with_error('File not found', 404, exception)
4343
end
4444

45+
rescue_from Rack::Timeout::RequestTimeoutException do |exception|
46+
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
47+
end
48+
49+
rescue_from Rack::Timeout::RequestTimeoutError do |exception|
50+
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
51+
end
52+
4553
before_action :set_default_url_options
4654
before_action :check_api_version
4755
before_action :set_bare_layout

config.ru

+3
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
require ::File.expand_path('../config/environment', __FILE__)
66
run TmcServer::Application
77

8+
require 'rack-timeout'
9+
Rack::Timeout
10+
811
require 'rack/attack'
912
use Rack::Attack

config/initializers/rack_timeout.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
# set the timeout to 2 minutes
4+
ENV['RACK_TIMEOUT_SERVICE_TIMEOUT'] = '120'
5+
6+
# rack-timeout is too verbose by default, only log errors
7+
# Rack::Timeout::Logger.level = Logger::ERROR

0 commit comments

Comments
 (0)