Skip to content

Commit fd931ba

Browse files
committed
Consistently use net_http_persistent with a memorized Faraday object
1 parent 63fd9d5 commit fd931ba

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

app/services/exercise_service.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class ExerciseService < ServiceBase
4+
def self.connection
5+
@connection ||= Faraday.new do |faraday|
6+
faraday.options[:open_timeout] = 5
7+
faraday.options[:timeout] = 5
8+
9+
faraday.adapter :net_http_persistent
10+
end
11+
end
12+
end

app/services/exercise_service/check_external.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# frozen_string_literal: true
22

3-
module ExerciseService
4-
class CheckExternal < ServiceBase
3+
class ExerciseService
4+
class CheckExternal < ExerciseService
55
def initialize(uuid:, codeharbor_link:)
66
super()
77
@uuid = uuid
88
@codeharbor_link = codeharbor_link
99
end
1010

1111
def execute
12-
response = connection.post do |req|
12+
response = self.class.connection.post @codeharbor_link.check_uuid_url do |req|
1313
req.headers['Content-Type'] = 'application/json'
1414
req.headers['Authorization'] = "Bearer #{@codeharbor_link.api_key}"
1515
req.body = {uuid: @uuid}.to_json
@@ -30,14 +30,5 @@ def message(task_found, update_right)
3030
I18n.t('exercises.export_codeharbor.check.no_task')
3131
end
3232
end
33-
34-
def connection
35-
Faraday.new(url: @codeharbor_link.check_uuid_url) do |faraday|
36-
faraday.options[:open_timeout] = 5
37-
faraday.options[:timeout] = 5
38-
39-
faraday.adapter Faraday.default_adapter
40-
end
41-
end
4233
end
4334
end
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

3-
module ExerciseService
4-
class PushExternal < ServiceBase
3+
class ExerciseService
4+
class PushExternal < ExerciseService
55
def initialize(zip:, codeharbor_link:)
66
super()
77
@zip = zip
@@ -11,7 +11,7 @@ def initialize(zip:, codeharbor_link:)
1111
def execute
1212
body = @zip.string
1313
begin
14-
response = connection.post do |request|
14+
response = self.class.connection.post @codeharbor_link.push_url do |request|
1515
request.headers['Content-Type'] = 'application/zip'
1616
request.headers['Content-Length'] = body.length.to_s
1717
request.headers['Authorization'] = "Bearer #{@codeharbor_link.api_key}"
@@ -23,13 +23,5 @@ def execute
2323
e.message
2424
end
2525
end
26-
27-
private
28-
29-
def connection
30-
Faraday.new(url: @codeharbor_link.push_url) do |faraday|
31-
faraday.adapter Faraday.default_adapter
32-
end
33-
end
3426
end
3527
end

lib/runner/strategy/poseidon.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,18 @@ def self.headers
311311

312312
def self.http_connection
313313
@http_connection ||= Faraday.new(ssl: {ca_file: config[:ca_file]}, headers:) do |faraday|
314+
faraday.options[:open_timeout] = 5
315+
faraday.options[:timeout] = 5
316+
314317
faraday.adapter :net_http_persistent
315318
end
316319
end
317320

318321
def self.new_http_connection
319322
Faraday.new(ssl: {ca_file: config[:ca_file]}, headers:) do |faraday|
323+
faraday.options[:open_timeout] = 5
324+
faraday.options[:timeout] = 5
325+
320326
faraday.adapter :net_http
321327
end
322328
end

spec/services/exercise_service/check_external_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@
7474
end
7575

7676
context 'when the request fails' do
77-
before { allow(Faraday).to receive(:new).and_raise(Faraday::Error, 'error') }
77+
before do
78+
# Un-memoize the connection to force a reconnection
79+
described_class.instance_variable_set(:@connection, nil)
80+
allow(Faraday).to receive(:new).and_raise(Faraday::Error, 'error')
81+
end
7882

7983
it 'returns the correct hash' do
8084
expect(check_external_service).to eql(error: true, message: I18n.t('exercises.export_codeharbor.error'))

spec/services/exercise_service/push_external_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@
5656
end
5757

5858
context 'when an error occurs' do
59-
before { allow(Faraday).to receive(:new).and_raise(StandardError) }
59+
before do
60+
# Un-memoize the connection to force a reconnection
61+
described_class.instance_variable_set(:@connection, nil)
62+
allow(Faraday).to receive(:new).and_raise(StandardError)
63+
end
6064

6165
it { is_expected.not_to be_nil }
6266
end

0 commit comments

Comments
 (0)