Skip to content

Commit 498f0c4

Browse files
committed
Consistently use net_http_persistent with a memorized Faraday object
1 parent 99e9dd4 commit 498f0c4

File tree

7 files changed

+41
-29
lines changed

7 files changed

+41
-29
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gem 'coffee-rails', require: false
1010
gem 'config'
1111
gem 'devise-bootstrap-views'
1212
gem 'faraday'
13+
gem 'faraday-net_http_persistent'
1314
gem 'http_accept_language'
1415
gem 'i18n-js'
1516
gem 'image_processing'

Gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ GEM
167167
multipart-post (~> 2.0)
168168
faraday-net_http (3.4.0)
169169
net-http (>= 0.5.0)
170+
faraday-net_http_persistent (2.3.0)
171+
faraday (~> 2.5)
172+
net-http-persistent (>= 4.0.4, < 5)
170173
ffi (1.17.1)
171174
fugit (1.11.1)
172175
et-orbi (~> 1, >= 1.2.11)
@@ -276,6 +279,8 @@ GEM
276279
rails (>= 3.2.0)
277280
net-http (0.6.0)
278281
uri
282+
net-http-persistent (4.0.5)
283+
connection_pool (~> 2.2)
279284
net-imap (0.5.6)
280285
date
281286
net-protocol
@@ -613,6 +618,7 @@ DEPENDENCIES
613618
devise-bootstrap-views
614619
factory_bot_rails
615620
faraday
621+
faraday-net_http_persistent
616622
http_accept_language
617623
i18n-js
618624
i18n-tasks
@@ -741,6 +747,7 @@ CHECKSUMS
741747
faraday (2.12.2) sha256=157339c25c7b8bcb739f5cf1207cb0cefe8fa1c65027266bcbc34c90c84b9ad6
742748
faraday-multipart (1.1.0) sha256=856b0f1c7316a4d6c052dd2eef5c42f887d56d93a171fe8880da1af064ca0751
743749
faraday-net_http (3.4.0) sha256=a1f1e4cd6a2cf21599c8221595e27582d9936819977bbd4089a601f24c64e54a
750+
faraday-net_http_persistent (2.3.0) sha256=33d4948cabe9f8148222c4ca19634c71e1f25595cccf9da2e02ace8d754f1bb1
744751
ffi (1.17.1) sha256=26f6b0dbd1101e6ffc09d3ca640b2a21840cc52731ad8a7ded9fb89e5fb0fc39
745752
fugit (1.11.1) sha256=e89485e7be22226d8e9c6da411664d0660284b4b1c08cacb540f505907869868
746753
glob (0.4.1) sha256=e68e50419ffb7f896b39a483c1a37e7a1aa8f1a8c8ea13961f8cd1b50f40715d
@@ -787,6 +794,7 @@ CHECKSUMS
787794
nested_form (0.3.2) sha256=b1c468d7eac781235861c2f74fc9f675df0c4d915d5724aaf7fd29f7891c0538
788795
nested_form_fields (0.8.4) sha256=e3db8e935b40c6b6027ce65b10ee0c5cf575d1ba175be85154c81d4253635b19
789796
net-http (0.6.0) sha256=9621b20c137898af9d890556848c93603716cab516dc2c89b01a38b894e259fb
797+
net-http-persistent (4.0.5) sha256=6e42880b347e650ffeaf679ae59c9d5a6ed8a22cda6e1b959d9c270050aefa8e
790798
net-imap (0.5.6) sha256=1ede8048ee688a14206060bf37a716d18cb6ea00855f6c9b15daee97ee51fbe5
791799
net-pop (0.1.2) sha256=848b4e982013c15b2f0382792268763b748cce91c9e91e36b0f27ed26420dff3
792800
net-protocol (0.2.2) sha256=aa73e0cba6a125369de9837b8d8ef82a61849360eba0521900e2c3713aa162a8

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ def auth
116116
end
117117

118118
def api_conn
119-
Faraday.new(url: settings.api_host, headers:)
119+
@api_conn ||= Faraday.new(url: settings.api_host, headers:) do |faraday|
120+
faraday.options[:open_timeout] = 5
121+
faraday.options[:timeout] = 5
122+
123+
faraday.adapter :net_http_persistent
124+
end
120125
end
121126

122127
def source_slug

0 commit comments

Comments
 (0)