Skip to content

Commit 8b1d7cf

Browse files
committed
Add timeouts
1 parent e460376 commit 8b1d7cf

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

app/models/sponsor.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,21 @@ def ads_for(ip)
88
params = { forwardedip: ip }
99
params.merge!( testMode: true, ignore: true ) if Rails.env.development?
1010
uri = URI::HTTPS.build(host: HOST, path: PATH, query: params.to_query)
11-
response = Faraday.get(uri)
12-
results = JSON.parse(response.body) rescue nil
11+
12+
results = begin
13+
start = Time.now
14+
response = Faraday.new(url: uri).get do |req|
15+
req.options.timeout = 2 # open/read timeout in seconds
16+
req.options.open_timeout = 1 # connection open timeout in seconds
17+
end
18+
logger.info "sponsor=success seconds=#{"%.2f" % (Time.now - start)}"
19+
20+
JSON.parse(response.body) rescue nil
21+
rescue Faraday::TimeoutError
22+
logger.info "sponsor=timeout seconds=#{"%.2f" % (Time.now - start)}"
23+
nil
24+
end
25+
1326
return [] if results.nil?
1427
results['ads'].select{|a| a['creativeid'] }.collect{ |data| build_sponsor(data) }
1528
end

config/database.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ test:
8181
#
8282
production:
8383
<<: *default
84+
connect_timeout: 1
85+
checkout_timeout: 1
8486
database: coderwall-next_production
8587
username: coderwall-next
8688
password: <%= ENV['CODERWALL-NEXT_DATABASE_PASSWORD'] %>
89+
variables:
90+
statement_timeout: 500 # ms

config/puma.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
threads_count = Integer(ENV['MAX_THREADS'] || 5)
33
threads threads_count, threads_count
44

5+
worker_timeout 15
6+
worker_shutdown_timeout 8
7+
58
preload_app!
69

710
rackup DefaultRackup

0 commit comments

Comments
 (0)