fiber_connection_pool 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/lib/fiber_connection_pool.rb +10 -15
- data/test/fiber_connection_pool_test.rb +11 -2
- metadata +2 -2
data/README.md
CHANGED
@@ -169,7 +169,7 @@ pool = FiberConnectionPool.new(:size => 5){ MyFancyConnection.new }
|
|
169
169
|
# add a request to save data for each successful call on a connection
|
170
170
|
# will save the return value inside a hash on the key ':affected_rows'
|
171
171
|
# and make it available for the fiber that made the call
|
172
|
-
pool.save_data(:affected_rows) do |connection|
|
172
|
+
pool.save_data(:affected_rows) do |connection, method, args|
|
173
173
|
connection.affected_rows
|
174
174
|
end
|
175
175
|
```
|
@@ -191,7 +191,7 @@ right after you made the query that generated it. But you could:
|
|
191
191
|
|
192
192
|
``` ruby
|
193
193
|
# save only the first run
|
194
|
-
pool.save_data(:affected_rows) do |connection|
|
194
|
+
pool.save_data(:affected_rows) do |connection, method, args|
|
195
195
|
pool.gathered_data[:affected_rows] || connection.affected_rows
|
196
196
|
end
|
197
197
|
```
|
@@ -2,7 +2,7 @@ require 'fiber'
|
|
2
2
|
require_relative 'fiber_connection_pool/exceptions'
|
3
3
|
|
4
4
|
class FiberConnectionPool
|
5
|
-
VERSION = '0.2.
|
5
|
+
VERSION = '0.2.5'
|
6
6
|
|
7
7
|
RESERVED_TTL_SECS = 30 # reserved cleanup trigger
|
8
8
|
SAVED_DATA_TTL_SECS = 30 # saved_data cleanup trigger
|
@@ -44,27 +44,22 @@ class FiberConnectionPool
|
|
44
44
|
# call to -any- method on the connection.
|
45
45
|
# The connection and the method name are passed to the block.
|
46
46
|
#
|
47
|
-
# The returned value will be saved in pool.
|
47
|
+
# The returned value will be saved in pool.gathered_data[key],
|
48
48
|
# and will be kept as long as the fiber stays alive.
|
49
49
|
#
|
50
50
|
# Ex:
|
51
51
|
#
|
52
52
|
# # (...right after pool's creation...)
|
53
|
-
# pool.save_data(:hey_or_hoo) do |conn, method|
|
53
|
+
# pool.save_data(:hey_or_hoo) do |conn, method, args|
|
54
54
|
# return 'hey' if method == 'query'
|
55
55
|
# 'hoo'
|
56
56
|
# end
|
57
57
|
#
|
58
58
|
# # (...from a reactor fiber...)
|
59
|
-
# myfiber = Fiber.current
|
60
59
|
# pool.query('select anything from anywhere')
|
61
|
-
# puts pool.
|
60
|
+
# puts pool.gathered_data[:hey_or_hoo]
|
62
61
|
# => 'hey'
|
63
62
|
#
|
64
|
-
# # (...eventually fiber dies...)
|
65
|
-
# puts pool.saved_data[myfiber].inspect
|
66
|
-
# => nil
|
67
|
-
#
|
68
63
|
def save_data(key, &block)
|
69
64
|
@save_data_requests[key] = block
|
70
65
|
end
|
@@ -100,7 +95,7 @@ class FiberConnectionPool
|
|
100
95
|
# Avoid method_missing stack for 'query'
|
101
96
|
#
|
102
97
|
def query(sql, *args)
|
103
|
-
execute('query') do |conn|
|
98
|
+
execute('query', args) do |conn|
|
104
99
|
conn.query sql, *args
|
105
100
|
end
|
106
101
|
end
|
@@ -152,7 +147,7 @@ class FiberConnectionPool
|
|
152
147
|
#
|
153
148
|
# After running the block, save requested data and release the connection.
|
154
149
|
#
|
155
|
-
def execute(method)
|
150
|
+
def execute(method, args)
|
156
151
|
f = Fiber.current
|
157
152
|
begin
|
158
153
|
# get a connection and use it
|
@@ -160,7 +155,7 @@ class FiberConnectionPool
|
|
160
155
|
retval = yield conn
|
161
156
|
|
162
157
|
# save anything requested
|
163
|
-
process_save_data(f, conn, method)
|
158
|
+
process_save_data(f, conn, method, args)
|
164
159
|
|
165
160
|
# successful run, release
|
166
161
|
release(f)
|
@@ -181,10 +176,10 @@ class FiberConnectionPool
|
|
181
176
|
# and save the data for the given fiber.
|
182
177
|
# Also perform cleanup if TTL is past
|
183
178
|
#
|
184
|
-
def process_save_data(fiber, conn, method)
|
179
|
+
def process_save_data(fiber, conn, method, args)
|
185
180
|
@save_data_requests.each do |key,block|
|
186
181
|
@saved_data[fiber] ||= {}
|
187
|
-
@saved_data[fiber][key] = block.call(conn, method)
|
182
|
+
@saved_data[fiber][key] = block.call(conn, method, args)
|
188
183
|
end
|
189
184
|
# try cleanup
|
190
185
|
save_data_cleanup if (Time.now - @last_data_cleanup) >= SAVED_DATA_TTL_SECS
|
@@ -227,7 +222,7 @@ class FiberConnectionPool
|
|
227
222
|
# waiting for IO, allowing the reactor run other fibers)
|
228
223
|
#
|
229
224
|
def method_missing(method, *args, &blk)
|
230
|
-
execute(method) do |conn|
|
225
|
+
execute(method, args) do |conn|
|
231
226
|
conn.send(method, *args, &blk)
|
232
227
|
end
|
233
228
|
end
|
@@ -242,8 +242,17 @@ class TestFiberConnectionPool < Minitest::Test
|
|
242
242
|
pool = FiberConnectionPool.new(:size => 2) { ::EMSynchronyConnection.new(:delay => 0.05) }
|
243
243
|
|
244
244
|
# ask to save some data
|
245
|
-
pool.save_data(:connection_id)
|
246
|
-
|
245
|
+
pool.save_data(:connection_id) do |conn, method, args|
|
246
|
+
assert_equal :do_something, method
|
247
|
+
assert_equal info, args.first # args is an array with the args given to the method
|
248
|
+
conn.object_id
|
249
|
+
end
|
250
|
+
|
251
|
+
pool.save_data(:fiber_id) do |conn, method, args|
|
252
|
+
assert_equal :do_something, method
|
253
|
+
assert_equal info, args.first
|
254
|
+
Fiber.current.object_id
|
255
|
+
end
|
247
256
|
|
248
257
|
fibers = Array.new(4) do
|
249
258
|
Fiber.new do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fiber_connection_pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-09-
|
13
|
+
date: 2013-09-24 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: minitest
|