philiprehberger-timeout_kit 0.2.1 → 0.4.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +21 -0
- data/lib/philiprehberger/timeout_kit/deadline.rb +29 -1
- data/lib/philiprehberger/timeout_kit/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9dfc9b63782aba549dbc26a8ee6d86dda684966ecdad05feef37e76c76b711ef
|
|
4
|
+
data.tar.gz: 701cb726e36b5fc249ba29e4f21b4c870e4a1f26239b67661ec6e749798876c2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 97067f5fb32a46696cc861eacdf1f4f986b4031694affc5a7ee905666cfd240e05323beebd32819c6b5b7cdc220b1f91b29c88e68208a7ff159d44504df86d38
|
|
7
|
+
data.tar.gz: 2da040fd6f4c14d1dff37ae0524bea4d12e65510aa888eed55410c6cf3c0453380c8b0b83da22fecab7d51b1d2f763bdb6401e746461d3b9706212e34baaec37
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.4.0] - 2026-05-07
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `Deadline#duration` — exposes the original budget passed to `Deadline.new`.
|
|
14
|
+
- `Deadline#progress` — fraction of the budget consumed, useful for progress bars and adaptive cancellation.
|
|
15
|
+
|
|
16
|
+
## [0.3.0] - 2026-04-17
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
- `Deadline#elapsed` returns seconds since the deadline was created (complement to `#remaining`)
|
|
20
|
+
|
|
10
21
|
## [0.2.1] - 2026-03-31
|
|
11
22
|
|
|
12
23
|
### Changed
|
data/README.md
CHANGED
|
@@ -49,6 +49,24 @@ Philiprehberger::TimeoutKit.deadline(10) do |d|
|
|
|
49
49
|
end
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
### Measuring elapsed time
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
Philiprehberger::TimeoutKit.deadline(10) do |d|
|
|
56
|
+
do_work
|
|
57
|
+
puts "Work took #{d.elapsed}s so far"
|
|
58
|
+
# elapsed continues to grow past the budget even after expiration
|
|
59
|
+
end
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Deadline Progress
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
deadline = Philiprehberger::TimeoutKit::Deadline.new(10)
|
|
66
|
+
sleep 5
|
|
67
|
+
deadline.progress # => 0.5 (approximately)
|
|
68
|
+
```
|
|
69
|
+
|
|
52
70
|
### Nested Deadlines
|
|
53
71
|
|
|
54
72
|
```ruby
|
|
@@ -140,6 +158,9 @@ end
|
|
|
140
158
|
| `.current_deadline` | Return the current active deadline or nil |
|
|
141
159
|
| `Deadline#check!` | Raise `DeadlineExceeded` if the deadline has passed (respects grace period) |
|
|
142
160
|
| `Deadline#remaining` | Seconds remaining until the primary deadline (negative during grace) |
|
|
161
|
+
| `Deadline#elapsed` | Seconds elapsed since the deadline was created (continues past the budget after expiration) |
|
|
162
|
+
| `Deadline#duration` | The original budget passed to `Deadline.new` (Float) |
|
|
163
|
+
| `Deadline#progress` | Fraction of the budget that has elapsed (`elapsed / duration`); exceeds 1.0 after expiry |
|
|
143
164
|
| `Deadline#expired?` | Whether the primary deadline has passed |
|
|
144
165
|
| `Deadline#name` | The human-readable name for this deadline (nil if not set) |
|
|
145
166
|
| `Deadline#in_grace?` | Whether the deadline is in the grace period |
|
|
@@ -13,6 +13,11 @@ module Philiprehberger
|
|
|
13
13
|
# @return [String, nil] the human-readable name for this deadline
|
|
14
14
|
attr_reader :name
|
|
15
15
|
|
|
16
|
+
# The original budget passed to {.new} as +seconds+.
|
|
17
|
+
#
|
|
18
|
+
# @return [Float] the original deadline duration in seconds
|
|
19
|
+
attr_reader :duration
|
|
20
|
+
|
|
16
21
|
# Create a new deadline.
|
|
17
22
|
#
|
|
18
23
|
# @param seconds [Numeric] the number of seconds until the deadline expires
|
|
@@ -20,7 +25,9 @@ module Philiprehberger
|
|
|
20
25
|
# @param grace [Numeric, nil] optional grace period in seconds after the primary deadline
|
|
21
26
|
# @param on_expire [Proc, nil] optional callback that fires once when expiry is detected
|
|
22
27
|
def initialize(seconds, name: nil, grace: nil, on_expire: nil)
|
|
23
|
-
@
|
|
28
|
+
@started_at = now
|
|
29
|
+
@duration = seconds.to_f
|
|
30
|
+
@expires_at = @started_at + seconds
|
|
24
31
|
@name = name
|
|
25
32
|
@grace_seconds = grace
|
|
26
33
|
@grace_expires_at = @grace_seconds ? @expires_at + @grace_seconds : nil
|
|
@@ -65,6 +72,27 @@ module Philiprehberger
|
|
|
65
72
|
end
|
|
66
73
|
end
|
|
67
74
|
|
|
75
|
+
# Return the number of seconds elapsed since the deadline was created.
|
|
76
|
+
# This is a pure wall-clock reading from the monotonic clock and continues
|
|
77
|
+
# to increase past the original budget after expiration. Independent of
|
|
78
|
+
# {#expired?} and {#in_grace?}.
|
|
79
|
+
#
|
|
80
|
+
# @return [Float] seconds elapsed since creation
|
|
81
|
+
def elapsed
|
|
82
|
+
now - @started_at
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Fraction of the original budget that has elapsed (0.0..1.0+).
|
|
86
|
+
# Exceeds 1.0 once the primary deadline has expired (and during grace period).
|
|
87
|
+
# Returns 1.0 if the original budget is zero.
|
|
88
|
+
#
|
|
89
|
+
# @return [Float] elapsed / duration
|
|
90
|
+
def progress
|
|
91
|
+
return 1.0 if @duration.zero?
|
|
92
|
+
|
|
93
|
+
elapsed / @duration
|
|
94
|
+
end
|
|
95
|
+
|
|
68
96
|
# Return the remaining time in the grace period.
|
|
69
97
|
#
|
|
70
98
|
# @return [Float] seconds remaining in grace period (0.0 if no grace period or grace expired)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: philiprehberger-timeout_kit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Philip Rehberger
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-05-07 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A cooperative timeout library providing deadline and timeout patterns
|
|
14
14
|
that avoid Thread.raise, with nested deadline support, grace periods, callbacks,
|
|
@@ -25,11 +25,11 @@ files:
|
|
|
25
25
|
- lib/philiprehberger/timeout_kit.rb
|
|
26
26
|
- lib/philiprehberger/timeout_kit/deadline.rb
|
|
27
27
|
- lib/philiprehberger/timeout_kit/version.rb
|
|
28
|
-
homepage: https://
|
|
28
|
+
homepage: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-timeout_kit
|
|
29
29
|
licenses:
|
|
30
30
|
- MIT
|
|
31
31
|
metadata:
|
|
32
|
-
homepage_uri: https://
|
|
32
|
+
homepage_uri: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-timeout_kit
|
|
33
33
|
source_code_uri: https://github.com/philiprehberger/rb-timeout-kit
|
|
34
34
|
changelog_uri: https://github.com/philiprehberger/rb-timeout-kit/blob/main/CHANGELOG.md
|
|
35
35
|
bug_tracker_uri: https://github.com/philiprehberger/rb-timeout-kit/issues
|