delayer 1.1.1 → 1.1.2

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
  SHA256:
3
- metadata.gz: a2b9095d6ff8a3d46813d7ca787b7ffcf13050de5a21de2ba8b63940a9394631
4
- data.tar.gz: 1da28faf0ed99c6f87450c85c469b967ca2cdc5dc08e5bcbb629d050f1d5aa66
3
+ metadata.gz: 00c64707cef5f0715d026e9966b1a9d0b7e9676cad1882f73a145574f27d6352
4
+ data.tar.gz: ac42bd9584e379c7d565eca7a79ef168266480dcf9594dee461d119e46b3066b
5
5
  SHA512:
6
- metadata.gz: 22cac588a6463302883fbac1ada84b6922cf84076f854c2e75757ea495ac71569b133a0955dc131ca582d08b3b175beca6fb184e778975eda172ff5846747d7b
7
- data.tar.gz: fc1cf72aae866f8d832f9f856b44dc7dd2e29e26459715800ba4d30ce8a503e782e89e86e302b0629b73f55049a083eeba60030466290f6fef376bb38f1f78b8
6
+ metadata.gz: d94ccb45deebd4d2c16d0f96e107da98297c53d691a1c9ebf9eb6c234e327b9e72da07576e6d3b024969dce8e3ace8ecd9366cfb9b8a65e257994b3fdd5d4765
7
+ data.tar.gz: c208b40f6e1e0166aea86ab71c25b1fdb74e8412771724ac008800173eebb7396bea0c4c3665afc38e672cdd4a5fa2828aa05645eb6a27c9c909a76a581821d6
@@ -0,0 +1,40 @@
1
+ version: '2.1'
2
+
3
+ executors:
4
+ ruby:
5
+ parameters:
6
+ tag:
7
+ type: string
8
+ docker:
9
+ - image: circleci/ruby:<< parameters.tag >>
10
+
11
+ jobs:
12
+ build:
13
+ parameters:
14
+ ruby-version:
15
+ type: string
16
+ executor:
17
+ name: ruby
18
+ tag: << parameters.ruby-version >>
19
+ steps:
20
+ - checkout
21
+ - run:
22
+ name: Which bundler?
23
+ command: bundle -v
24
+ - run:
25
+ command: bundle install --path vendor/bundle
26
+ - run:
27
+ name: test
28
+ command: bundle exec rake test
29
+ workflows:
30
+ build:
31
+ jobs:
32
+ - build:
33
+ name: 'ruby-2.5'
34
+ ruby-version: '2.5.7'
35
+ - build:
36
+ name: 'ruby-2.6'
37
+ ruby-version: '2.6.5'
38
+ - build:
39
+ name: 'ruby-2.7'
40
+ ruby-version: '2.7.0'
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Delayer
2
2
 
3
+ [![toshia](https://circleci.com/gh/toshia/delayer.svg?style=svg)](https://circleci.com/gh/toshia/delayer)
4
+
3
5
  Delay Any task. Similar priority-queue.
4
6
 
5
7
  ## Installation
@@ -33,8 +33,12 @@ module Delayer
33
33
  end
34
34
  end
35
35
 
36
- def method_missing(*args, &proc)
37
- (@default ||= generate_class).__send__(*args, &proc)
36
+ def method_missing(*args, **kwrest, &proc)
37
+ if kwrest.empty?
38
+ (@default ||= generate_class).__send__(*args, &proc)
39
+ else
40
+ (@default ||= generate_class).__send__(*args, **kwrest, &proc)
41
+ end
38
42
  end
39
43
  end
40
44
  end
@@ -4,8 +4,7 @@ module Delayer
4
4
  class DelayedProcedure
5
5
  include Comparable
6
6
 
7
- attr_reader :state, :delayer, :reserve_at, :right
8
-
7
+ attr_reader :state, :delayer, :reserve_at
9
8
  def initialize(delayer, delay:, &proc)
10
9
  @delayer = delayer
11
10
  @proc = proc
@@ -15,42 +14,11 @@ module Delayer
15
14
  else
16
15
  @reserve_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + delay.to_f
17
16
  end
18
- @left = @right = @cancel = nil
19
- @size = 1
17
+ @cancel = nil
20
18
  @procedure = nil
21
19
  @delayer.class.reserve(self)
22
20
  end
23
21
 
24
- def size
25
- @size
26
- end
27
-
28
- def add(other)
29
- return self unless other
30
- if self >= other
31
- other.add(self)
32
- else
33
- @left, *children, @right = [@left, @right, other].compact.sort
34
- child = children.first
35
- if child
36
- if @right.right
37
- @left = @left.add(child)
38
- else
39
- @right = @right.add(child)
40
- end
41
- end
42
- @size += 1
43
- self
44
- end
45
- end
46
-
47
- def next
48
- @left&.add(@right).tap do
49
- @left = @right = nil
50
- freeze
51
- end
52
- end
53
-
54
22
  def register
55
23
  if !canceled?
56
24
  @procedure = Procedure.new(@delayer, &@proc)
@@ -50,9 +50,22 @@ module Delayer
50
50
  @remain_hook = nil
51
51
  @exception = nil
52
52
  @remain_received = false
53
- @lock = Mutex.new
53
+ @lock = Monitor.new
54
54
  @bucket = Bucket.new(nil, nil, {}, nil)
55
- @reserve = nil
55
+ @last_reserve = nil
56
+ @reserves = Set.new
57
+ end
58
+ end
59
+
60
+ def pop_reserve(start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC))
61
+ if @last_reserve&.reserve_at&.<=(start_time)
62
+ lock.synchronize do
63
+ while @last_reserve&.reserve_at&.<=(start_time)
64
+ @last_reserve.register
65
+ @last_reserve = @reserves.min
66
+ @reserves.delete(@last_reserve)
67
+ end
68
+ end
56
69
  end
57
70
  end
58
71
 
@@ -63,15 +76,12 @@ module Delayer
63
76
  # self
64
77
  def run(current_expire = @expire)
65
78
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
66
- while @reserve&.reserve_at&.<=(start_time)
67
- @reserve.register
68
- @reserve = @reserve.next
69
- end
79
+ pop_reserve(start_time)
70
80
  if current_expire == 0
71
- run_once until empty?
81
+ run_once_without_pop_reserve until empty?
72
82
  else
73
83
  @end_time = start_time + @expire
74
- run_once while !empty? && (@end_time >= Process.clock_gettime(Process::CLOCK_MONOTONIC))
84
+ run_once_without_pop_reserve while !empty? && (@end_time >= Process.clock_gettime(Process::CLOCK_MONOTONIC))
75
85
  @end_time = nil
76
86
  end
77
87
  if @remain_hook
@@ -95,6 +105,11 @@ module Delayer
95
105
  # ==== Return
96
106
  # self
97
107
  def run_once
108
+ pop_reserve
109
+ run_once_without_pop_reserve
110
+ end
111
+
112
+ private def run_once_without_pop_reserve
98
113
  if @bucket.first
99
114
  @busy = true
100
115
  procedure = forward
@@ -165,10 +180,15 @@ module Delayer
165
180
  # self
166
181
  def reserve(procedure)
167
182
  lock.synchronize do
168
- if @reserve
169
- @reserve = @reserve.add(procedure)
183
+ if @last_reserve
184
+ if @last_reserve > procedure
185
+ @reserves.add(@last_reserve)
186
+ @last_reserve = procedure
187
+ else
188
+ @reserves.add(procedure)
189
+ end
170
190
  else
171
- @reserve = procedure
191
+ @last_reserve = procedure
172
192
  end
173
193
  end
174
194
  self
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Delayer
4
- VERSION = '1.1.1'
4
+ VERSION = '1.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delayer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshiaki Asai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-06 00:00:00.000000000 Z
11
+ date: 2020-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,6 +65,7 @@ executables: []
65
65
  extensions: []
66
66
  extra_rdoc_files: []
67
67
  files:
68
+ - ".circleci/config.yml"
68
69
  - ".gitignore"
69
70
  - Gemfile
70
71
  - LICENSE.txt