Skip to content

Add configurable timeout value #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions lib/prerender_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,19 @@ def call(env)
return cached_response.finish
end

prerendered_response = get_prerendered_page_response(env)

if prerendered_response
response = build_rack_response_from_prerender(prerendered_response)
after_render(env, prerendered_response)
return response.finish
begin
prerendered_response = get_prerendered_page_response(env)

if prerendered_response
response = build_rack_response_from_prerender(prerendered_response)
after_render(env, prerendered_response)
return response.finish
end
rescue Timeout::Error
if @options[:read_timeout]
response = Rack::Response.new(nil, 503, nil)
return response.finish
end
end
end

Expand Down Expand Up @@ -183,6 +190,7 @@ def get_prerendered_page_response(env)
req.basic_auth(ENV['PRERENDER_USERNAME'], ENV['PRERENDER_PASSWORD']) if @options[:basic_auth]
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true if url.scheme == 'https'
http.read_timeout = @options[:read_timeout] if @options[:read_timeout]
response = http.request(req)
if response['Content-Encoding'] == 'gzip'
response.body = ActiveSupport::Gzip.decompress(response.body)
Expand Down
14 changes: 14 additions & 0 deletions test/lib/prerender_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@
end


it 'should return a timeout error to the crawler if the request takes longer than the read_timeout argument' do
request = Rack::MockRequest.env_for '/', 'HTTP_USER_AGENT' => bot
stub_request(:get, @prerender.build_api_url(request))
.with(headers: { 'User-Agent': bot })
.to_raise(Timeout::Error)

err = assert_raises Timeout::Error do
Rack::Prerender.new(@app, read_timeout: 22).call(request)
end

assert_equal err.message, '503 Service Unavailable'
end


it "should continue to app routes if the url is part of the regex specific blacklist" do
request = Rack::MockRequest.env_for "/search/things/123/page", "HTTP_USER_AGENT" => bot
response = Rack::Prerender.new(@app, blacklist: ['^/search', '/help']).call(request)
Expand Down