io_request 2.4.1 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/io_request.rb +3 -0
- data/lib/io_request/client.rb +20 -9
- data/lib/io_request/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53fdb5fa823faf9d379e6c2180f0d47f79959caa144dd1c34295c81226a4566d
|
4
|
+
data.tar.gz: 8614bb17e707782adc8d13ea01d0328bac2432658d0dc91977ee728107dcfcb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 234a5e6d21fe3b1214a719af4b52617d4f58a3cf205e17cd8fa2d045098ada47bae1a272b8ec1daadfe5909831408e754d6a84085cad3705b5e09c8d3f940ccf
|
7
|
+
data.tar.gz: ef33270ed7cbf3b2082f7845d1ca1bbab2b3e77a145cff65ec4de361fb9b728535b3bd7b6d63bfef28713a216daea9434a6b908459fc3d7be1002142e90e3386
|
data/lib/io_request.rb
CHANGED
data/lib/io_request/client.rb
CHANGED
@@ -77,6 +77,7 @@ module IORequest
|
|
77
77
|
end
|
78
78
|
alias respond on_request
|
79
79
|
|
80
|
+
# Code to execute after connection is closed.
|
80
81
|
def on_close(&block)
|
81
82
|
IORequest.logger.debug(prog_name) { 'Saved on_close block' }
|
82
83
|
@on_close = block
|
@@ -84,17 +85,18 @@ module IORequest
|
|
84
85
|
|
85
86
|
# If callback block is provided, request will be sent asynchroniously.
|
86
87
|
# @param data [Hash]
|
87
|
-
|
88
|
+
# @param timeout [Integer] timeout in seconds.
|
89
|
+
def request(data, timeout: nil, &callback)
|
88
90
|
message = Message.new(data, type: :request)
|
89
91
|
|
90
92
|
if block_given?
|
91
93
|
# Async execution of request
|
92
94
|
in_thread(callback, name: 'requesting') do |cb|
|
93
|
-
cb.call(send_request_and_wait_for_response(message).data)
|
95
|
+
cb.call(send_request_and_wait_for_response(message, timeout).data)
|
94
96
|
end
|
95
97
|
nil
|
96
98
|
else
|
97
|
-
send_request_and_wait_for_response(message).data
|
99
|
+
send_request_and_wait_for_response(message, timeout).data
|
98
100
|
end
|
99
101
|
end
|
100
102
|
|
@@ -182,7 +184,11 @@ module IORequest
|
|
182
184
|
def send_response(response)
|
183
185
|
@mutex_w.synchronize do
|
184
186
|
IORequest.logger.debug(prog_name) { "Sending response: #{response}" }
|
185
|
-
|
187
|
+
begin
|
188
|
+
response.write_to(@io_w)
|
189
|
+
rescue IOError => e
|
190
|
+
IORequest.logger.debug(prog_name) { "Failed to write response message: #{e}" }
|
191
|
+
end
|
186
192
|
end
|
187
193
|
end
|
188
194
|
|
@@ -195,21 +201,26 @@ module IORequest
|
|
195
201
|
IORequest.logger.debug(prog_name) { "Failed to send zero-sized message(#{e})" }
|
196
202
|
end
|
197
203
|
|
198
|
-
def send_request_and_wait_for_response(request)
|
204
|
+
def send_request_and_wait_for_response(request, timeout)
|
199
205
|
@mutex_w.synchronize do
|
200
206
|
IORequest.logger.debug(prog_name) { "Sending message: #{request}" }
|
201
207
|
request.write_to(@io_w)
|
202
208
|
end
|
203
|
-
wait_for_response(request)
|
209
|
+
wait_for_response(request, timeout)
|
204
210
|
end
|
205
211
|
|
206
|
-
def wait_for_response(request)
|
212
|
+
def wait_for_response(request, timeout)
|
207
213
|
IORequest.logger.debug(prog_name) { "Waiting for response for #{request}" }
|
214
|
+
waiting_start_time = Time.now
|
208
215
|
@responses_access_mutex.synchronize do
|
209
216
|
response = nil
|
210
217
|
until response
|
211
|
-
@responses_access_cv.wait(@responses_access_mutex)
|
212
|
-
|
218
|
+
@responses_access_cv.wait(@responses_access_mutex, 1)
|
219
|
+
if @responses_access_mutex.owned?
|
220
|
+
# NOTE: Only accessing responses hash if thread owns access mutex
|
221
|
+
response = @responses.delete(request.id.to_s)
|
222
|
+
end
|
223
|
+
raise RequestTimeoutError if timeout && (Time.now - waiting_start_time < timeout)
|
213
224
|
end
|
214
225
|
IORequest.logger.debug(prog_name) { "Found response: #{response}" }
|
215
226
|
response
|
data/lib/io_request/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: io_request
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fizvlad
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|