proco 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/proco/dispatcher.rb +3 -3
- data/lib/proco/queue/base.rb +22 -5
- data/lib/proco/queue/batch_queue.rb +2 -2
- data/lib/proco/queue/multi_queue.rb +1 -1
- data/lib/proco/queue/single_queue.rb +1 -1
- data/lib/proco/version.rb +1 -1
- data/test/test_queue.rb +4 -4
- metadata +1 -1
data/lib/proco/dispatcher.rb
CHANGED
@@ -11,11 +11,11 @@ class Dispatcher
|
|
11
11
|
@logger, interval, qs, batch, batch_size =
|
12
12
|
proco.options.values_at :logger, :interval, :queue_size, :batch, :batch_size
|
13
13
|
@queue = if batch && batch_size
|
14
|
-
Proco::Queue::BatchQueue.new(qs, batch_size)
|
14
|
+
Proco::Queue::BatchQueue.new(qs, batch_size, interval)
|
15
15
|
elsif batch
|
16
|
-
Proco::Queue::MultiQueue.new(qs)
|
16
|
+
Proco::Queue::MultiQueue.new(qs, interval)
|
17
17
|
else
|
18
|
-
Proco::Queue::SingleQueue.new(qs)
|
18
|
+
Proco::Queue::SingleQueue.new(qs, interval)
|
19
19
|
end
|
20
20
|
@pool = thread_pool
|
21
21
|
@block = block
|
data/lib/proco/queue/base.rb
CHANGED
@@ -12,11 +12,12 @@ class Base
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def initialize size
|
15
|
+
def initialize size, delay
|
16
16
|
super()
|
17
|
-
@size
|
18
|
-
@
|
19
|
-
@
|
17
|
+
@size = size
|
18
|
+
@delay = delay || 0
|
19
|
+
@items = []
|
20
|
+
@valid = true
|
20
21
|
end
|
21
22
|
|
22
23
|
def invalidate
|
@@ -40,10 +41,26 @@ class Base
|
|
40
41
|
|
41
42
|
def take
|
42
43
|
@mtx.lock
|
44
|
+
wait_at = nil
|
43
45
|
while true
|
44
46
|
empty = @items.empty?
|
45
|
-
|
47
|
+
unless empty
|
48
|
+
if wait_at && @delay > 0
|
49
|
+
n = Time.now
|
50
|
+
t = wait_at + @delay
|
51
|
+
t += @delay * ((n - t) / @delay).to_i if t < n
|
52
|
+
t += @delay if t < n
|
53
|
+
|
54
|
+
# Haven't took anything.
|
55
|
+
# No need to broadcast to blocked pushers
|
56
|
+
@mtx.unlock
|
57
|
+
sleep t - n
|
58
|
+
@mtx.lock
|
59
|
+
end
|
60
|
+
break
|
61
|
+
end
|
46
62
|
return nil unless @valid
|
63
|
+
wait_at = Time.now
|
47
64
|
@cv.wait @mtx
|
48
65
|
end
|
49
66
|
take_impl
|
data/lib/proco/version.rb
CHANGED
data/test/test_queue.rb
CHANGED
@@ -7,7 +7,7 @@ require 'lps'
|
|
7
7
|
|
8
8
|
class TestQueue < MiniTest::Unit::TestCase
|
9
9
|
def test_single_queue
|
10
|
-
q = Proco::Queue::SingleQueue.new 100
|
10
|
+
q = Proco::Queue::SingleQueue.new 100, nil
|
11
11
|
f = q.push 1
|
12
12
|
assert_instance_of Proco::Future, f
|
13
13
|
q.push 2
|
@@ -21,7 +21,7 @@ class TestQueue < MiniTest::Unit::TestCase
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_batch_queue
|
24
|
-
q = Proco::Queue::BatchQueue.new 100, 10
|
24
|
+
q = Proco::Queue::BatchQueue.new 100, 10, nil
|
25
25
|
|
26
26
|
futures = 10.times.map { |i| q.push i }
|
27
27
|
assert_equal 1, futures.uniq.length
|
@@ -43,7 +43,7 @@ class TestQueue < MiniTest::Unit::TestCase
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_multi_queue
|
46
|
-
q = Proco::Queue::MultiQueue.new 100
|
46
|
+
q = Proco::Queue::MultiQueue.new 100, nil
|
47
47
|
f1 = q.push 1
|
48
48
|
f2 = q.push 2
|
49
49
|
f3 = q.push 3
|
@@ -63,7 +63,7 @@ class TestQueue < MiniTest::Unit::TestCase
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def test_multi_queue_complex
|
66
|
-
queue = Proco::Queue::MultiQueue.new(200)
|
66
|
+
queue = Proco::Queue::MultiQueue.new(200, nil)
|
67
67
|
futures = []
|
68
68
|
num_batches = 0
|
69
69
|
num_items = 0
|