deadline_machines 0.1.3 → 0.2.0

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.
@@ -1,183 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DeadlineMachines
4
- class Deadline
5
- NS_PER_SECOND = 1_000_000_000
6
-
7
- attr_reader :expires_at_ns, :parent, :clock, :reason
8
-
9
- class << self
10
- def after(seconds, clock: DeadlineMachines.clock)
11
- new(expires_at_ns: clock.now_ns + to_ns(seconds), clock: clock)
12
- end
13
-
14
- def never(clock: DeadlineMachines.clock)
15
- new(expires_at_ns: nil, clock: clock)
16
- end
17
-
18
- def to_ns(seconds)
19
- (seconds.to_r * NS_PER_SECOND).round
20
- end
21
- end
22
-
23
- def initialize(expires_at_ns:, clock:, parent: nil)
24
- @expires_at_ns = expires_at_ns
25
- @clock = clock
26
- @parent = parent
27
- @cancelled = false
28
- @detached = false
29
- @reason = nil
30
- @children = ObjectSpace::WeakMap.new
31
- @retained = {}
32
- @callbacks = []
33
- @mutex = parent ? parent.mutex : Mutex.new
34
- end
35
-
36
- def remaining_ns
37
- return nil unless @expires_at_ns
38
-
39
- left = @expires_at_ns - @clock.now_ns
40
- left.negative? ? 0 : left
41
- end
42
-
43
- def remaining
44
- ns = remaining_ns
45
- ns ? ns / NS_PER_SECOND.to_f : Float::INFINITY
46
- end
47
-
48
- def expired?
49
- remaining_ns == 0
50
- end
51
-
52
- def cancelled?
53
- @cancelled || !!@parent&.cancelled?
54
- end
55
-
56
- def done?
57
- cancelled? || expired?
58
- end
59
-
60
- def check!
61
- raise Cancelled.new(self, cancel_reason) if cancelled?
62
- raise Exceeded, self if expired?
63
-
64
- self
65
- end
66
-
67
- def clamp_sleep(seconds)
68
- left = remaining
69
- seconds < left ? seconds : left
70
- end
71
-
72
- def cancel(reason = nil)
73
- callbacks, children = @mutex.synchronize do
74
- return self if @cancelled
75
-
76
- @cancelled = true
77
- @reason = reason
78
- @parent&.drop(self)
79
- [@callbacks.dup, @children.keys]
80
- end
81
-
82
- failures = []
83
- children.each { |child| notify(failures) { child.cancel(reason) } }
84
- callbacks.each { |callback| notify(failures) { callback.call(self) } }
85
- raise failures.first unless failures.empty?
86
-
87
- self
88
- end
89
-
90
- def on_cancel(&block)
91
- already = @mutex.synchronize do
92
- settled = cancelled?
93
- unless settled
94
- @callbacks << block
95
- @parent&.hold(self) unless @detached
96
- end
97
- settled
98
- end
99
-
100
- block.call(self) if already
101
- self
102
- end
103
-
104
- def derive(seconds = nil)
105
- child = self.class.new(expires_at_ns: child_expiry_ns(seconds), clock: @clock, parent: self)
106
- @mutex.synchronize { @children[child] = true }
107
- return child unless block_given?
108
-
109
- begin
110
- yield child
111
- ensure
112
- @mutex.synchronize { detach(child) }
113
- end
114
- end
115
-
116
- def inspect
117
- state = cancelled? ? 'cancelled' : (expired? ? 'expired' : format('%.3fs left', remaining))
118
- "#<#{self.class} #{state}>"
119
- end
120
-
121
- protected
122
-
123
- attr_reader :mutex
124
- attr_writer :detached
125
-
126
- # A derived deadline is the caller's to keep, so the parent follows it
127
- # weakly. Something to deliver is the exception, and it has to be held all
128
- # the way to the root: an intermediate deadline nobody else references is
129
- # the one link whose collection would strand the delivery below it.
130
- #
131
- # Holding and dropping walk the chain, so they cannot be made atomic out of
132
- # one lock per link: every deadline in a tree shares the root's lock, and
133
- # both run inside a section the caller already holds.
134
- def hold(child)
135
- @retained[child] = true
136
- @parent.hold(self) if @parent && !@detached
137
- end
138
-
139
- def drop(child)
140
- @retained.delete(child)
141
- @parent.drop(self) if @parent && !pending?
142
- end
143
-
144
- # A scope that has ended owns its child outright: the tree will not reach
145
- # it again, so the parent holds nothing for it and an observer registered
146
- # afterwards is the caller's own to keep alive.
147
- def detach(child)
148
- @children.delete(child)
149
- child.detached = true
150
- drop(child)
151
- end
152
-
153
- # Whether anything is still owed through this deadline.
154
- def pending?
155
- !@cancelled && !(@callbacks.empty? && @retained.empty?)
156
- end
157
-
158
- private
159
-
160
- # Every observer and descendant is told, then the first failure is raised.
161
- # One raising observer must not silence its peers.
162
- def notify(failures)
163
- yield
164
- rescue StandardError => e
165
- failures << e
166
- end
167
-
168
- protected
169
-
170
- def cancel_reason
171
- @reason || @parent&.cancel_reason
172
- end
173
-
174
- private
175
-
176
- def child_expiry_ns(seconds)
177
- return @expires_at_ns unless seconds
178
-
179
- candidate = @clock.now_ns + self.class.to_ns(seconds)
180
- @expires_at_ns ? [candidate, @expires_at_ns].min : candidate
181
- end
182
- end
183
- end