deadline_machines 0.1.1 → 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.
@@ -2,23 +2,57 @@ module DeadlineMachines
2
2
  VERSION: String
3
3
  FIBER_KEY: Symbol
4
4
 
5
- # Any monotonic nanosecond time source.
5
+ # :exceeded, :cancelled: the core's Refusal, in declaration order.
6
+ REFUSALS: Array[refusal]
7
+
8
+ type refusal = :exceeded | :cancelled
9
+ type cancellation = :propagated | :already_cancelled
10
+
11
+ # Any monotonic clock in Integer nanoseconds.
6
12
  interface _Clock
7
13
  def now_ns: () -> Integer
8
14
  end
9
15
 
16
+ # When a budget runs out, Integer nanoseconds or nil for never. Native and
17
+ # validated through the core.
18
+ class Deadline < Data
19
+ attr_reader expires_at: Integer?
20
+
21
+ def self.new: (Integer? expires_at) -> Deadline
22
+ | (expires_at: Integer?) -> Deadline
23
+ def self.[]: (Integer? expires_at) -> Deadline
24
+ | (expires_at: Integer?) -> Deadline
25
+ def self.members: () -> [:expires_at]
26
+
27
+ def self.never: () -> Deadline
28
+ def self.at: (Integer instant) -> Deadline
29
+ def self.after: (Integer now, Integer timeout) -> Deadline
30
+
31
+ def initialize: (expires_at: Integer?) -> void
32
+
33
+ # nil when there is no limit.
34
+ def remaining: (Integer now) -> Integer?
35
+ def expired?: (Integer now) -> bool
36
+ # nil while live, :exceeded at or past expires_at.
37
+ def check: (Integer now) -> (:exceeded | nil)
38
+ def derive: (Integer now, Integer timeout) -> Deadline
39
+ def extend_by: (Integer by) -> Deadline
40
+ def tighten: (Deadline other) -> Deadline
41
+ def clamp_sleep: (Integer now, Integer requested) -> Integer
42
+ end
43
+
10
44
  class Error < StandardError
11
45
  end
12
46
 
13
47
  class Exceeded < Error
14
- attr_reader deadline: Deadline
15
- def initialize: (Deadline) -> void
48
+ attr_reader scope: Scope
49
+ def initialize: (Scope scope) -> void
16
50
  end
17
51
 
18
52
  class Cancelled < Error
19
- attr_reader deadline: Deadline
53
+ attr_reader scope: Scope
20
54
  attr_reader reason: untyped
21
- def initialize: (Deadline, ?untyped) -> void
55
+ def initialize: (Scope scope, ?untyped reason) -> void
22
56
  end
23
57
 
24
58
  module MonotonicClock
@@ -26,53 +60,100 @@ module DeadlineMachines
26
60
  end
27
61
 
28
62
  class TestClock
29
- attr_accessor now_ns: Integer
30
- def initialize: (?now_ns: Integer) -> void
31
- def advance: (Numeric) -> self
63
+ attr_reader now_ns: Integer
64
+ def initialize: (?Integer now_ns) -> void
65
+ def now_ns=: (Integer now_ns) -> Integer
66
+ def advance: (Integer nanos) -> self
67
+
68
+ private
69
+
70
+ def nanoseconds: (Integer value) -> Integer
32
71
  end
33
72
 
34
- class Deadline
35
- NS_PER_SECOND: Integer
73
+ # The runtime cancellation tree.
74
+ class CancelToken
75
+ def self.root: () -> CancelToken
76
+
77
+ def initialize: (CancelToken? parent) -> void
78
+ def cancelled?: () -> bool
79
+ def reason: () -> untyped
80
+ def cancel: (?untyped reason) -> cancellation
81
+ def on_cancel: () { (CancelToken) -> void } -> self
82
+
83
+ # Without a block the child is returned; with one it is unlinked on exit
84
+ # and the block's value is returned.
85
+ def child: () -> CancelToken
86
+ | [T] () { (CancelToken) -> T } -> T
87
+
88
+ def inspect: () -> String
89
+
90
+ private def self.new: (CancelToken? parent) -> CancelToken
91
+
92
+ attr_reader mutex: Thread::Mutex
93
+ attr_writer detached: bool
94
+
95
+ def propagate: (untyped reason, bool inherited) -> bool
96
+ def hold: (CancelToken token) -> void
97
+ def drop: (CancelToken token) -> void
98
+ def detach: (CancelToken token) -> void
99
+ def pending?: () -> bool
100
+
101
+ private
36
102
 
37
- attr_reader expires_at_ns: Integer?
38
- attr_reader parent: Deadline?
103
+ def notify: (Array[StandardError] failures) { () -> void } -> void
104
+ end
105
+
106
+ # A Deadline read against a clock, and a CancelToken.
107
+ class Scope
108
+ attr_reader deadline: Deadline
109
+ attr_reader token: CancelToken
39
110
  attr_reader clock: _Clock
40
- attr_reader reason: untyped
41
111
 
42
- def self.after: (Numeric, ?clock: _Clock) -> Deadline
43
- def self.never: (?clock: _Clock) -> Deadline
44
- def self.to_ns: (Numeric) -> Integer
112
+ def self.new: (Deadline deadline, ?clock: _Clock) -> Scope
113
+ def self.never: (?clock: _Clock) -> Scope
114
+ def self.with_token: (Deadline deadline, CancelToken token, ?clock: _Clock) -> Scope
45
115
 
46
- def initialize: (expires_at_ns: Integer?, clock: _Clock, ?parent: Deadline?) -> void
116
+ def initialize: (Deadline deadline, CancelToken token, _Clock clock) -> void
47
117
 
48
- # nil when there is no time limit.
49
- def remaining_ns: () -> Integer?
50
- def remaining: () -> Float
118
+ # nil when there is no limit.
119
+ def remaining: () -> Integer?
51
120
  def expired?: () -> bool
52
121
  def cancelled?: () -> bool
53
122
  def done?: () -> bool
123
+ def reason: () -> untyped
124
+ def check: () -> refusal?
54
125
  def check!: () -> self
55
- def clamp_sleep: (Numeric) -> Numeric
56
- def cancel: (?untyped) -> self
57
- def on_cancel: () { (Deadline) -> void } -> self
126
+ def clamp_sleep: (Integer requested) -> Integer
127
+ def cancel: (?untyped reason) -> cancellation
128
+ def on_cancel: () { (Scope) -> void } -> self
58
129
 
59
130
  # Without a block the child is returned; with one it is unlinked on exit
60
131
  # and the block's value is returned.
61
- def derive: (?Numeric?) -> Deadline
62
- | [T] (?Numeric?) { (Deadline) -> T } -> T
63
-
132
+ def derive: (Integer timeout) -> Scope
133
+ | [T] (Integer timeout) { (Scope) -> T } -> T
134
+ def child: () -> Scope
135
+ | [T] () { (Scope) -> T } -> T
136
+ def tighten: (Deadline other) -> Scope
137
+ def extend_by: (Integer by) -> Scope
64
138
  def inspect: () -> String
65
139
 
66
- private def child_expiry_ns: (Numeric?) -> Integer?
140
+ private
141
+
142
+ def now: () -> Integer
143
+ def below: (Deadline deadline) -> Scope
144
+ | [T] (Deadline deadline) { (Scope) -> T } -> T
145
+ def state: () -> String
67
146
  end
68
147
 
69
148
  def self.clock: () -> _Clock
70
- def self.clock=: (_Clock?) -> _Clock?
71
- def self.current: () -> Deadline?
72
- def self.with_deadline: [T] (?Numeric?) { (Deadline) -> T } -> T
73
- def self.bind: [T] (Deadline) { (Deadline) -> T } -> T
74
- def self.check!: () -> Deadline?
75
- def self.remaining: () -> Float
76
- def self.clamp_sleep: (Numeric) -> Numeric
77
- def self.deadline?: (untyped) -> bool
149
+ def self.clock=: (_Clock? clock) -> _Clock?
150
+ def self.current: () -> Scope?
151
+ def self.with_deadline: [T] (?Integer? timeout) { (Scope) -> T } -> T
152
+ def self.bind: [T] (Scope scope) { (Scope) -> T } -> T
153
+ def self.check!: () -> Scope?
154
+ def self.remaining: () -> Integer?
155
+ def self.clamp_sleep: (Integer requested) -> Integer
156
+
157
+ private def self.__deadline: (untyped expires_at) -> nil
158
+ private def self.__deadline_of: (untyped deadline) -> Deadline
78
159
  end
metadata CHANGED
@@ -1,34 +1,70 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deadline_machines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies: []
12
- description: 'A monotonic deadline token that derives child budgets, cancels down
13
- its tree, and raises only at checkpoints you choose. The shared time contract for
14
- the machines family: retries, circuit breakers and throttles read one budget instead
15
- of each keeping its own clock.'
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rb_sys
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.9'
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.130
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '0.9'
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.9.130
32
+ description: 'A monotonic deadline that derives child budgets, cancels down its tree,
33
+ and raises only at checkpoints you choose. The arithmetic comes from the no_std
34
+ deadline-machines crate; Ruby owns the cancellation tree and the ambient scope.
35
+ The shared time contract for the machines family: retries, circuit breakers and
36
+ throttles read one budget.'
16
37
  email:
17
38
  - oss@seuros.com
18
39
  executables: []
19
- extensions: []
40
+ extensions:
41
+ - ext/deadline_machines_native/extconf.rb
20
42
  extra_rdoc_files: []
21
43
  files:
22
44
  - CHANGELOG.md
45
+ - Cargo.lock
23
46
  - Cargo.toml
24
47
  - LICENSE.txt
25
48
  - README.md
49
+ - ext/deadline_machines_native/core/CHANGELOG.md
26
50
  - ext/deadline_machines_native/core/Cargo.toml
51
+ - ext/deadline_machines_native/core/README.md
52
+ - ext/deadline_machines_native/core/src/cancel.rs
53
+ - ext/deadline_machines_native/core/src/deadline.rs
27
54
  - ext/deadline_machines_native/core/src/lib.rs
55
+ - ext/deadline_machines_native/core/src/refusal.rs
56
+ - ext/deadline_machines_native/core/src/scope.rs
57
+ - ext/deadline_machines_native/core/src/tokio_support.rs
58
+ - ext/deadline_machines_native/extconf.rb
59
+ - ext/deadline_machines_native/ffi/Cargo.toml
60
+ - ext/deadline_machines_native/ffi/extconf.rb
61
+ - ext/deadline_machines_native/ffi/src/init.rs
62
+ - ext/deadline_machines_native/ffi/src/lib.rs
28
63
  - lib/deadline_machines.rb
64
+ - lib/deadline_machines/cancel_token.rb
29
65
  - lib/deadline_machines/clock.rb
30
- - lib/deadline_machines/deadline.rb
31
66
  - lib/deadline_machines/errors.rb
67
+ - lib/deadline_machines/scope.rb
32
68
  - lib/deadline_machines/version.rb
33
69
  - sig/deadline_machines.rbs
34
70
  homepage: https://github.com/seuros/deadline_machines
@@ -39,6 +75,8 @@ metadata:
39
75
  changelog_uri: https://github.com/seuros/deadline_machines/blob/main/CHANGELOG.md
40
76
  bug_tracker_uri: https://github.com/seuros/deadline_machines/issues
41
77
  rubygems_mfa_required: 'true'
78
+ cargo_crate_name: deadline_machines_native
79
+ cargo_manifest_path: ext/deadline_machines_native/ffi/Cargo.toml
42
80
  rdoc_options: []
43
81
  require_paths:
44
82
  - lib
@@ -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