Skip to content

Commit d8c7c86

Browse files
committed
Consistently use net_http_persistent with a memorized Faraday object
1 parent 3ec3bdb commit d8c7c86

File tree

7 files changed

+46
-32
lines changed

7 files changed

+46
-32
lines changed

app/services/task_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 TaskService < 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
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
# frozen_string_literal: true
22

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

1111
def execute
12-
response = connection.post do |req|
13-
req.headers['Content-Type'] = 'application/json'
14-
req.headers['Authorization'] = authorization_header
15-
req.body = {uuid: @uuid}.to_json
16-
end
12+
body = {uuid: @uuid}.to_json
13+
response = self.class.connection.post(@account_link.check_uuid_url) {|request| request_parameters(request, body) }
1714
response_hash = JSON.parse(response.body, symbolize_names: true).slice(:uuid_found, :update_right)
1815

1916
{error: false, message: message(response_hash)}.merge(response_hash)
@@ -23,8 +20,12 @@ def execute
2320

2421
private
2522

26-
def authorization_header
27-
"Bearer #{@account_link.api_key}"
23+
def request_parameters(request, body)
24+
request.tap do |req|
25+
req.headers['Content-Type'] = 'application/json'
26+
req.headers['Authorization'] = "Bearer #{@account_link.api_key}"
27+
req.body = body
28+
end
2829
end
2930

3031
def message(response_hash)
@@ -38,14 +39,5 @@ def message(response_hash)
3839
I18n.t('tasks.task_service.check_external.no_task')
3940
end
4041
end
41-
42-
def connection
43-
Faraday.new(url: @account_link.check_uuid_url) do |faraday|
44-
faraday.options[:open_timeout] = 5
45-
faraday.options[:timeout] = 5
46-
47-
faraday.adapter Faraday.default_adapter
48-
end
49-
end
5042
end
5143
end

app/services/task_service/handle_groups.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
module TaskService
3+
class TaskService
44
class HandleGroups < ServiceBase
55
def initialize(user:, task:, group_tasks_params:)
66
super()

app/services/task_service/push_external.rb

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

3-
module TaskService
4-
class PushExternal < ServiceBase
3+
class TaskService
4+
class PushExternal < TaskService
55
def initialize(zip:, account_link:)
66
super()
77
@zip = zip
@@ -11,7 +11,7 @@ def initialize(zip:, account_link:)
1111
def execute
1212
body = @zip.string
1313
begin
14-
response = connection.post {|request| request_parameters(request, body) }
14+
response = self.class.connection.post(@account_link.push_url) {|request| request_parameters(request, body) }
1515
response.success? ? nil : response.body
1616
rescue StandardError => e
1717
e
@@ -28,11 +28,5 @@ def request_parameters(request, body)
2828
req.body = body
2929
end
3030
end
31-
32-
def connection
33-
Faraday.new(url: @account_link.push_url) do |faraday|
34-
faraday.adapter Faraday.default_adapter
35-
end
36-
end
3731
end
3832
end

lib/nbp/push_connector.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,15 @@ def auth
116116
end
117117

118118
def api_conn
119-
Faraday.new(url: settings.api_host, headers:)
119+
# Refresh headers (incl. the dynamic API token) for each request
120+
return @api_conn.tap {|req| req.headers = headers } if @api_conn
121+
122+
@api_conn ||= Faraday.new(url: settings.api_host, headers:) do |faraday|
123+
faraday.options[:open_timeout] = 5
124+
faraday.options[:timeout] = 5
125+
126+
faraday.adapter :net_http_persistent
127+
end
120128
end
121129

122130
def source_slug

spec/services/task_service/check_external_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@
7777
end
7878

7979
context 'when the request fails' do
80-
before { allow(Faraday).to receive(:new).and_raise(Faraday::Error, 'error') }
80+
before do
81+
# Un-memoize the connection to force a reconnection
82+
described_class.instance_variable_set(:@connection, nil)
83+
allow(Faraday).to receive(:new).and_raise(Faraday::Error, 'error')
84+
end
8185

8286
it 'returns the correct hash' do
8387
expect(check_external_service).to eql(error: true, message: I18n.t('common.errors.generic'))

spec/services/task_service/push_external_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,18 @@
4949

5050
context 'when response status is 500' do
5151
let(:status) { 500 }
52-
let(:response) { 'an error occured' }
52+
let(:response) { 'an error occurred' }
5353

5454
it { is_expected.to be response }
5555
end
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)