freddy 0.5.2 → 0.5.3

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
  SHA1:
3
- metadata.gz: 87c1ea2e30dfb3705596a40af65a95c4d722ad41
4
- data.tar.gz: 60c14e4980b8e72cf131cb6161ebf8a6b632157a
3
+ metadata.gz: a2496631f882b6eb9b44c6249eea9426415a6af2
4
+ data.tar.gz: 9eed09d9bce769a4fe87799b9517c26f0a606d0e
5
5
  SHA512:
6
- metadata.gz: 68b0c2b6aa8b41ec71ead2d99e01be93e5e686aec774a6c99313e0d227196b0d56336ef255858c41916f35b72cdb946ff945723c02a270df898c13ad8f9a1ee4
7
- data.tar.gz: 3f4a430ef425723992644418e3a526cf51bc6cca1abe7ef3043f0d9569d84953c20240541a384ddfba92ee93596e2d9ad06b7e1a9458fe26a7095b18ecfbdc47
6
+ metadata.gz: 18e9eedeb5afdd6fbd2ae9256871e11e04b2850a086648c7584a3478e570c2720bdd39c9b24441c026994b2cf0f7708bb010c115690ba8422e0db70c9859d2a9
7
+ data.tar.gz: 2578e475defeb9b2ca3fe194f9bdf0881ebc0b7c10c74f3cce216f2b92d5cddef428ec12617db9308dc472894dbeb804ef798a38c05a5ceeec5503a6955b2f6d
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  else
9
9
  spec.name = "freddy"
10
10
  end
11
- spec.version = '0.5.2'
11
+ spec.version = '0.5.3'
12
12
  spec.authors = ["Urmas Talimaa"]
13
13
  spec.email = ["urmas.talimaa@gmail.com"]
14
14
  spec.description = %q{Messaging API}
@@ -17,7 +17,6 @@ class Freddy
17
17
  end
18
18
 
19
19
  @response_queue = @channel.queue("", exclusive: true)
20
- @request_manager.start
21
20
 
22
21
  @response_consumer = Consumers::ResponseConsumer.new(@logger)
23
22
  @response_consumer.consume(@response_queue, &method(:handle_response))
@@ -26,20 +25,30 @@ class Freddy
26
25
  def produce(destination, payload, properties)
27
26
  timeout_in_seconds = properties.fetch(:timeout_in_seconds)
28
27
  container = SyncResponseContainer.new
29
- async_request destination, payload, properties, &container
30
- container.wait_for_response(timeout_in_seconds + 0.1)
28
+ async_request destination, payload, container, properties
29
+ container.wait_for_response(timeout_in_seconds)
31
30
  end
32
31
 
33
32
  private
34
33
 
35
- def async_request(destination, payload, timeout_in_seconds:, delete_on_timeout:, **properties, &block)
34
+ def async_request(destination, payload, container, timeout_in_seconds:, delete_on_timeout:, **properties)
36
35
  correlation_id = SecureRandom.uuid
36
+
37
37
  @request_manager.store(correlation_id,
38
- callback: block,
39
- destination: destination,
40
- expires_at: Time.now + timeout_in_seconds
38
+ callback: container,
39
+ destination: destination
41
40
  )
42
41
 
42
+ container.on_timeout do
43
+ @logger.warn "Request timed out waiting response from #{destination}, correlation id #{correlation_id}"
44
+ Utils.notify 'RequestTimeout', "Request timed out waiting for response from #{destination}", {
45
+ correlation_id: correlation_id,
46
+ destination: destination,
47
+ timeout_in_seconds: timeout_in_seconds
48
+ }
49
+ @request_manager.delete(correlation_id)
50
+ end
51
+
43
52
  if delete_on_timeout
44
53
  properties[:expiration] = (timeout_in_seconds * 1000).to_i
45
54
  end
@@ -1,20 +1,10 @@
1
1
  class Freddy
2
2
  class RequestManager
3
-
4
3
  def initialize(logger)
5
4
  @requests = Hamster.mutable_hash
6
5
  @logger = logger
7
6
  end
8
7
 
9
- def start
10
- @timeout_thread = Thread.new do
11
- while true do
12
- clear_timeouts Time.now
13
- sleep 0.05
14
- end
15
- end
16
- end
17
-
18
8
  def no_route(correlation_id)
19
9
  if request = @requests[correlation_id]
20
10
  @requests.delete correlation_id
@@ -29,26 +19,5 @@ class Freddy
29
19
  def delete(correlation_id)
30
20
  @requests.delete(correlation_id)
31
21
  end
32
-
33
- private
34
-
35
- def clear_timeouts(now)
36
- @requests.each do |key, value|
37
- timeout(key, value) if now > value[:expires_at]
38
- end
39
- end
40
-
41
- def timeout(correlation_id, request)
42
- @requests.delete correlation_id
43
-
44
- @logger.warn "Request timed out waiting response from #{request[:destination]}, correlation id #{correlation_id}"
45
- Utils.notify 'RequestTimeout', "Request timed out waiting for response from #{request[:destination]}", {
46
- correlation_id: correlation_id,
47
- destination: request[:destination],
48
- expires_at: request[:expires_at]
49
- }
50
-
51
- request[:callback].call({error: 'RequestTimeout', message: 'Timed out waiting for response'}, nil)
52
- end
53
22
  end
54
23
  end
@@ -13,6 +13,10 @@ class Freddy
13
13
  @mutex.synchronize { @waiting.wakeup }
14
14
  end
15
15
 
16
+ def on_timeout(&block)
17
+ @on_timeout = block
18
+ end
19
+
16
20
  def wait_for_response(timeout)
17
21
  @mutex.synchronize do
18
22
  @waiting = Thread.current
@@ -20,22 +24,18 @@ class Freddy
20
24
  end
21
25
 
22
26
  if !defined?(@response)
23
- raise Timeout::Error, 'execution expired'
27
+ @on_timeout.call
28
+ raise TimeoutError.new(
29
+ error: 'RequestTimeout',
30
+ message: 'Timed out waiting for response'
31
+ )
24
32
  elsif @response.nil?
25
33
  raise StandardError, 'unexpected nil value for response'
26
- elsif @response[:error] == 'RequestTimeout'
27
- raise TimeoutError.new(@response)
28
34
  elsif !@delivery || @delivery.type == 'error'
29
35
  raise InvalidRequestError.new(@response)
30
36
  else
31
37
  @response
32
38
  end
33
39
  end
34
-
35
- private
36
-
37
- def to_proc
38
- Proc.new {|*args| self.call(*args)}
39
- end
40
40
  end
41
41
  end
@@ -3,11 +3,21 @@ require 'spec_helper'
3
3
  describe Freddy::SyncResponseContainer do
4
4
  let(:container) { described_class.new }
5
5
 
6
+ before do
7
+ container.on_timeout {}
8
+ end
9
+
6
10
  context 'when timeout' do
7
11
  subject { container.wait_for_response(0.01) }
8
12
 
9
13
  it 'raises timeout error' do
10
- expect { subject }.to raise_error(Timeout::Error, 'execution expired')
14
+ expect { subject }.to raise_error do |error|
15
+ expect(error).to be_a(Freddy::TimeoutError)
16
+ expect(error.response).to eq(
17
+ error: 'RequestTimeout',
18
+ message: 'Timed out waiting for response'
19
+ )
20
+ end
11
21
  end
12
22
  end
13
23
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freddy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Urmas Talimaa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-08 00:00:00.000000000 Z
11
+ date: 2016-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler