ztimer 0.4.0 → 0.4.1
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.
- checksums.yaml +4 -4
- data/lib/ztimer/version.rb +1 -1
- data/lib/ztimer/watcher.rb +20 -13
- data/lib/ztimer.rb +6 -7
- data/ztimer.gemspec +0 -2
- metadata +4 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 121c5eba1e94fd6be30153011569eda98500a497
|
4
|
+
data.tar.gz: 084133c7d9e7ab4dad366c21107d494aa452f20b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 878684872b565c899f5963970fad9604057f769864f1ffee747612a471d7b2029081463b66c8d0c9ad3c668409056c64853b60e03c974b4dd108ae198d7b87f0
|
7
|
+
data.tar.gz: 5fe4d73a56bf09c3ea73d6be5c0eed9620a19509f5914f79b1624ec658ee882c5c24882b2b4dd130182e9d8056b9d881454222fc6b7a279cab2ad33f5de2ff48
|
data/lib/ztimer/version.rb
CHANGED
data/lib/ztimer/watcher.rb
CHANGED
@@ -4,11 +4,9 @@ module Ztimer
|
|
4
4
|
|
5
5
|
def initialize(&callback)
|
6
6
|
@thread = nil
|
7
|
-
@idler = Lounger.new
|
8
7
|
@slots = Ztimer::SortedStore.new
|
9
8
|
@callback = callback
|
10
9
|
@lock = Mutex.new
|
11
|
-
@metric = Hitimes::Metric.new("Notifier")
|
12
10
|
@mutex = Mutex.new
|
13
11
|
end
|
14
12
|
|
@@ -29,7 +27,8 @@ module Ztimer
|
|
29
27
|
|
30
28
|
def run
|
31
29
|
if @thread
|
32
|
-
@
|
30
|
+
@thread.wakeup
|
31
|
+
@thread.run
|
33
32
|
else
|
34
33
|
start
|
35
34
|
end
|
@@ -40,15 +39,19 @@ module Ztimer
|
|
40
39
|
return if @thread
|
41
40
|
@thread = Thread.new do
|
42
41
|
loop do
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
42
|
+
begin
|
43
|
+
delay = get_delay
|
44
|
+
if delay.nil?
|
45
|
+
Thread.stop
|
46
|
+
next
|
47
|
+
end
|
48
48
|
|
49
|
-
|
49
|
+
select(nil, nil, nil, delay / 1_000_000.to_f) if delay > 1 # 1 microsecond of cranularity
|
50
50
|
|
51
|
-
|
51
|
+
while get_first_expired do
|
52
|
+
end
|
53
|
+
rescue => e
|
54
|
+
puts e.inspect + "\n" + e.backtrace.join("\n")
|
52
55
|
end
|
53
56
|
end
|
54
57
|
end
|
@@ -57,15 +60,15 @@ module Ztimer
|
|
57
60
|
end
|
58
61
|
|
59
62
|
def get_delay
|
60
|
-
return @mutex.synchronize { @slots.empty? ? nil : @slots.first.expires_at -
|
63
|
+
return @mutex.synchronize { @slots.empty? ? nil : @slots.first.expires_at - utc_microseconds }
|
61
64
|
end
|
62
65
|
|
63
66
|
def get_first_expired
|
64
67
|
@mutex.synchronize do
|
65
68
|
slot = @slots.first
|
66
|
-
if slot && (slot.expires_at <
|
69
|
+
if slot && (slot.expires_at < utc_microseconds)
|
67
70
|
@slots.shift
|
68
|
-
slot.started_at =
|
71
|
+
slot.started_at = utc_microseconds
|
69
72
|
unless slot.canceled?
|
70
73
|
execute(slot)
|
71
74
|
if slot.recurrent?
|
@@ -84,5 +87,9 @@ module Ztimer
|
|
84
87
|
def execute(slot)
|
85
88
|
@callback.call(slot)
|
86
89
|
end
|
90
|
+
|
91
|
+
def utc_microseconds
|
92
|
+
return Time.now.to_f * 1_000_000
|
93
|
+
end
|
87
94
|
end
|
88
95
|
end
|
data/lib/ztimer.rb
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
require 'set'
|
2
|
-
require 'hitimes'
|
3
|
-
require 'lounger'
|
4
|
-
|
5
1
|
require "ztimer/version"
|
6
2
|
require "ztimer/slot"
|
7
3
|
require "ztimer/sorted_store"
|
@@ -10,7 +6,6 @@ require "ztimer/watcher"
|
|
10
6
|
module Ztimer
|
11
7
|
@concurrency = 20
|
12
8
|
@watcher = Ztimer::Watcher.new(){|slot| execute(slot) }
|
13
|
-
@metric = Hitimes::Metric.new("Notifier")
|
14
9
|
@workers_lock = Mutex.new
|
15
10
|
@queue = Queue.new
|
16
11
|
@running = 0
|
@@ -20,7 +15,7 @@ module Ztimer
|
|
20
15
|
attr_reader :concurrency, :running, :count
|
21
16
|
|
22
17
|
def after(milliseconds, &callback)
|
23
|
-
enqueued_at =
|
18
|
+
enqueued_at = utc_microseconds
|
24
19
|
expires_at = enqueued_at + milliseconds * 1000
|
25
20
|
slot = Slot.new(enqueued_at, expires_at, -1, &callback)
|
26
21
|
|
@@ -65,7 +60,7 @@ module Ztimer
|
|
65
60
|
worker = Thread.new do
|
66
61
|
begin
|
67
62
|
while !@queue.empty? && @queue.pop(true) do
|
68
|
-
slot.executed_at =
|
63
|
+
slot.executed_at = utc_microseconds
|
69
64
|
slot.callback.call(slot) unless slot.callback.nil?
|
70
65
|
end
|
71
66
|
rescue ThreadError
|
@@ -80,5 +75,9 @@ module Ztimer
|
|
80
75
|
end
|
81
76
|
end
|
82
77
|
end
|
78
|
+
|
79
|
+
def utc_microseconds
|
80
|
+
return Time.now.to_f * 1_000_000
|
81
|
+
end
|
83
82
|
end
|
84
83
|
end
|
data/ztimer.gemspec
CHANGED
@@ -22,6 +22,4 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.11"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
-
spec.add_dependency "hitimes", "~> 1.2"
|
26
|
-
spec.add_dependency "lounger", "~> 0.2"
|
27
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ztimer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Groza Sergiu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,34 +52,6 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: hitimes
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '1.2'
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '1.2'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: lounger
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0.2'
|
76
|
-
type: :runtime
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0.2'
|
83
55
|
description: Ruby asyncrhonous timer that allows you to enqueue tasks to be executed
|
84
56
|
asyncrhonously after a delay
|
85
57
|
email:
|
@@ -123,8 +95,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
95
|
version: '0'
|
124
96
|
requirements: []
|
125
97
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.4.
|
98
|
+
rubygems_version: 2.4.6
|
127
99
|
signing_key:
|
128
100
|
specification_version: 4
|
129
101
|
summary: An asyncrhonous timer
|
130
102
|
test_files: []
|
103
|
+
has_rdoc:
|