io_request 2.4.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 987e7926f719a3479bf88cc0bdc76701edbc35e3d77d3c25d0f6ff8009be971c
4
- data.tar.gz: 7ad37eab2d0718cdd100a9f16c7f272e538e39ae97939aa3e5858dbeb5f05413
3
+ metadata.gz: 53fdb5fa823faf9d379e6c2180f0d47f79959caa144dd1c34295c81226a4566d
4
+ data.tar.gz: 8614bb17e707782adc8d13ea01d0328bac2432658d0dc91977ee728107dcfcb7
5
5
  SHA512:
6
- metadata.gz: '089626846b0ce20d3cc9ae7c7dfeb9c4b457de3f3500cd75b7456a112e6f1f1304763a6288c4349b220c7b69c24ef5e4b5f34124235359184b5a7e5f34e50b66'
7
- data.tar.gz: 71e6692045f5752e5bd5eda60eeea755a8ac542f465ed6a19804569f5816a3599b615986a81793a8a4248a22000696ac4a362160efc246236c701beb2dcc0383
6
+ metadata.gz: 234a5e6d21fe3b1214a719af4b52617d4f58a3cf205e17cd8fa2d045098ada47bae1a272b8ec1daadfe5909831408e754d6a84085cad3705b5e09c8d3f940ccf
7
+ data.tar.gz: ef33270ed7cbf3b2082f7845d1ca1bbab2b3e77a145cff65ec4de361fb9b728535b3bd7b6d63bfef28713a216daea9434a6b908459fc3d7be1002142e90e3386
@@ -7,6 +7,9 @@ module IORequest
7
7
 
8
8
  # Authorization failed.
9
9
  class AuthorizationFailureError < RuntimeError; end
10
+
11
+ # Request timed out.
12
+ class RequestTimeoutError < RuntimeError; end
10
13
  end
11
14
 
12
15
  require_relative 'io_request/version'
@@ -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
- def request(data = {}, &callback)
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
- response.write_to(@io_w)
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
- response = @responses[request.id.to_s]
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
@@ -2,5 +2,5 @@
2
2
 
3
3
  module IORequest
4
4
  # Gem version.
5
- VERSION = '2.4.1'
5
+ VERSION = '2.5.0'
6
6
  end
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.1
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-23 00:00:00.000000000 Z
11
+ date: 2020-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler