ione-rpc 1.1.0.pre0 → 1.1.0.pre1

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: b76720f1019ef558e761ca6150d3e6417525f7e4
4
- data.tar.gz: e0e60b176ac1d29de164124a9db8090be4224e2c
3
+ metadata.gz: 4a15bae5d5ac949f0eb49613fcfaa8a55f9e376d
4
+ data.tar.gz: 318088075e05b2642ec5bc1e57948cc4daa667d4
5
5
  SHA512:
6
- metadata.gz: 843c6352df3a73996b9df2c110e4c9c259f85fa6dec03cda53619f69af220ba519aa17a11ef583aa111523a6dbe3e8fa09bfd987144dc7e3ee4b3809777b5afa
7
- data.tar.gz: 1ecb1ef66d1f4f17ca14dccc216cc21a9ecaec02eee64fe1bed4bf32229f44dd56616df562fc0af64fdd09ab79314b6a651cc5a8d1d21f431e5ac12b406c6dd6
6
+ metadata.gz: 83514557ca9c22a11f590dd48ccbeb389999d3f77086a539903429c7f3428eb838a2af01c531984aa273277bbc8516451d9415786f67bddb059ea775d9262f55
7
+ data.tar.gz: d7c39a4d45605ce7335085d55ed11aaa2ad471df50419f380254bdd3acc2b05cede92510921604d5afd38cab864599ed55e19ea4b59064ae1a5c15ab849cb89d
@@ -42,16 +42,20 @@ module Ione
42
42
  @max_channels = options[:max_channels] || 128
43
43
  @logger = options[:logger]
44
44
  @hosts = []
45
- @connections = []
45
+ @connections = [].freeze
46
46
  Array(options[:hosts]).each { |h| add_host(*h) }
47
47
  end
48
48
 
49
49
  # A client is connected when it has at least one open connection.
50
50
  def connected?
51
+ connections = nil
51
52
  @lock.lock
52
- @connections.any?
53
- ensure
54
- @lock.unlock
53
+ begin
54
+ connections = @connections
55
+ ensure
56
+ @lock.unlock
57
+ end
58
+ connections.any?
55
59
  end
56
60
 
57
61
  # Returns an array of info and statistics about the currently open connections.
@@ -99,7 +103,7 @@ module Ione
99
103
  # @return [Ione::Future<Ione::Rpc::Client>] a future that resolves to the
100
104
  # client when all connections have closed and the IO reactor has stopped.
101
105
  def stop
102
- @lock.synchronize { @connections = [] }
106
+ @lock.synchronize { @connections = [].freeze }
103
107
  @io_reactor.stop.map(self)
104
108
  end
105
109
 
@@ -186,12 +190,14 @@ module Ione
186
190
  if connection
187
191
  chosen_connection = connection
188
192
  else
193
+ connections = nil
189
194
  @lock.lock
190
195
  begin
191
- chosen_connection = choose_connection(@connections, request)
196
+ connections = @connections
192
197
  ensure
193
198
  @lock.unlock
194
199
  end
200
+ chosen_connection = choose_connection(connections, request)
195
201
  end
196
202
  if chosen_connection && !chosen_connection.closed?
197
203
  f = chosen_connection.send_message(request, timeout)
@@ -327,7 +333,7 @@ module Ione
327
333
  @logger.info('Connected to %s:%d' % [connection.host, connection.port]) if @logger
328
334
  connection.on_closed { |error| handle_disconnected(connection, error) }
329
335
  if connect?(connection.host, connection.port)
330
- @lock.synchronize { @connections << connection }
336
+ @lock.synchronize { @connections = (@connections + [connection]).freeze }
331
337
  else
332
338
  connection.close
333
339
  end
@@ -345,7 +351,7 @@ module Ione
345
351
  else
346
352
  @logger.info(message) if @logger
347
353
  end
348
- @lock.synchronize { @connections.delete(connection) }
354
+ @lock.synchronize { @connections = (@connections - [connection]).freeze }
349
355
  if error && reconnect?(connection.host, connection.port, 0)
350
356
  connect(connection.host, connection.port)
351
357
  else
@@ -11,12 +11,11 @@ module Ione
11
11
  raise ArgumentError, 'More than 2**15 channels is not supported' if max_channels > 2**15
12
12
  super(connection, codec, scheduler)
13
13
  @lock = Mutex.new
14
- @channels = [nil] * max_channels
15
- @queue = []
16
14
  @encode_eagerly = @codec.recoding?
17
15
  @sent_messages = 0
18
16
  @received_responses = 0
19
17
  @timeouts = 0
18
+ reset(max_channels)
20
19
  end
21
20
 
22
21
  def stats
@@ -25,7 +24,7 @@ module Ione
25
24
  :host => @host,
26
25
  :port => @port,
27
26
  :max_channels => @channels.size,
28
- :active_channels => @channels.size - @channels.count(nil),
27
+ :active_channels => @channels.size - @free_channels.size,
29
28
  :queued_messages => @queue.size,
30
29
  :sent_messages => @sent_messages,
31
30
  :received_responses => @received_responses,
@@ -82,6 +81,7 @@ module Ione
82
81
  begin
83
82
  promise = @channels[channel]
84
83
  @channels[channel] = nil
84
+ @free_channels << channel
85
85
  @received_responses += 1 if promise
86
86
  ensure
87
87
  @lock.unlock
@@ -117,7 +117,7 @@ module Ione
117
117
  end
118
118
 
119
119
  def take_channel(promise)
120
- if (channel = @channels.index(nil))
120
+ if (channel = @free_channels.pop)
121
121
  @channels[channel] = promise
122
122
  channel
123
123
  end
@@ -128,10 +128,9 @@ module Ione
128
128
  queued_promises = nil
129
129
  @lock.lock
130
130
  begin
131
- in_flight_promises = @channels.reject(&:nil?)
132
- @channels = [nil] * @channels.size
131
+ in_flight_promises = @channels.compact
133
132
  queued_promises = @queue.map(&:last)
134
- @queue = []
133
+ reset(@channels.size)
135
134
  ensure
136
135
  @lock.unlock
137
136
  end
@@ -143,6 +142,12 @@ module Ione
143
142
  queued_promises.each { |p| p.fail(error) }
144
143
  super
145
144
  end
145
+
146
+ def reset(max_channels)
147
+ @channels = [nil] * max_channels
148
+ @free_channels = (0...max_channels).to_a
149
+ @queue = []
150
+ end
146
151
  end
147
152
  end
148
153
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ione
4
4
  module Rpc
5
- VERSION = '1.1.0.pre0'.freeze
5
+ VERSION = '1.1.0.pre1'.freeze
6
6
  end
7
7
  end
@@ -104,10 +104,15 @@ module Ione
104
104
 
105
105
  it 'uses the next available channel' do
106
106
  peer.send_message('hello')
107
+ hello_channel = connection.written_bytes.rpartition('@').last
107
108
  peer.send_message('foo')
108
- connection.data_listener.call('world@0')
109
+ foo_channel = connection.written_bytes.rpartition('@').last
110
+ foo_channel.should_not eq(hello_channel)
111
+
112
+ connection.data_listener.call('world@' << hello_channel)
109
113
  peer.send_message('bar')
110
- connection.written_bytes.should == 'hello@000foo@001bar@000'
114
+ bar_channel = connection.written_bytes.rpartition('@').last
115
+ bar_channel.should eq(hello_channel)
111
116
  end
112
117
 
113
118
  it 'queues requests when all channels are in use' do
@@ -131,8 +136,9 @@ module Ione
131
136
 
132
137
  it 'returns a future that resolves to the response' do
133
138
  f = peer.send_message('foo')
139
+ foo_channel = connection.written_bytes.rpartition('@').last
134
140
  f.should_not be_resolved
135
- connection.data_listener.call('bar@000')
141
+ connection.data_listener.call('bar@' << foo_channel)
136
142
  f.value.payload.should == 'bar'
137
143
  end
138
144
 
@@ -144,14 +150,16 @@ module Ione
144
150
 
145
151
  it 'does not fail the request when the response is received before the timeout passes' do
146
152
  f = peer.send_message('foo', 2)
147
- connection.data_listener.call('bar@000')
153
+ foo_channel = connection.written_bytes.rpartition('@').last
154
+ connection.data_listener.call('bar@' << foo_channel)
148
155
  scheduler.timer_promises.first.fulfill
149
156
  expect { f.value }.to_not raise_error
150
157
  end
151
158
 
152
159
  it 'cancel the timeout timer when the response is received before the timeout passes' do
153
160
  f = peer.send_message('foo', 2)
154
- connection.data_listener.call('bar@000')
161
+ foo_channel = connection.written_bytes.rpartition('@').last
162
+ connection.data_listener.call('bar@' << foo_channel)
155
163
  scheduler.timer_promises.first.fulfill
156
164
  scheduler.should have_received(:cancel_timer).once
157
165
  end
@@ -86,8 +86,8 @@ shared_examples 'peers' do
86
86
 
87
87
  it 'writes the encoded frame to the connection' do
88
88
  peer.send_message('FUZZBAZZ')
89
- codec.should have_received(:encode).with('FUZZBAZZ', 0)
90
- connection.written_bytes.should == 'FUZZBAZZ@0'
89
+ codec.should have_received(:encode).with('FUZZBAZZ', kind_of(Fixnum))
90
+ connection.written_bytes.should match(/\AFUZZBAZZ@\d+\Z/)
91
91
  end
92
92
  end
93
93
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ione-rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0.pre0
4
+ version: 1.1.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo Hultberg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-12 00:00:00.000000000 Z
11
+ date: 2016-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ione