deadline_machines 0.2.0-aarch64-linux
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 +7 -0
- data/CHANGELOG.md +74 -0
- data/LICENSE.txt +21 -0
- data/README.md +142 -0
- data/ext/deadline_machines_native/core/CHANGELOG.md +44 -0
- data/ext/deadline_machines_native/core/README.md +105 -0
- data/ext/deadline_machines_native/extconf.rb +2 -0
- data/lib/deadline_machines/cancel_token.rb +152 -0
- data/lib/deadline_machines/clock.rb +39 -0
- data/lib/deadline_machines/errors.rb +28 -0
- data/lib/deadline_machines/scope.rb +152 -0
- data/lib/deadline_machines/version.rb +5 -0
- data/lib/deadline_machines.rb +110 -0
- data/lib/deadline_machines_native/deadline_machines_native.so +0 -0
- data/sig/deadline_machines.rbs +159 -0
- metadata +70 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 2a08d8fa4b343e18d89bb118359bf3c2ba1c9b5ff847eef6406fa58615f89925
|
|
4
|
+
data.tar.gz: 151a66b82b0633f4d2b4cd44b9e7b254ecf985103324dc894d2d7053974d781d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 68a73ace827ec557f0e875bc44fc073b2ee51ae1a97e086be99163fb3ae8c6a08253b89963c0dc3efe69756f8dd73385783066166213410d89e2fb84df32d3c5
|
|
7
|
+
data.tar.gz: 68f702bc8ce9be0a2d098291be5ce975db1585d58c8914e21fc404d70aac3d0859a15bf78fa002e9980e60109e42fd078a6cb8cdc20396096f84d4d81962ba68
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.2.0](https://github.com/seuros/deadline_machines/compare/deadline_machines/v0.1.3...deadline_machines/v0.2.0) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### ⚠ BREAKING CHANGES
|
|
7
|
+
|
|
8
|
+
* split Deadline into a native Data value, Scope and CancelToken
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* split Deadline into a native Data value, Scope and CancelToken ([9a4a4ad](https://github.com/seuros/deadline_machines/commit/9a4a4ad1f51feef7fd88d55b1419eee5e93cf5ff))
|
|
13
|
+
|
|
14
|
+
## [0.1.3](https://github.com/seuros/deadline_machines/compare/deadline_machines/v0.1.2...deadline_machines/v0.1.3) (2026-11-06)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* await cancellation and bridge to tokio-util tokens ([72118dc](https://github.com/seuros/deadline_machines/commit/72118dc316a4281f9fbcbb82c536e72b35cac0c2))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Bug Fixes
|
|
23
|
+
|
|
24
|
+
* keep test modules out of the packaged crate ([2719800](https://github.com/seuros/deadline_machines/commit/27198003e6883be4eab975c15cd0a3e14614d808))
|
|
25
|
+
|
|
26
|
+
## [0.1.2](https://github.com/seuros/deadline_machines/compare/deadline_machines/v0.1.1...deadline_machines/v0.1.2) (2026-11-03)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Features
|
|
30
|
+
|
|
31
|
+
* add optional Tokio deadline adapter ([6a36628](https://github.com/seuros/deadline_machines/commit/6a366288d4738f99affc172c62e6c49ca5caae37))
|
|
32
|
+
* pass a rubygems OTP through the environment ([d21d1b1](https://github.com/seuros/deadline_machines/commit/d21d1b13e22172eed51081e971342bd70f99f437))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### Bug Fixes
|
|
36
|
+
|
|
37
|
+
* a republished platform gem is skipped, not fatal ([7e12525](https://github.com/seuros/deadline_machines/commit/7e12525bfebb0d708b83784b2cb8a9e60404794b))
|
|
38
|
+
* collect the built gems whatever depth the download puts them at ([d5330ad](https://github.com/seuros/deadline_machines/commit/d5330ad2c7789ab810b73e1acb5ea2aa749c9a7c))
|
|
39
|
+
* the release tag carries the gem name, and the gate uses the aggregate output ([a4e0f34](https://github.com/seuros/deadline_machines/commit/a4e0f34ac239c046ed6fb1edb0ab6b6027fcf48c))
|
|
40
|
+
|
|
41
|
+
## [0.1.1](https://github.com/seuros/deadline_machines/compare/deadline_machines-v0.1.0...deadline_machines/v0.1.1) (2026-11-01)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
### Features
|
|
45
|
+
|
|
46
|
+
* **core:** add Scope, RBS signatures, release config and CI ([24018c3](https://github.com/seuros/deadline_machines/commit/24018c39edcb12634f92057d699e1f2ad936ad4a))
|
|
47
|
+
* deadline propagation and cooperative cancellation ([9858496](https://github.com/seuros/deadline_machines/commit/9858496f48cc0eca1edf8df613f9974fa15abba2))
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### Bug Fixes
|
|
51
|
+
|
|
52
|
+
* a block scope lets go of its child's retention too ([e5502c2](https://github.com/seuros/deadline_machines/commit/e5502c29ba21306ef386303e465195d27cb5d643))
|
|
53
|
+
* a cancellation racing a registration no longer strands the child ([e387b71](https://github.com/seuros/deadline_machines/commit/e387b71e6464bb5a6b35ec17b68e898231a8eca7))
|
|
54
|
+
* a failed gem push fails the release ([26678da](https://github.com/seuros/deadline_machines/commit/26678da9039c72be8457e53f3cc4473b6394f5c1))
|
|
55
|
+
* a nested scope without a limit inherits the ambient budget ([f88a0d5](https://github.com/seuros/deadline_machines/commit/f88a0d56497ee199664bd96fa479d6262abcc245))
|
|
56
|
+
* a parent no longer owns every deadline derived from it ([41d609c](https://github.com/seuros/deadline_machines/commit/41d609c623a480b94a3f2789c0d7c3dbf2920ee9))
|
|
57
|
+
* an observer deep in a chain is held all the way to the root ([9abeb8d](https://github.com/seuros/deadline_machines/commit/9abeb8d311f85991d4eed2d4f932b147e59ed574))
|
|
58
|
+
* detachment stops the retention walk, not just its first step ([44ff4d1](https://github.com/seuros/deadline_machines/commit/44ff4d108a6dc139a7731e50432d5010b2f7c2cd))
|
|
59
|
+
* one failing cancel observer no longer silences its peers ([a9ddd70](https://github.com/seuros/deadline_machines/commit/a9ddd70d0d1355aacfc0170d9b874bd9c90e3309))
|
|
60
|
+
* one lock per tree, so a retention chain walk is atomic ([eec49b9](https://github.com/seuros/deadline_machines/commit/eec49b9ff6a79930cfbb4cbb8de398ad7b75efaa))
|
|
61
|
+
* ship the core's test files so the packaged crate builds ([0340bc6](https://github.com/seuros/deadline_machines/commit/0340bc6ec958cb1983212f4cd0b9943cc36aef5c))
|
|
62
|
+
|
|
63
|
+
## 0.1.0
|
|
64
|
+
|
|
65
|
+
- Initial release.
|
|
66
|
+
- `DeadlineMachines::Deadline`: monotonic budgets, `derive` for child scopes that
|
|
67
|
+
cannot outlive their parent, cancellation that flows down a tree, and `check!`
|
|
68
|
+
as the only place an error is raised.
|
|
69
|
+
- `DeadlineMachines.with_deadline` for a fiber-scoped ambient deadline.
|
|
70
|
+
- `DeadlineMachines::TestClock` for deterministic tests across the family.
|
|
71
|
+
- `deadline-machines` core crate: the same arithmetic and cancellation tree in
|
|
72
|
+
`no_std` Rust with zero dependencies, plus `Scope` pairing a budget with a
|
|
73
|
+
cancellation handle. Builds and lints clean on bare metal.
|
|
74
|
+
- RBS signatures for the whole public API.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Abdelkader Boudih
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# DeadlineMachines ⏱️
|
|
2
|
+
|
|
3
|
+
> **One clock for the whole fleet.** The tender that hands every ship the same
|
|
4
|
+
> budget, so nobody spends time the mission no longer has.
|
|
5
|
+
|
|
6
|
+
Deadline propagation and cooperative cancellation for Ruby.
|
|
7
|
+
|
|
8
|
+
`Timeout.timeout` raises wherever the interpreter happens to be, which is why it
|
|
9
|
+
corrupts connection state and why every serious library bans it. A deadline here
|
|
10
|
+
is a value you pass down and check at points you choose. **AMS Layland** carries
|
|
11
|
+
no weapons and trips no failures; it keeps the time the rest of the fleet
|
|
12
|
+
navigates by.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 🚀 Launch Sequence
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# Add to your ship's Gemfile
|
|
20
|
+
gem 'deadline_machines'
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Requires **Ruby 4.0+**. The budget arithmetic is native: platform gems ship
|
|
24
|
+
precompiled for macOS and Linux, and a source install builds the extension
|
|
25
|
+
with a Rust toolchain.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 🎯 Mission Brief
|
|
30
|
+
|
|
31
|
+
Every instant and span is **Integer nanoseconds**, as
|
|
32
|
+
`Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)` reads them.
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
SECOND = 1_000_000_000
|
|
36
|
+
|
|
37
|
+
DeadlineMachines.with_deadline(5 * SECOND) do |request|
|
|
38
|
+
request.remaining # => 5_000_000_000
|
|
39
|
+
|
|
40
|
+
request.derive(SECOND) do |attempt|
|
|
41
|
+
attempt.remaining # => 1_000_000_000
|
|
42
|
+
attempt.check # => nil, or :exceeded / :cancelled
|
|
43
|
+
attempt.check! # raises DeadlineMachines::Exceeded when spent
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
DeadlineMachines.with_deadline(60 * SECOND) do
|
|
47
|
+
DeadlineMachines.remaining # => 5_000_000_000, a nested scope cannot widen the budget
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
request.cancel('client hung up') # => :propagated (:already_cancelled on a repeat)
|
|
51
|
+
DeadlineMachines.check! # raises DeadlineMachines::Cancelled
|
|
52
|
+
end
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The yielded object is a `Scope`: a frozen `Deadline` value plus a
|
|
56
|
+
`CancelToken`, read against `DeadlineMachines.clock`. `remaining` is `nil`
|
|
57
|
+
when there is no limit.
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## 🛰️ Why the Fleet Needs It
|
|
62
|
+
|
|
63
|
+
Without a shared budget, a retry policy will happily spend thirty seconds on a
|
|
64
|
+
request the caller abandoned twenty-eight seconds ago. `clamp_sleep` is the hook
|
|
65
|
+
that stops it:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
delay = scope.clamp_sleep(backoff.next_delay_ns) # never overruns what is left
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`DeadlineMachines.current` is fiber-scoped, so `Async` tasks inherit the ambient
|
|
72
|
+
deadline without being handed one.
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## 🕰️ The Ship's Chronometer
|
|
77
|
+
|
|
78
|
+
Budgets are built on `CLOCK_MONOTONIC`, so an NTP step cannot stretch them.
|
|
79
|
+
Inject `TestClock` and the whole family becomes deterministic:
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
clock = DeadlineMachines::TestClock.new
|
|
83
|
+
DeadlineMachines.clock = clock
|
|
84
|
+
scope = DeadlineMachines::Scope.new(DeadlineMachines::Deadline.after(clock.now_ns, 1_000))
|
|
85
|
+
clock.advance(2_000)
|
|
86
|
+
scope.expired? # => true
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`Deadline` itself holds no clock and mirrors the Rust core, `now` first:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
deadline = DeadlineMachines::Deadline.after(0, 100)
|
|
93
|
+
deadline.remaining(40) # => 60
|
|
94
|
+
deadline.check(100) # => :exceeded
|
|
95
|
+
deadline.clamp_sleep(80, 30) # => 20
|
|
96
|
+
deadline.tighten(DeadlineMachines::Deadline.never) # => the same deadline
|
|
97
|
+
deadline.extend_by(50) # the core's `extend`, renamed past Object#extend
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
A `Deadline` is valid or does not exist: `expires_at` is `nil` or an Integer
|
|
101
|
+
in the core's `Duration` range, checked natively whether the gem built it or
|
|
102
|
+
you did. TypeError for the wrong type, RangeError out of range.
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## 🔻 Cancellation Runs Down the Chain of Command
|
|
107
|
+
|
|
108
|
+
Cancellation flows down a tree and never up. A child derived after its parent was
|
|
109
|
+
cancelled is already cancelled, and cancelling it answers `:already_cancelled`
|
|
110
|
+
and changes nothing, its reason included. `on_cancel` fires once, and a callback
|
|
111
|
+
registered on an already-cancelled scope fires immediately. The block forms of
|
|
112
|
+
`derive` and `child` unlink the child on exit, so a long-lived parent does not
|
|
113
|
+
accumulate children. `tighten` and `extend_by` return a scope sharing the same
|
|
114
|
+
token.
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## 🔩 The Rust Core
|
|
119
|
+
|
|
120
|
+
The arithmetic ships as the `deadline-machines` crate under
|
|
121
|
+
`ext/deadline_machines_native/core`: `no_std`, `Deadline` allocation-free with
|
|
122
|
+
zero dependencies, verified on bare metal. Every `Deadline` operation in this
|
|
123
|
+
gem goes through it. `CancelToken` and `Scope` there (feature `alloc`) are the
|
|
124
|
+
peers of the Ruby classes of the same names; the Ruby ones stay in Ruby
|
|
125
|
+
because they hold Ruby observers, reasons and fiber-bound state.
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## 📡 Credits
|
|
130
|
+
|
|
131
|
+
**AMS Layland**, NCC-0112, *Layland*-class tender.
|
|
132
|
+
|
|
133
|
+
Named for C. L. Liu and James W. Layland's *Scheduling Algorithms for
|
|
134
|
+
Multiprogramming in a Hard-Real-Time Environment* (1973), which gave
|
|
135
|
+
earliest-deadline-first scheduling its proof and made a deadline a value a
|
|
136
|
+
system reasons about rather than an alarm it waits for.
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
MIT.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.5.0](https://github.com/seuros/deadline_machines/compare/deadline-machines-v0.4.0...deadline-machines-v0.5.0) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### ⚠ BREAKING CHANGES
|
|
7
|
+
|
|
8
|
+
* split Deadline into a native Data value, Scope and CancelToken
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* split Deadline into a native Data value, Scope and CancelToken ([9a4a4ad](https://github.com/seuros/deadline_machines/commit/9a4a4ad1f51feef7fd88d55b1419eee5e93cf5ff))
|
|
13
|
+
|
|
14
|
+
## [0.4.0](https://github.com/seuros/deadline_machines/compare/deadline-machines-v0.3.0...deadline-machines-v0.4.0) (2026-11-06)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* await cancellation and bridge to tokio-util tokens ([72118dc](https://github.com/seuros/deadline_machines/commit/72118dc316a4281f9fbcbb82c536e72b35cac0c2))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Bug Fixes
|
|
23
|
+
|
|
24
|
+
* keep test modules out of the packaged crate ([2719800](https://github.com/seuros/deadline_machines/commit/27198003e6883be4eab975c15cd0a3e14614d808))
|
|
25
|
+
|
|
26
|
+
## [0.3.0](https://github.com/seuros/deadline_machines/compare/deadline-machines-v0.2.0...deadline-machines-v0.3.0) (2026-11-03)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Features
|
|
30
|
+
|
|
31
|
+
* add optional Tokio deadline adapter ([6a36628](https://github.com/seuros/deadline_machines/commit/6a366288d4738f99affc172c62e6c49ca5caae37))
|
|
32
|
+
|
|
33
|
+
## [0.2.0](https://github.com/seuros/deadline_machines/compare/deadline-machines-v0.1.0...deadline-machines-v0.2.0) (2026-11-01)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
### Features
|
|
37
|
+
|
|
38
|
+
* **core:** add Scope, RBS signatures, release config and CI ([24018c3](https://github.com/seuros/deadline_machines/commit/24018c39edcb12634f92057d699e1f2ad936ad4a))
|
|
39
|
+
* deadline propagation and cooperative cancellation ([9858496](https://github.com/seuros/deadline_machines/commit/9858496f48cc0eca1edf8df613f9974fa15abba2))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
### Bug Fixes
|
|
43
|
+
|
|
44
|
+
* ship the core's test files so the packaged crate builds ([0340bc6](https://github.com/seuros/deadline_machines/commit/0340bc6ec958cb1983212f4cd0b9943cc36aef5c))
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# deadline-machines
|
|
2
|
+
|
|
3
|
+
Monotonic deadline arithmetic and cooperative cancellation trees. `no_std`;
|
|
4
|
+
`Deadline` is allocation-free and has zero dependencies.
|
|
5
|
+
|
|
6
|
+
Every instant is a `core::time::Duration` since an epoch the caller picks, and
|
|
7
|
+
every span is a `Duration`. `Deadline` holds no clock: the caller reads its
|
|
8
|
+
own and passes `now` in.
|
|
9
|
+
|
|
10
|
+
```rust
|
|
11
|
+
use core::time::Duration;
|
|
12
|
+
use deadline_machines::{Deadline, Refusal};
|
|
13
|
+
|
|
14
|
+
let now = Duration::ZERO;
|
|
15
|
+
let request = Deadline::after(now, Duration::from_secs(5));
|
|
16
|
+
|
|
17
|
+
// A child never outlives its parent.
|
|
18
|
+
let attempt = request.derive(now, Duration::from_secs(9));
|
|
19
|
+
assert_eq!(attempt, request);
|
|
20
|
+
|
|
21
|
+
// A backoff that cannot overrun the budget.
|
|
22
|
+
let later = Duration::from_secs(4);
|
|
23
|
+
assert_eq!(request.clamp_sleep(later, Duration::from_secs(3)), Duration::from_secs(1));
|
|
24
|
+
|
|
25
|
+
// The checkpoint. A refusal records nothing: it answers for that instant.
|
|
26
|
+
assert_eq!(request.check(Duration::from_secs(5)), Err(Refusal::Exceeded));
|
|
27
|
+
assert_eq!(request.check(later), Ok(()));
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
With the `alloc` feature, `CancelToken` is a cancellation tree that flows down
|
|
31
|
+
and never up, and `Scope` pairs a deadline with a token, which is what a retry
|
|
32
|
+
policy or a circuit breaker actually consults:
|
|
33
|
+
|
|
34
|
+
```rust
|
|
35
|
+
use core::time::Duration;
|
|
36
|
+
use deadline_machines::cancel::Cancellation;
|
|
37
|
+
use deadline_machines::{Deadline, Refusal, Scope};
|
|
38
|
+
|
|
39
|
+
let now = Duration::ZERO;
|
|
40
|
+
let request = Scope::new(Deadline::after(now, Duration::from_secs(5)));
|
|
41
|
+
let attempt = request.derive(now, Duration::from_secs(9));
|
|
42
|
+
|
|
43
|
+
// A child cannot outlive its parent.
|
|
44
|
+
assert_eq!(attempt.deadline(), request.deadline());
|
|
45
|
+
|
|
46
|
+
assert_eq!(request.cancel(), Cancellation::Propagated);
|
|
47
|
+
assert_eq!(request.cancel(), Cancellation::AlreadyCancelled);
|
|
48
|
+
assert_eq!(attempt.check(now), Err(Refusal::Cancelled));
|
|
49
|
+
|
|
50
|
+
// A budget that paused should not pay for the wait.
|
|
51
|
+
let resumed = attempt.extend(Duration::from_secs(2));
|
|
52
|
+
assert_eq!(resumed.deadline().expires_at(), Some(Duration::from_secs(7)));
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`Deadline` builds on bare metal with no features; `CancelToken` and `Scope`
|
|
56
|
+
need `alloc`.
|
|
57
|
+
|
|
58
|
+
The `tokio` feature adds `TokioClock`, the one clock this crate reads. It
|
|
59
|
+
follows Tokio's time, paused test time included, so one deadline drives both
|
|
60
|
+
the arithmetic and the executor's timer:
|
|
61
|
+
|
|
62
|
+
```rust
|
|
63
|
+
use core::time::Duration;
|
|
64
|
+
use deadline_machines::tokio_support::{TokioClock, timeout};
|
|
65
|
+
use deadline_machines::{Deadline, Refusal};
|
|
66
|
+
|
|
67
|
+
# async fn example() -> Result<(), Refusal> {
|
|
68
|
+
let clock = TokioClock::new();
|
|
69
|
+
let deadline = Deadline::after(clock.now(), Duration::from_secs(2));
|
|
70
|
+
timeout(&clock, deadline, async { do_work().await }).await?;
|
|
71
|
+
# Ok(())
|
|
72
|
+
# }
|
|
73
|
+
# async fn do_work() {}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
A `Scope` runs a future under its remaining budget and its cancellation at
|
|
77
|
+
once, and `cancelled()` is a cancel-safe future for `select!`:
|
|
78
|
+
|
|
79
|
+
```rust
|
|
80
|
+
use core::time::Duration;
|
|
81
|
+
use deadline_machines::tokio_support::TokioClock;
|
|
82
|
+
use deadline_machines::{Deadline, Refusal, Scope};
|
|
83
|
+
|
|
84
|
+
# async fn example() -> Result<(), Refusal> {
|
|
85
|
+
let clock = TokioClock::new();
|
|
86
|
+
let request = Scope::new(Deadline::after(clock.now(), Duration::from_secs(2)));
|
|
87
|
+
let attempt = request.child();
|
|
88
|
+
|
|
89
|
+
let output = attempt.tokio_timeout(&clock, async { do_work().await }).await?;
|
|
90
|
+
# let () = output;
|
|
91
|
+
# Ok(())
|
|
92
|
+
# }
|
|
93
|
+
# async fn do_work() {}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
A deadline past the last instant Tokio can represent starts no timer: no
|
|
97
|
+
timer would ever reach it.
|
|
98
|
+
|
|
99
|
+
The `tokio-util` feature bridges both ways with `tokio_util::CancellationToken`:
|
|
100
|
+
`CancelToken::linked_to(&token)` is cancelled whenever `token` is, and
|
|
101
|
+
`to_cancellation_token()` hands out one that follows this tree.
|
|
102
|
+
|
|
103
|
+
This crate is the time contract the rest of the machines family builds on:
|
|
104
|
+
`chrono-machines` clamps its backoff to it, `breaker-machines` fails fast when it
|
|
105
|
+
is already spent.
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DeadlineMachines
|
|
4
|
+
# A node in a cancellation tree, the runtime peer of the core's
|
|
5
|
+
# `CancelToken`: cancellation flows down and never up, a child of a
|
|
6
|
+
# cancelled token is born cancelled, and observers registered with
|
|
7
|
+
# {#on_cancel} fire once. Pure Ruby, because it holds Ruby callbacks.
|
|
8
|
+
class CancelToken
|
|
9
|
+
class << self
|
|
10
|
+
# A token with no parent.
|
|
11
|
+
def root
|
|
12
|
+
new(nil)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private :new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def initialize(parent)
|
|
19
|
+
@parent = parent
|
|
20
|
+
@cancelled = false
|
|
21
|
+
@detached = false
|
|
22
|
+
@reason = nil
|
|
23
|
+
@children = ObjectSpace::WeakMap.new
|
|
24
|
+
@retained = {}
|
|
25
|
+
@callbacks = []
|
|
26
|
+
@mutex = parent ? parent.mutex : Mutex.new
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Whether this token or an ancestor was cancelled.
|
|
30
|
+
def cancelled?
|
|
31
|
+
@cancelled || !!@parent&.cancelled?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Why this token, or the nearest cancelled ancestor, was cancelled.
|
|
35
|
+
def reason
|
|
36
|
+
@reason || @parent&.reason
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Cancels this token and every descendant, now or later, then tells every
|
|
40
|
+
# observer. Answers :propagated, or :already_cancelled when this token or
|
|
41
|
+
# an ancestor already was: that repeat changes nothing, +reason+ included
|
|
42
|
+
# (read {#reason} for the one that stands). When observers raise, all of
|
|
43
|
+
# them still run and the first error is raised afterwards.
|
|
44
|
+
def cancel(reason = nil)
|
|
45
|
+
propagate(reason, true) ? :propagated : :already_cancelled
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Calls the block with this token once it is cancelled, at once if it
|
|
49
|
+
# already is.
|
|
50
|
+
def on_cancel(&block)
|
|
51
|
+
already = @mutex.synchronize do
|
|
52
|
+
settled = cancelled?
|
|
53
|
+
unless settled
|
|
54
|
+
@callbacks << block
|
|
55
|
+
@parent&.hold(self) unless @detached
|
|
56
|
+
end
|
|
57
|
+
settled
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
block.call(self) if already
|
|
61
|
+
self
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# A token cancelled whenever this one is, cancellable on its own. With a
|
|
65
|
+
# block, the child is yielded and unlinked from this token's bookkeeping
|
|
66
|
+
# when the block exits; the block's value is returned.
|
|
67
|
+
def child
|
|
68
|
+
token = CancelToken.__send__(:new, self)
|
|
69
|
+
@mutex.synchronize { @children[token] = true }
|
|
70
|
+
return token unless block_given?
|
|
71
|
+
|
|
72
|
+
begin
|
|
73
|
+
yield token
|
|
74
|
+
ensure
|
|
75
|
+
@mutex.synchronize { detach(token) }
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def inspect
|
|
80
|
+
"#<#{self.class} #{cancelled? ? 'cancelled' : 'live'}>"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
protected
|
|
84
|
+
|
|
85
|
+
attr_reader :mutex
|
|
86
|
+
attr_writer :detached
|
|
87
|
+
|
|
88
|
+
# Sets this token's own flag and cascades. False when it was already set
|
|
89
|
+
# (a racing cancel got there first) or, for a cancel called on this token
|
|
90
|
+
# (+inherited+), when an ancestor already was: decided under the tree's
|
|
91
|
+
# one lock, so the answer and the effect agree.
|
|
92
|
+
def propagate(reason, inherited)
|
|
93
|
+
callbacks, children = @mutex.synchronize do
|
|
94
|
+
return false if @cancelled || (inherited && @parent&.cancelled?)
|
|
95
|
+
|
|
96
|
+
@cancelled = true
|
|
97
|
+
@reason = reason
|
|
98
|
+
@parent&.drop(self)
|
|
99
|
+
[@callbacks.dup, @children.keys]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
failures = []
|
|
103
|
+
children.each { |token| notify(failures) { token.propagate(reason, false) } }
|
|
104
|
+
callbacks.each { |callback| notify(failures) { callback.call(self) } }
|
|
105
|
+
raise failures.first unless failures.empty?
|
|
106
|
+
|
|
107
|
+
true
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# A child token is the caller's to keep, so the parent follows it weakly.
|
|
111
|
+
# Something to deliver is the exception, and it has to be held all the
|
|
112
|
+
# way to the root: an intermediate token nobody else references is the
|
|
113
|
+
# one link whose collection would strand the delivery below it.
|
|
114
|
+
#
|
|
115
|
+
# Holding and dropping walk the chain, so they cannot be made atomic out
|
|
116
|
+
# of one lock per link: every token in a tree shares the root's lock, and
|
|
117
|
+
# both run inside a section the caller already holds.
|
|
118
|
+
def hold(token)
|
|
119
|
+
@retained[token] = true
|
|
120
|
+
@parent.hold(self) if @parent && !@detached
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def drop(token)
|
|
124
|
+
@retained.delete(token)
|
|
125
|
+
@parent.drop(self) if @parent && !pending?
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# A block that has ended owns its child outright: the tree will not reach
|
|
129
|
+
# it again, so the parent holds nothing for it and an observer registered
|
|
130
|
+
# afterwards is the caller's own to keep alive.
|
|
131
|
+
def detach(token)
|
|
132
|
+
@children.delete(token)
|
|
133
|
+
token.detached = true
|
|
134
|
+
drop(token)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Whether anything is still owed through this token.
|
|
138
|
+
def pending?
|
|
139
|
+
!@cancelled && !(@callbacks.empty? && @retained.empty?)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
private
|
|
143
|
+
|
|
144
|
+
# Every observer and descendant is told, then the first failure is raised.
|
|
145
|
+
# One raising observer must not silence its peers.
|
|
146
|
+
def notify(failures)
|
|
147
|
+
yield
|
|
148
|
+
rescue StandardError => e
|
|
149
|
+
failures << e
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DeadlineMachines
|
|
4
|
+
# The process's monotonic clock, in Integer nanoseconds.
|
|
5
|
+
module MonotonicClock
|
|
6
|
+
def self.now_ns
|
|
7
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# A clock that moves only when told to, for deterministic tests.
|
|
12
|
+
class TestClock
|
|
13
|
+
attr_reader :now_ns
|
|
14
|
+
|
|
15
|
+
def initialize(now_ns = 0)
|
|
16
|
+
@now_ns = nanoseconds(now_ns)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Sets the clock to +now_ns+.
|
|
20
|
+
def now_ns=(now_ns)
|
|
21
|
+
@now_ns = nanoseconds(now_ns)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Moves the clock forward by +nanos+.
|
|
25
|
+
def advance(nanos)
|
|
26
|
+
@now_ns += nanoseconds(nanos)
|
|
27
|
+
self
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def nanoseconds(value)
|
|
33
|
+
raise TypeError, 'a TestClock reads Integer nanoseconds' unless value.is_a?(Integer)
|
|
34
|
+
raise RangeError, 'a TestClock reads no negative nanoseconds' if value.negative?
|
|
35
|
+
|
|
36
|
+
value
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DeadlineMachines
|
|
4
|
+
# What a checkpoint raises. Refusals as values are the symbols of
|
|
5
|
+
# {REFUSALS}; these exist so `check!` can unwind cooperative work.
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
# The budget of +scope+ ran out.
|
|
9
|
+
class Exceeded < Error
|
|
10
|
+
attr_reader :scope
|
|
11
|
+
|
|
12
|
+
def initialize(scope)
|
|
13
|
+
@scope = scope
|
|
14
|
+
super('deadline exceeded')
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# +scope+ or an ancestor was cancelled, for +reason+.
|
|
19
|
+
class Cancelled < Error
|
|
20
|
+
attr_reader :scope, :reason
|
|
21
|
+
|
|
22
|
+
def initialize(scope, reason = nil)
|
|
23
|
+
@scope = scope
|
|
24
|
+
@reason = reason
|
|
25
|
+
super(reason ? "deadline cancelled: #{reason}" : 'deadline cancelled')
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DeadlineMachines
|
|
4
|
+
# A {Deadline} and a {CancelToken} as one value, the runtime peer of the
|
|
5
|
+
# core's `Scope`. Where the core takes `now`, a scope reads its clock
|
|
6
|
+
# (`clock` keyword, {DeadlineMachines.clock} by default): that clock and
|
|
7
|
+
# the token's Ruby observers are what this runtime adds on top of the core.
|
|
8
|
+
# Frozen; scopes sharing a token share cancellation.
|
|
9
|
+
class Scope
|
|
10
|
+
attr_reader :deadline, :token, :clock
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
# A scope under +deadline+ with a fresh root token.
|
|
14
|
+
def new(deadline, clock: DeadlineMachines.clock)
|
|
15
|
+
with_token(deadline, CancelToken.root, clock: clock)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# A scope with no time limit, cancellable only.
|
|
19
|
+
def never(clock: DeadlineMachines.clock)
|
|
20
|
+
new(Deadline.never, clock: clock)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# A scope under +deadline+ that shares +token+. The deadline is read
|
|
24
|
+
# natively, as every Deadline operation reads one, and the scope keeps
|
|
25
|
+
# the value rebuilt from it. The token and the clock are this runtime's
|
|
26
|
+
# own Ruby objects and are checked here.
|
|
27
|
+
def with_token(deadline, token, clock: DeadlineMachines.clock)
|
|
28
|
+
deadline = DeadlineMachines.__send__(:__deadline_of, deadline)
|
|
29
|
+
raise TypeError, 'token must be a DeadlineMachines::CancelToken' unless token.instance_of?(CancelToken)
|
|
30
|
+
raise TypeError, 'clock must respond to now_ns' unless clock.respond_to?(:now_ns)
|
|
31
|
+
|
|
32
|
+
scope = allocate
|
|
33
|
+
scope.__send__(:initialize, deadline, token, clock)
|
|
34
|
+
scope
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def initialize(deadline, token, clock)
|
|
39
|
+
@deadline = deadline
|
|
40
|
+
@token = token
|
|
41
|
+
@clock = clock
|
|
42
|
+
freeze
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Nanoseconds left, zero once spent, nil when there is no limit.
|
|
46
|
+
def remaining
|
|
47
|
+
@deadline.remaining(now)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def expired?
|
|
51
|
+
@deadline.expired?(now)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def cancelled?
|
|
55
|
+
@token.cancelled?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def done?
|
|
59
|
+
cancelled? || expired?
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Why this scope or the nearest cancelled ancestor was cancelled.
|
|
63
|
+
def reason
|
|
64
|
+
@token.reason
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The checkpoint as a value: nil, or the refusal symbol. :cancelled
|
|
68
|
+
# outranks :exceeded.
|
|
69
|
+
def check
|
|
70
|
+
return :cancelled if cancelled?
|
|
71
|
+
|
|
72
|
+
@deadline.check(now)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The cooperative checkpoint: raises Cancelled (which outranks) or
|
|
76
|
+
# Exceeded, else returns self.
|
|
77
|
+
def check!
|
|
78
|
+
raise Cancelled.new(self, reason) if cancelled?
|
|
79
|
+
raise Exceeded, self if expired?
|
|
80
|
+
|
|
81
|
+
self
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# The longest sleep up to +requested+ nanoseconds that still fits.
|
|
85
|
+
def clamp_sleep(requested)
|
|
86
|
+
@deadline.clamp_sleep(now, requested)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# See {CancelToken#cancel}: :propagated or :already_cancelled.
|
|
90
|
+
def cancel(reason = nil)
|
|
91
|
+
@token.cancel(reason)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Calls the block with this scope once it is cancelled, at once if it
|
|
95
|
+
# already is.
|
|
96
|
+
def on_cancel
|
|
97
|
+
@token.on_cancel { yield self }
|
|
98
|
+
self
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# A child scope whose deadline is the tighter of this one and +timeout+
|
|
102
|
+
# nanoseconds from now, cancellable on its own. With a block, the child
|
|
103
|
+
# is yielded and unlinked when the block exits; the block's value is
|
|
104
|
+
# returned.
|
|
105
|
+
def derive(timeout, &)
|
|
106
|
+
below(@deadline.derive(now, timeout), &)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# A child scope with the same deadline, cancellable on its own; the block
|
|
110
|
+
# form as {#derive}.
|
|
111
|
+
def child(&)
|
|
112
|
+
below(@deadline, &)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# This scope with the tighter of its deadline and +other+, sharing its
|
|
116
|
+
# cancellation.
|
|
117
|
+
def tighten(other)
|
|
118
|
+
Scope.with_token(@deadline.tighten(other), @token, clock: @clock)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# This scope with its expiry pushed back by +by+ nanoseconds, sharing its
|
|
122
|
+
# cancellation.
|
|
123
|
+
def extend_by(by)
|
|
124
|
+
Scope.with_token(@deadline.extend_by(by), @token, clock: @clock)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def inspect
|
|
128
|
+
"#<#{self.class} #{state}>"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
private
|
|
132
|
+
|
|
133
|
+
def now
|
|
134
|
+
@clock.now_ns
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def below(deadline)
|
|
138
|
+
return Scope.with_token(deadline, @token.child, clock: @clock) unless block_given?
|
|
139
|
+
|
|
140
|
+
@token.child { |token| yield Scope.with_token(deadline, token, clock: @clock) }
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def state
|
|
144
|
+
return 'cancelled' if cancelled?
|
|
145
|
+
|
|
146
|
+
left = remaining
|
|
147
|
+
return 'unbounded' if left.nil?
|
|
148
|
+
|
|
149
|
+
left.zero? ? 'expired' : "#{left}ns left"
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'deadline_machines/version'
|
|
4
|
+
|
|
5
|
+
# Every instant and span in this gem is Integer nanoseconds: `now` as read
|
|
6
|
+
# from `Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)`, and
|
|
7
|
+
# every timeout, extension, remaining budget and sleep. They reach the core
|
|
8
|
+
# as a `Duration`, so each is in 0..Duration::MAX nanoseconds; TypeError for
|
|
9
|
+
# anything but an Integer, RangeError outside that range.
|
|
10
|
+
module DeadlineMachines
|
|
11
|
+
# When a budget runs out: `expires_at` is an instant in Integer
|
|
12
|
+
# nanoseconds, or nil for never. A frozen value, validated through the core
|
|
13
|
+
# whether the gem built it or the caller did. `never`, `at`, `after` and
|
|
14
|
+
# every operation below are native and mirror the core's `Deadline`;
|
|
15
|
+
# `extend_by` is the core's `extend`, renamed so it does not shadow
|
|
16
|
+
# `Object#extend`.
|
|
17
|
+
Deadline = Data.define(:expires_at) do
|
|
18
|
+
def initialize(expires_at:)
|
|
19
|
+
DeadlineMachines.__send__(:__deadline, expires_at)
|
|
20
|
+
super
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Native is required: no algorithm fallback. It validates and builds the
|
|
26
|
+
# Deadline above and adds its arithmetic, so the class must exist first.
|
|
27
|
+
require 'deadline_machines_native/deadline_machines_native'
|
|
28
|
+
|
|
29
|
+
module DeadlineMachines
|
|
30
|
+
private_class_method :__deadline, :__deadline_of
|
|
31
|
+
|
|
32
|
+
# The value is sealed. `initialize` is where it validates, and a subclass
|
|
33
|
+
# could override it, or `allocate` build an instance that never ran it:
|
|
34
|
+
# subclassing raises TypeError and `allocate` is gone. Sealing stops
|
|
35
|
+
# accidents, not reflection (`Marshal.load` skips `initialize`), so the
|
|
36
|
+
# native side is the authority: a Deadline it reads must be exactly that
|
|
37
|
+
# class, and its expiry is checked again.
|
|
38
|
+
Deadline.singleton_class.undef_method(:allocate)
|
|
39
|
+
Deadline.define_singleton_method(:inherited) { |_| raise TypeError, "#{Deadline} cannot be subclassed" }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
require_relative 'deadline_machines/errors'
|
|
43
|
+
require_relative 'deadline_machines/clock'
|
|
44
|
+
require_relative 'deadline_machines/cancel_token'
|
|
45
|
+
require_relative 'deadline_machines/scope'
|
|
46
|
+
|
|
47
|
+
# The ambient scope: a runtime layered on the core, bound per fiber.
|
|
48
|
+
module DeadlineMachines
|
|
49
|
+
FIBER_KEY = :deadline_machines_scope
|
|
50
|
+
|
|
51
|
+
class << self
|
|
52
|
+
attr_writer :clock
|
|
53
|
+
|
|
54
|
+
# What every scope built without an explicit clock reads.
|
|
55
|
+
def clock
|
|
56
|
+
@clock ||= MonotonicClock
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The scope bound to this fiber, or nil.
|
|
60
|
+
def current
|
|
61
|
+
Fiber[FIBER_KEY]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Runs the block under a scope bound to this fiber, and returns its value.
|
|
65
|
+
# Under an ambient scope it is a child of that one, tightened to
|
|
66
|
+
# +timeout+ nanoseconds when given; otherwise a root scope that runs out
|
|
67
|
+
# +timeout+ nanoseconds from now, or never.
|
|
68
|
+
def with_deadline(timeout = nil, &)
|
|
69
|
+
ambient = current
|
|
70
|
+
if ambient
|
|
71
|
+
return ambient.child { |scope| bind(scope, &) } if timeout.nil?
|
|
72
|
+
|
|
73
|
+
return ambient.derive(timeout) { |scope| bind(scope, &) }
|
|
74
|
+
end
|
|
75
|
+
deadline = timeout.nil? ? Deadline.never : Deadline.after(clock.now_ns, timeout)
|
|
76
|
+
bind(Scope.new(deadline), &)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Binds +scope+ to this fiber for the block, restoring the previous one.
|
|
80
|
+
def bind(scope)
|
|
81
|
+
raise TypeError, 'scope must be a DeadlineMachines::Scope' unless scope.instance_of?(Scope)
|
|
82
|
+
|
|
83
|
+
previous = Fiber[FIBER_KEY]
|
|
84
|
+
Fiber[FIBER_KEY] = scope
|
|
85
|
+
begin
|
|
86
|
+
yield scope
|
|
87
|
+
ensure
|
|
88
|
+
Fiber[FIBER_KEY] = previous
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The ambient checkpoint: raises Cancelled or Exceeded, else returns the
|
|
93
|
+
# scope, or nil outside one.
|
|
94
|
+
def check!
|
|
95
|
+
current&.check!
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Nanoseconds left in the ambient scope, nil when there is no limit.
|
|
99
|
+
def remaining
|
|
100
|
+
current&.remaining
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# The longest sleep up to +requested+ nanoseconds that fits the ambient
|
|
104
|
+
# budget; +requested+ itself outside a scope.
|
|
105
|
+
def clamp_sleep(requested)
|
|
106
|
+
scope = current
|
|
107
|
+
scope ? scope.clamp_sleep(requested) : Deadline.never.clamp_sleep(0, requested)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
Binary file
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
module DeadlineMachines
|
|
2
|
+
VERSION: String
|
|
3
|
+
FIBER_KEY: Symbol
|
|
4
|
+
|
|
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.
|
|
12
|
+
interface _Clock
|
|
13
|
+
def now_ns: () -> Integer
|
|
14
|
+
end
|
|
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
|
+
|
|
44
|
+
class Error < StandardError
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class Exceeded < Error
|
|
48
|
+
attr_reader scope: Scope
|
|
49
|
+
def initialize: (Scope scope) -> void
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
class Cancelled < Error
|
|
53
|
+
attr_reader scope: Scope
|
|
54
|
+
attr_reader reason: untyped
|
|
55
|
+
def initialize: (Scope scope, ?untyped reason) -> void
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
module MonotonicClock
|
|
59
|
+
def self.now_ns: () -> Integer
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
class TestClock
|
|
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
|
|
71
|
+
end
|
|
72
|
+
|
|
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
|
|
102
|
+
|
|
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
|
|
110
|
+
attr_reader clock: _Clock
|
|
111
|
+
|
|
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
|
|
115
|
+
|
|
116
|
+
def initialize: (Deadline deadline, CancelToken token, _Clock clock) -> void
|
|
117
|
+
|
|
118
|
+
# nil when there is no limit.
|
|
119
|
+
def remaining: () -> Integer?
|
|
120
|
+
def expired?: () -> bool
|
|
121
|
+
def cancelled?: () -> bool
|
|
122
|
+
def done?: () -> bool
|
|
123
|
+
def reason: () -> untyped
|
|
124
|
+
def check: () -> refusal?
|
|
125
|
+
def check!: () -> self
|
|
126
|
+
def clamp_sleep: (Integer requested) -> Integer
|
|
127
|
+
def cancel: (?untyped reason) -> cancellation
|
|
128
|
+
def on_cancel: () { (Scope) -> void } -> self
|
|
129
|
+
|
|
130
|
+
# Without a block the child is returned; with one it is unlinked on exit
|
|
131
|
+
# and the block's value is returned.
|
|
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
|
|
138
|
+
def inspect: () -> String
|
|
139
|
+
|
|
140
|
+
private
|
|
141
|
+
|
|
142
|
+
def now: () -> Integer
|
|
143
|
+
def below: (Deadline deadline) -> Scope
|
|
144
|
+
| [T] (Deadline deadline) { (Scope) -> T } -> T
|
|
145
|
+
def state: () -> String
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def self.clock: () -> _Clock
|
|
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
|
|
159
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: deadline_machines
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: aarch64-linux
|
|
6
|
+
authors:
|
|
7
|
+
- Abdelkader Boudih
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: 'A monotonic deadline that derives child budgets, cancels down its tree,
|
|
14
|
+
and raises only at checkpoints you choose. The arithmetic comes from the no_std
|
|
15
|
+
deadline-machines crate; Ruby owns the cancellation tree and the ambient scope.
|
|
16
|
+
The shared time contract for the machines family: retries, circuit breakers and
|
|
17
|
+
throttles read one budget.'
|
|
18
|
+
email:
|
|
19
|
+
- oss@seuros.com
|
|
20
|
+
executables: []
|
|
21
|
+
extensions: []
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
files:
|
|
24
|
+
- CHANGELOG.md
|
|
25
|
+
- LICENSE.txt
|
|
26
|
+
- README.md
|
|
27
|
+
- ext/deadline_machines_native/core/CHANGELOG.md
|
|
28
|
+
- ext/deadline_machines_native/core/README.md
|
|
29
|
+
- ext/deadline_machines_native/extconf.rb
|
|
30
|
+
- lib/deadline_machines.rb
|
|
31
|
+
- lib/deadline_machines/cancel_token.rb
|
|
32
|
+
- lib/deadline_machines/clock.rb
|
|
33
|
+
- lib/deadline_machines/errors.rb
|
|
34
|
+
- lib/deadline_machines/scope.rb
|
|
35
|
+
- lib/deadline_machines/version.rb
|
|
36
|
+
- lib/deadline_machines_native/deadline_machines_native.so
|
|
37
|
+
- sig/deadline_machines.rbs
|
|
38
|
+
homepage: https://github.com/seuros/deadline_machines
|
|
39
|
+
licenses:
|
|
40
|
+
- MIT
|
|
41
|
+
metadata:
|
|
42
|
+
source_code_uri: https://github.com/seuros/deadline_machines
|
|
43
|
+
changelog_uri: https://github.com/seuros/deadline_machines/blob/main/CHANGELOG.md
|
|
44
|
+
bug_tracker_uri: https://github.com/seuros/deadline_machines/issues
|
|
45
|
+
rubygems_mfa_required: 'true'
|
|
46
|
+
cargo_crate_name: deadline_machines_native
|
|
47
|
+
cargo_manifest_path: ext/deadline_machines_native/ffi/Cargo.toml
|
|
48
|
+
post_install_message:
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: '4.0'
|
|
57
|
+
- - "<"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: 4.1.dev
|
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '0'
|
|
65
|
+
requirements: []
|
|
66
|
+
rubygems_version: 3.5.23
|
|
67
|
+
signing_key:
|
|
68
|
+
specification_version: 4
|
|
69
|
+
summary: Deadline propagation and cooperative cancellation for Ruby
|
|
70
|
+
test_files: []
|