protein 0.4.0 → 0.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
  SHA1:
3
- metadata.gz: 481a92b147284621cdecd97ae82f7774b7b05763
4
- data.tar.gz: fa5e1a24c93ead9e7a59d444fa0a2d0aac6e862b
3
+ metadata.gz: 45f21a4d7185d3be24bb0125c0cc33102f64c0b0
4
+ data.tar.gz: 3db590ca179c16f5e5a7d7de7bc878a7734d56ed
5
5
  SHA512:
6
- metadata.gz: 965a2f137632fcaf66e8d3fa3ff1baf82b31b29739ea5e44c18c3a8564e932b1430db52ffdacdb0f9237298723e283368b43eb76cc3c41807aeddde40e3bad56
7
- data.tar.gz: b78b7951fd364b4ae595603ec9337e87fd53dc499b0e0f486d968c37221d7bd719ed64f03225f698b020aea1c5ded8fa3e0cd8bc83cbea733d76d1cb1f72d9c9
6
+ metadata.gz: 7679e7bc9a29e477a5f0a15f82e89dbaa775340cedfd6e831c9af4e690efa42370c81a6edda62fc14dc43fa2b6f3e7e5cf5767064d2b88d403534a9d316b23a7
7
+ data.tar.gz: a7cc6dfd6efed158af28a6a7c0edabbc5e3fd0de70f2979679735b18f02f369635e219eb331e975b011fe8e8cb441dbb319d790ce51636da84aed13fcae6c774
@@ -35,22 +35,30 @@ class AMQPAdapter
35
35
  end
36
36
 
37
37
  attr_reader :reply_queue
38
- attr_accessor :response, :call_id
39
- attr_reader :lock, :condition
38
+ attr_accessor :calls
40
39
 
41
40
  def call(request_payload)
42
41
  prepare_client
43
42
 
44
- @call_id = SecureRandom.uuid
43
+ call_id = SecureRandom.uuid
45
44
 
46
45
  @x.publish(request_payload,
47
- correlation_id: @call_id,
46
+ correlation_id: call_id,
48
47
  routing_key: @server_queue,
49
48
  reply_to: @reply_queue.name,
50
49
  expiration: timeout)
51
50
 
52
- self.response = nil
53
- lock.synchronize { condition.wait(lock, timeout && timeout * 0.001) }
51
+ call = Concurrent::Hash.new
52
+ mutex = Mutex.new
53
+ condition = ConditionVariable.new
54
+ call[:mutex] = mutex
55
+ call[:condition] = condition
56
+ calls[call_id] = call
57
+
58
+ mutex.synchronize { condition.wait(mutex, timeout && timeout * 0.001) }
59
+
60
+ response = call[:response]
61
+ calls.delete(call_id)
54
62
 
55
63
  if response == nil
56
64
  raise(TransportError, "timeout after #{timeout}ms")
@@ -70,7 +78,13 @@ class AMQPAdapter
70
78
 
71
79
  def serve(router)
72
80
  @conn = Bunny.new(url)
73
- @conn.start
81
+ begin
82
+ @conn.start
83
+ rescue Bunny::TCPConnectionFailed => e
84
+ Protein.logger.error "RPC server connection error: #{e.inspect}"
85
+ log_error(e)
86
+ raise(e)
87
+ end
74
88
  @ch = @conn.create_channel
75
89
  @ch.prefetch(1)
76
90
  @q = @ch.queue(queue)
@@ -96,15 +110,13 @@ class AMQPAdapter
96
110
  end
97
111
 
98
112
  @ch.ack(delivery_info.delivery_tag)
99
-
100
113
  if @error
101
- error_logger = Protein.config.error_logger
102
- error_logger.call(@error) if error_logger
103
-
104
- raise(@error)
114
+ log_error(@error)
115
+ raise(message)
105
116
  end
106
117
  end
107
118
  rescue StandardError => e
119
+ log_error(e)
108
120
  Protein.logger.error "RPC server error: #{e.inspect}, restarting the server in 5s..."
109
121
 
110
122
  sleep 5
@@ -114,6 +126,11 @@ class AMQPAdapter
114
126
 
115
127
  private
116
128
 
129
+ def log_error(error)
130
+ @error_logger ||= Protein.config.error_logger
131
+ @error_logger.call(error) if @error_logger
132
+ end
133
+
117
134
  def prepare_client
118
135
  return if @conn
119
136
 
@@ -123,15 +140,18 @@ class AMQPAdapter
123
140
  @x = @ch.default_exchange
124
141
  @server_queue = queue
125
142
  @reply_queue = @ch.queue("", exclusive: true)
126
- @lock = Mutex.new
127
- @condition = ConditionVariable.new
128
-
129
- that = self
143
+ @calls = Concurrent::Hash.new
130
144
 
131
145
  @reply_queue.subscribe do |delivery_info, properties, payload|
132
- if properties[:correlation_id] == that.call_id
133
- that.response = payload
134
- that.lock.synchronize{that.condition.signal}
146
+ call_id = properties[:correlation_id]
147
+ call = calls[call_id]
148
+
149
+ if call
150
+ mutex = call[:mutex]
151
+ condition = call[:condition]
152
+ call[:response] = payload
153
+
154
+ mutex.synchronize { condition.signal }
135
155
  end
136
156
  end
137
157
  end
@@ -1,3 +1,3 @@
1
1
  module Protein
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protein
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karol Słuszniak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-10 00:00:00.000000000 Z
11
+ date: 2018-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -16,20 +16,34 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: concurrent-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
20
34
  - - ">="
21
35
  - !ruby/object:Gem::Version
22
- version: 2.0.0
23
- type: :development
36
+ version: 1.0.5
37
+ type: :runtime
24
38
  prerelease: false
25
39
  version_requirements: !ruby/object:Gem::Requirement
26
40
  requirements:
27
41
  - - "~>"
28
42
  - !ruby/object:Gem::Version
29
- version: '2.0'
43
+ version: '1.0'
30
44
  - - ">="
31
45
  - !ruby/object:Gem::Version
32
- version: 2.0.0
46
+ version: 1.0.5
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: google-protobuf
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -37,9 +51,6 @@ dependencies:
37
51
  - - "~>"
38
52
  - !ruby/object:Gem::Version
39
53
  version: '3.0'
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 3.0.0
43
54
  type: :runtime
44
55
  prerelease: false
45
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -47,9 +58,6 @@ dependencies:
47
58
  - - "~>"
48
59
  - !ruby/object:Gem::Version
49
60
  version: '3.0'
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 3.0.0
53
61
  - !ruby/object:Gem::Dependency
54
62
  name: parallel
55
63
  requirement: !ruby/object:Gem::Requirement
@@ -57,9 +65,6 @@ dependencies:
57
65
  - - "~>"
58
66
  - !ruby/object:Gem::Version
59
67
  version: '1.0'
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: 1.0.0
63
68
  type: :runtime
64
69
  prerelease: false
65
70
  version_requirements: !ruby/object:Gem::Requirement
@@ -67,9 +72,6 @@ dependencies:
67
72
  - - "~>"
68
73
  - !ruby/object:Gem::Version
69
74
  version: '1.0'
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: 1.0.0
73
75
  description:
74
76
  email: karol@shedul.com
75
77
  executables: []