proco 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
@@ -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 = size
18
- @items = []
19
- @valid = true
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
- break unless empty
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
@@ -4,8 +4,8 @@ class Proco
4
4
  module Queue
5
5
  # @private
6
6
  class BatchQueue < Proco::Queue::Base
7
- def initialize size, batch_size
8
- super size
7
+ def initialize size, batch_size, delay
8
+ super size, delay
9
9
  @futures = []
10
10
  @batch_size = batch_size
11
11
  end
@@ -4,7 +4,7 @@ class Proco
4
4
  module Queue
5
5
  # @private
6
6
  class MultiQueue < Proco::Queue::Base
7
- def initialize size
7
+ def initialize size, delay
8
8
  super
9
9
  @future = Future.new
10
10
  end
@@ -4,7 +4,7 @@ class Proco
4
4
  module Queue
5
5
  # @private
6
6
  class SingleQueue < Proco::Queue::Base
7
- def initialize size
7
+ def initialize size, delay
8
8
  super
9
9
  end
10
10
 
@@ -1,3 +1,3 @@
1
1
  class Proco
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: