wheel_machines 0.7.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 +7 -0
- data/CHANGELOG.md +95 -0
- data/Cargo.lock +314 -0
- data/Cargo.toml +28 -0
- data/LICENSE.txt +21 -0
- data/README.md +316 -0
- data/ext/wheel_machines_native/core/CHANGELOG.md +70 -0
- data/ext/wheel_machines_native/core/Cargo.toml +20 -0
- data/ext/wheel_machines_native/core/README.md +119 -0
- data/ext/wheel_machines_native/core/src/lib.rs +535 -0
- data/ext/wheel_machines_native/core/src/test_support.rs +56 -0
- data/ext/wheel_machines_native/core/src/values.rs +104 -0
- data/ext/wheel_machines_native/extconf.rb +2 -0
- data/ext/wheel_machines_native/ffi/Cargo.toml +21 -0
- data/ext/wheel_machines_native/ffi/extconf.rb +14 -0
- data/ext/wheel_machines_native/ffi/src/init.rs +43 -0
- data/ext/wheel_machines_native/ffi/src/lib.rs +206 -0
- data/ext/wheel_machines_native/ffi/src/values.rs +105 -0
- data/lib/wheel_machines/version.rb +5 -0
- data/lib/wheel_machines.rb +65 -0
- data/sig/wheel_machines.rbs +89 -0
- metadata +87 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "wheel_machines_native"
|
|
3
|
+
version = "0.1.7"
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
rust-version.workspace = true
|
|
6
|
+
license.workspace = true
|
|
7
|
+
publish = false
|
|
8
|
+
|
|
9
|
+
[lib]
|
|
10
|
+
crate-type = ["cdylib"]
|
|
11
|
+
test = false
|
|
12
|
+
doctest = false
|
|
13
|
+
|
|
14
|
+
[dependencies]
|
|
15
|
+
wheel-machines.workspace = true
|
|
16
|
+
magnus.workspace = true
|
|
17
|
+
rb-sys.workspace = true
|
|
18
|
+
matryona-machines = { workspace = true, features = ["ruby"] }
|
|
19
|
+
|
|
20
|
+
[lints]
|
|
21
|
+
workspace = true
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'mkmf'
|
|
3
|
+
require 'pathname'
|
|
4
|
+
require 'rb_sys/mkmf'
|
|
5
|
+
|
|
6
|
+
create_rust_makefile('wheel_machines_native/wheel_machines_native') do |e|
|
|
7
|
+
e.ext_dir = Pathname(__dir__).relative_path_from(Pathname(Dir.pwd)).to_s
|
|
8
|
+
e.profile = ENV.fetch('RB_SYS_CARGO_PROFILE', :release).to_sym
|
|
9
|
+
end
|
|
10
|
+
makefile = File.join(Dir.pwd, 'Makefile')
|
|
11
|
+
File.write(makefile, File.read(makefile).gsub(
|
|
12
|
+
/^RB_SYS_CARGO_MANIFEST_DIR \?=.*$/,
|
|
13
|
+
"RB_SYS_CARGO_MANIFEST_DIR ?= #{File.expand_path(__dir__)}"
|
|
14
|
+
))
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//! Private native entry point.
|
|
2
|
+
use crate::{
|
|
3
|
+
CAPACITY, Issued, Wheel,
|
|
4
|
+
values::{checked_drain, checked_due},
|
|
5
|
+
};
|
|
6
|
+
use magnus::{Error, Ruby, function, method, prelude::*};
|
|
7
|
+
use matryona_machines::ruby::Class;
|
|
8
|
+
use wheel_machines::MAX_CASCADES;
|
|
9
|
+
|
|
10
|
+
#[magnus::init]
|
|
11
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
12
|
+
let module = ruby.define_module("WheelMachines")?;
|
|
13
|
+
module.const_set("CAPACITY", CAPACITY)?;
|
|
14
|
+
module.const_set("MAX_CASCADES", MAX_CASCADES)?;
|
|
15
|
+
module.define_singleton_method("__due", function!(checked_due, 2))?;
|
|
16
|
+
module.define_singleton_method("__drain", function!(checked_drain, 3))?;
|
|
17
|
+
let handle = module.define_class("Handle", ruby.class_object())?;
|
|
18
|
+
handle.undef_default_alloc_func();
|
|
19
|
+
handle.define_method("scope", method!(Issued::scope, 0))?;
|
|
20
|
+
crate::values::resolve(ruby, module, handle)?;
|
|
21
|
+
let class = module.define_class("Wheel", ruby.class_object())?;
|
|
22
|
+
class.undef_default_alloc_func();
|
|
23
|
+
class.define_singleton_method("new", function!(Wheel::new, 0))?;
|
|
24
|
+
class.define_singleton_method("with_scope", function!(Wheel::with_scope, 1))?;
|
|
25
|
+
class.define_singleton_method("starting_at", function!(Wheel::starting_at, 2))?;
|
|
26
|
+
class.define_singleton_method("resumed", function!(Wheel::resumed, 3))?;
|
|
27
|
+
class.define_method("scope", method!(Wheel::scope, 0))?;
|
|
28
|
+
class.define_method("issued", method!(Wheel::issued, 0))?;
|
|
29
|
+
class.define_method("register", method!(Wheel::register, 2))?;
|
|
30
|
+
class.define_method("cancel", method!(Wheel::cancel, 1))?;
|
|
31
|
+
class.define_method("deadline", method!(Wheel::deadline, 1))?;
|
|
32
|
+
class.define_method("advance", method!(Wheel::advance, 1))?;
|
|
33
|
+
class.define_method("drain", method!(Wheel::drain, 2))?;
|
|
34
|
+
class.define_method("tick", method!(Wheel::tick, 3))?;
|
|
35
|
+
class.define_method("now", method!(Wheel::now, 0))?;
|
|
36
|
+
class.define_method("size", method!(Wheel::size, 0))?;
|
|
37
|
+
class.define_method("empty?", method!(Wheel::is_empty, 0))?;
|
|
38
|
+
class.define_method("next_wake", method!(Wheel::next_wake, 0))?;
|
|
39
|
+
class.define_method("capacity", function!(Wheel::capacity, 0))?;
|
|
40
|
+
// `new`, `with_scope`, `starting_at` and `resumed` answer a Wheel: its
|
|
41
|
+
// class is fixed now, like the handle's.
|
|
42
|
+
Class::pin_wrapped::<Wheel>(ruby, class).map(drop)
|
|
43
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
//! Thin binding to the bounded hierarchical timing wheel.
|
|
2
|
+
mod init;
|
|
3
|
+
mod values;
|
|
4
|
+
|
|
5
|
+
use std::cell::RefCell;
|
|
6
|
+
|
|
7
|
+
use magnus::{Error, Ruby, Value, prelude::*, typed_data::Obj};
|
|
8
|
+
use matryona_machines::ruby::{SymbolNames, class_name, exact_typed_with, integer, keep, symbol};
|
|
9
|
+
use wheel_machines::{Drain, Due, Handle, Refusal};
|
|
10
|
+
|
|
11
|
+
use crate::values::Classes;
|
|
12
|
+
|
|
13
|
+
const CAPACITY: usize = 1024;
|
|
14
|
+
|
|
15
|
+
type Core = wheel_machines::Wheel<u64, CAPACITY>;
|
|
16
|
+
|
|
17
|
+
matryona_machines::symbols! {
|
|
18
|
+
/// The symbol each refusal answers as. `Regressed` carries data, so the
|
|
19
|
+
/// table names refusals and never parses one back.
|
|
20
|
+
Refusals for Refusal as "a refusal" => names_only {
|
|
21
|
+
Elapsed => "elapsed",
|
|
22
|
+
Full => "full",
|
|
23
|
+
Exhausted => "exhausted",
|
|
24
|
+
Regressed { .. } => "regressed",
|
|
25
|
+
Stale => "stale",
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// A refusal as the Ruby symbol it answers.
|
|
30
|
+
fn refused(ruby: &Ruby, refused: Refusal) -> Value {
|
|
31
|
+
Refusals::symbol(ruby, &refused).as_value()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// `WheelMachines::Handle`: the core's `Handle`. It carries the scope of the
|
|
35
|
+
/// wheel that issued it; the core refuses it anywhere else.
|
|
36
|
+
#[magnus::wrap(class = "WheelMachines::Handle", free_immediately, size)]
|
|
37
|
+
struct Issued(Handle);
|
|
38
|
+
|
|
39
|
+
impl Issued {
|
|
40
|
+
const fn scope(&self) -> u16 {
|
|
41
|
+
self.0.scope()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// The core handle `value` wraps: exactly a `WheelMachines::Handle`, a
|
|
45
|
+
/// subclass instance refused with `TypeError` naming the class received.
|
|
46
|
+
fn from(ruby: &Ruby, value: Value) -> Result<Handle, Error> {
|
|
47
|
+
let class = Classes::get(ruby)?.handle.get(ruby);
|
|
48
|
+
exact_typed_with::<Self>(ruby, value, class, || {
|
|
49
|
+
format!(
|
|
50
|
+
"handle must be a WheelMachines::Handle, not {}",
|
|
51
|
+
class_name(value)
|
|
52
|
+
)
|
|
53
|
+
})
|
|
54
|
+
.map(|issued| issued.0)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// Drain `core` into a buffer no larger than what can possibly be due.
|
|
59
|
+
fn drain_core(core: &mut Core, budget: usize, limit: usize) -> (Drain, Vec<Due<u64>>) {
|
|
60
|
+
let mut due = vec![Due::default(); limit.min(core.len())];
|
|
61
|
+
let drain = core.drain(budget, &mut due);
|
|
62
|
+
due.truncate(drain.emitted());
|
|
63
|
+
(drain, due)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#[magnus::wrap(class = "WheelMachines::Wheel", free_immediately, size)]
|
|
67
|
+
struct Wheel {
|
|
68
|
+
core: RefCell<Box<Core>>,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
impl Wheel {
|
|
72
|
+
fn new() -> Self {
|
|
73
|
+
Self::build(Core::new())
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
fn with_scope(scope: Value) -> Result<Self, Error> {
|
|
77
|
+
Ok(Self::build(Core::with_scope(integer(scope, "scope")?)))
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fn starting_at(scope: Value, now: Value) -> Result<Self, Error> {
|
|
81
|
+
let (scope, now) = (integer(scope, "scope")?, integer(now, "now")?);
|
|
82
|
+
Ok(Self::build(Core::starting_at(scope, now)))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
fn resumed(ruby: &Ruby, scope: Value, issued: Value, now: Value) -> Result<Self, Error> {
|
|
86
|
+
let scope = integer(scope, "scope")?;
|
|
87
|
+
let (issued, now) = (integer(issued, "issued")?, integer(now, "now")?);
|
|
88
|
+
Core::resumed(scope, issued, now)
|
|
89
|
+
.map(Self::build)
|
|
90
|
+
.ok_or_else(|| {
|
|
91
|
+
Error::new(
|
|
92
|
+
ruby.exception_range_error(),
|
|
93
|
+
"issued is past the last sequence of a scope (2**48 - 1)",
|
|
94
|
+
)
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
fn build(core: Core) -> Self {
|
|
99
|
+
Self {
|
|
100
|
+
core: RefCell::new(Box::new(core)),
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
fn scope(&self) -> u16 {
|
|
105
|
+
self.core.borrow().scope()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
fn issued(&self) -> u64 {
|
|
109
|
+
self.core.borrow().issued()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
fn register(ruby: &Ruby, this: Obj<Self>, id: Value, deadline: Value) -> Result<Value, Error> {
|
|
113
|
+
this.check_frozen()?;
|
|
114
|
+
let (id, deadline) = (integer(id, "id")?, integer(deadline, "deadline")?);
|
|
115
|
+
let outcome = this.core.borrow_mut().register(id, deadline);
|
|
116
|
+
Ok(match outcome {
|
|
117
|
+
Ok(handle) => ruby.obj_wrap(Issued(handle)).as_value(),
|
|
118
|
+
Err(refusal) => refused(ruby, refusal),
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fn cancel(ruby: &Ruby, this: Obj<Self>, issued: Value) -> Result<Value, Error> {
|
|
123
|
+
this.check_frozen()?;
|
|
124
|
+
let handle = Issued::from(ruby, issued)?;
|
|
125
|
+
let outcome = this.core.borrow_mut().cancel(handle);
|
|
126
|
+
Ok(match outcome {
|
|
127
|
+
Ok(id) => ruby.integer_from_u64(id).as_value(),
|
|
128
|
+
Err(refusal) => refused(ruby, refusal),
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
fn deadline(ruby: &Ruby, this: &Self, issued: Value) -> Result<Option<u64>, Error> {
|
|
133
|
+
let handle = Issued::from(ruby, issued)?;
|
|
134
|
+
Ok(this.core.borrow().deadline(handle))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
fn advance(ruby: &Ruby, this: Obj<Self>, now: Value) -> Result<Value, Error> {
|
|
138
|
+
this.check_frozen()?;
|
|
139
|
+
let now = integer(now, "now")?;
|
|
140
|
+
let outcome = this.core.borrow_mut().advance(now);
|
|
141
|
+
Ok(outcome.map_or_else(
|
|
142
|
+
|refusal| refused(ruby, refusal),
|
|
143
|
+
|()| symbol(ruby, "advanced").as_value(),
|
|
144
|
+
))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/// The answer is built from a copy of the wheel kept only once it exists
|
|
148
|
+
/// (see [`keep`]): a raise while building leaves every due entry in it.
|
|
149
|
+
fn drain(ruby: &Ruby, this: Obj<Self>, budget: Value, limit: Value) -> Result<Value, Error> {
|
|
150
|
+
this.check_frozen()?;
|
|
151
|
+
let (budget, limit) = (integer(budget, "budget")?, integer(limit, "limit")?);
|
|
152
|
+
// Everything that can raise happens before the core drains.
|
|
153
|
+
let classes = Classes::get(ruby)?;
|
|
154
|
+
keep(
|
|
155
|
+
ruby,
|
|
156
|
+
this,
|
|
157
|
+
&this.core,
|
|
158
|
+
|core| drain_core(core, budget, limit),
|
|
159
|
+
|(drain, due)| classes.report(ruby, drain, &due),
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
fn tick(
|
|
164
|
+
ruby: &Ruby,
|
|
165
|
+
this: Obj<Self>,
|
|
166
|
+
now: Value,
|
|
167
|
+
budget: Value,
|
|
168
|
+
limit: Value,
|
|
169
|
+
) -> Result<Value, Error> {
|
|
170
|
+
this.check_frozen()?;
|
|
171
|
+
let now = integer(now, "now")?;
|
|
172
|
+
let (budget, limit) = (integer(budget, "budget")?, integer(limit, "limit")?);
|
|
173
|
+
// Everything that can raise happens before the core moves.
|
|
174
|
+
let classes = Classes::get(ruby)?;
|
|
175
|
+
keep(
|
|
176
|
+
ruby,
|
|
177
|
+
this,
|
|
178
|
+
&this.core,
|
|
179
|
+
|core| core.advance(now).map(|()| drain_core(core, budget, limit)),
|
|
180
|
+
|ticked| match ticked {
|
|
181
|
+
Ok((drain, due)) => classes.report(ruby, drain, &due),
|
|
182
|
+
Err(refusal) => Ok(refused(ruby, refusal)),
|
|
183
|
+
},
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
fn now(&self) -> u64 {
|
|
188
|
+
self.core.borrow().now()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
fn size(&self) -> usize {
|
|
192
|
+
self.core.borrow().len()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
fn is_empty(&self) -> bool {
|
|
196
|
+
self.core.borrow().is_empty()
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
fn next_wake(&self) -> Option<u64> {
|
|
200
|
+
self.core.borrow().next_wake()
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const fn capacity() -> usize {
|
|
204
|
+
CAPACITY
|
|
205
|
+
}
|
|
206
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
//! `Due` and `Drain` values: validated through the core at construction, and
|
|
2
|
+
//! built from what a drain answered. Both classes are resolved once, at init,
|
|
3
|
+
//! and pinned.
|
|
4
|
+
use magnus::{Error, RArray, RClass, RModule, Ruby, Symbol, Value, prelude::*};
|
|
5
|
+
use matryona_machines::ruby::{Class, Registry, Symbols, exact_array, exact_fields, integer};
|
|
6
|
+
use wheel_machines::{Drain, Due, Status};
|
|
7
|
+
|
|
8
|
+
use crate::Issued;
|
|
9
|
+
|
|
10
|
+
/// `Due` and `Drain`, as `lib/wheel_machines.rb` defined them before the
|
|
11
|
+
/// extension loaded, and `Handle`, which init defines and pins as magnus's
|
|
12
|
+
/// class for every handle: `register` builds one after the core moved, and
|
|
13
|
+
/// building it must run no Ruby (no `Object.const_get`).
|
|
14
|
+
pub struct Classes {
|
|
15
|
+
pub handle: Class,
|
|
16
|
+
due: Class,
|
|
17
|
+
drain: Class,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static CLASSES: Registry<Classes> = Registry::new("wheel_machines");
|
|
21
|
+
|
|
22
|
+
pub fn resolve(ruby: &Ruby, module: RModule, handle: RClass) -> Result<(), Error> {
|
|
23
|
+
let classes = Classes {
|
|
24
|
+
handle: Class::pin_wrapped::<Issued>(ruby, handle)?,
|
|
25
|
+
due: Class::resolve(ruby, module, "Due")?,
|
|
26
|
+
drain: Class::resolve(ruby, module, "Drain")?,
|
|
27
|
+
};
|
|
28
|
+
CLASSES.set(ruby, classes).map(drop)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
impl Classes {
|
|
32
|
+
/// The gem's own `Data` classes, resolved at init.
|
|
33
|
+
pub fn get(ruby: &Ruby) -> Result<&'static Self, Error> {
|
|
34
|
+
CLASSES.get(ruby)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Build a `WheelMachines::Drain` holding `WheelMachines::Due` values.
|
|
38
|
+
pub fn report(&self, ruby: &Ruby, drain: Drain, due: &[Due<u64>]) -> Result<Value, Error> {
|
|
39
|
+
let (due_class, drain_class) = (self.due.get(ruby), self.drain.get(ruby));
|
|
40
|
+
let rows = ruby.ary_new_capa(due.len());
|
|
41
|
+
for entry in due {
|
|
42
|
+
let row: Value = due_class.funcall("new", (entry.id, entry.deadline))?;
|
|
43
|
+
rows.push(row)?;
|
|
44
|
+
}
|
|
45
|
+
rows.freeze();
|
|
46
|
+
let status = Statuses::symbol(ruby, drain.status());
|
|
47
|
+
drain_class.funcall("new", (rows, drain.cascaded(), status))
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
matryona_machines::symbols! {
|
|
52
|
+
/// Why a drain stopped, as the Symbol `Drain#status` holds.
|
|
53
|
+
Statuses for Status as "status" {
|
|
54
|
+
CaughtUp => "caught_up",
|
|
55
|
+
OutputFull => "output_full",
|
|
56
|
+
BudgetSpent => "budget_spent",
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// What `Due.new` stores: an id and a deadline the core's `Due::new` holds.
|
|
61
|
+
pub fn checked_due(id: Value, deadline: Value) -> Result<(u64, u64), Error> {
|
|
62
|
+
let due = Due::new(integer(id, "id")?, integer(deadline, "deadline")?);
|
|
63
|
+
Ok((due.id, due.deadline))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// What `Drain.new` stores: a frozen Array of `WheelMachines::Due`, a cascade
|
|
67
|
+
/// count and a status symbol the core's `Drain::new` accepts.
|
|
68
|
+
pub fn checked_drain(
|
|
69
|
+
ruby: &Ruby,
|
|
70
|
+
due: Value,
|
|
71
|
+
cascaded: Value,
|
|
72
|
+
status: Value,
|
|
73
|
+
) -> Result<(RArray, usize, Symbol), Error> {
|
|
74
|
+
let cascaded = integer(cascaded, "cascaded")?;
|
|
75
|
+
// Read natively from exactly an Array: a subclass is a `TypeError`.
|
|
76
|
+
let rows = exact_array(ruby, due, NOT_DUE)?;
|
|
77
|
+
let due_class = Classes::get(ruby)?.due;
|
|
78
|
+
let copy = ruby.ary_new_capa(rows.len());
|
|
79
|
+
for row in rows {
|
|
80
|
+
copy.push(due_row(ruby, row, due_class)?)?;
|
|
81
|
+
}
|
|
82
|
+
copy.freeze();
|
|
83
|
+
let status = Statuses::parse(ruby, status)?;
|
|
84
|
+
let symbol = Statuses::symbol(ruby, status);
|
|
85
|
+
Drain::new(copy.len(), cascaded, status)
|
|
86
|
+
.map(|_| (copy, cascaded, symbol))
|
|
87
|
+
.ok_or_else(|| {
|
|
88
|
+
Error::new(
|
|
89
|
+
ruby.exception_range_error(),
|
|
90
|
+
"emitted plus cascaded is past usize: no drain charges that much work",
|
|
91
|
+
)
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const NOT_DUE: &str = "due must be an Array of WheelMachines::Due";
|
|
96
|
+
|
|
97
|
+
/// `row` when it is exactly a `WheelMachines::Due` (a subclass's constant
|
|
98
|
+
/// leaks past the seal) whose fields the core's `Due::new` holds: read
|
|
99
|
+
/// straight from the struct, since `Marshal.load` skips `initialize`.
|
|
100
|
+
/// `TypeError` or `RangeError` otherwise.
|
|
101
|
+
fn due_row(ruby: &Ruby, row: Value, due_class: Class) -> Result<Value, Error> {
|
|
102
|
+
let [id, deadline] = exact_fields(ruby, row, due_class.get(ruby), NOT_DUE)?;
|
|
103
|
+
checked_due(id, deadline)?;
|
|
104
|
+
Ok(row)
|
|
105
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'wheel_machines/version'
|
|
4
|
+
|
|
5
|
+
module WheelMachines
|
|
6
|
+
# One due registration: the caller's id and the tick it was registered for.
|
|
7
|
+
# Like the core's `Due::new`, both are Integers in 0..2**64-1: TypeError for
|
|
8
|
+
# a non-Integer, RangeError outside the range.
|
|
9
|
+
Due = Data.define(:id, :deadline) do
|
|
10
|
+
def initialize(id:, deadline:)
|
|
11
|
+
id, deadline = WheelMachines.__send__(:__due, id, deadline)
|
|
12
|
+
super
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# What one drain did: the due entries in order, the cascade moves charged, and
|
|
17
|
+
# why it stopped (:caught_up, :output_full or :budget_spent). The core writes
|
|
18
|
+
# the entries to a caller's buffer and reports `emitted`; here `due` is that
|
|
19
|
+
# buffer, cut to the entries written, stored as a frozen copy.
|
|
20
|
+
#
|
|
21
|
+
# Validated by the core's `Drain::new`: `due` must be an Array of `Due` and
|
|
22
|
+
# `status` a Symbol (TypeError otherwise); an unknown status, a negative
|
|
23
|
+
# `cascaded`, or work past the core's `usize` raises RangeError.
|
|
24
|
+
Drain = Data.define(:due, :cascaded, :status) do
|
|
25
|
+
def initialize(due:, cascaded:, status:)
|
|
26
|
+
due, cascaded, status = WheelMachines.__send__(:__drain, due, cascaded, status)
|
|
27
|
+
super
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Due entries handed out, the core's `emitted`.
|
|
31
|
+
def emitted = due.size
|
|
32
|
+
|
|
33
|
+
# Work units charged against the budget.
|
|
34
|
+
def work = emitted + cascaded
|
|
35
|
+
|
|
36
|
+
def caught_up? = status == :caught_up
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Native is required: no algorithm fallback. It validates and builds the Due
|
|
41
|
+
# and Drain values above, so those classes must exist first.
|
|
42
|
+
require 'wheel_machines_native/wheel_machines_native'
|
|
43
|
+
|
|
44
|
+
module WheelMachines
|
|
45
|
+
private_class_method :__due, :__drain
|
|
46
|
+
|
|
47
|
+
# The values are sealed. `initialize` is where each validates, and a
|
|
48
|
+
# subclass could override it, or `allocate` build an instance that never ran
|
|
49
|
+
# it: subclassing raises TypeError and `allocate` is gone. Sealing stops
|
|
50
|
+
# accidents, not reflection (a named subclass's constant is bound before
|
|
51
|
+
# `inherited` raises; `Marshal.load` skips `initialize`), so the native side
|
|
52
|
+
# is the authority: it takes a `Due` only of exactly that class and checks
|
|
53
|
+
# its fields again.
|
|
54
|
+
[Due, Drain].each do |value|
|
|
55
|
+
value.singleton_class.undef_method(:allocate)
|
|
56
|
+
value.define_singleton_method(:inherited) { |_| raise TypeError, "#{value} cannot be subclassed" }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Wheel and Handle wrap native state and have no allocator: a subclass's
|
|
60
|
+
# `new` (or `with_scope`) would build the base class anyway, so subclassing
|
|
61
|
+
# raises too.
|
|
62
|
+
[Wheel, Handle].each do |native|
|
|
63
|
+
native.define_singleton_method(:inherited) { |_| raise TypeError, "#{native} cannot be subclassed" }
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
module WheelMachines
|
|
2
|
+
VERSION: String
|
|
3
|
+
CAPACITY: Integer
|
|
4
|
+
MAX_CASCADES: Integer
|
|
5
|
+
|
|
6
|
+
# Private native checks behind `Due.new` and `Drain.new`: the fields as the
|
|
7
|
+
# core holds them (`due` as a frozen copy), TypeError/RangeError for what it
|
|
8
|
+
# rejects.
|
|
9
|
+
private def self.__due: (Integer id, Integer deadline) -> [Integer, Integer]
|
|
10
|
+
private def self.__drain: (Array[Due] due, Integer cascaded, Drain::status status) -> [Array[Due], Integer, Drain::status]
|
|
11
|
+
|
|
12
|
+
# Both fields are Integers in 0..2**64-1: TypeError for a non-Integer,
|
|
13
|
+
# RangeError outside the range.
|
|
14
|
+
class Due < Data
|
|
15
|
+
attr_reader id: Integer
|
|
16
|
+
attr_reader deadline: Integer
|
|
17
|
+
def self.new: (Integer id, Integer deadline) -> Due
|
|
18
|
+
| (id: Integer, deadline: Integer) -> Due
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Validated by the core's `Drain::new`: TypeError when `due` is not an
|
|
22
|
+
# Array of Due or `status` not a Symbol; RangeError for an unknown status, a
|
|
23
|
+
# negative `cascaded`, or work past the core's usize. `due` is stored as a
|
|
24
|
+
# frozen copy.
|
|
25
|
+
class Drain < Data
|
|
26
|
+
type status = :caught_up | :output_full | :budget_spent
|
|
27
|
+
|
|
28
|
+
attr_reader due: Array[Due]
|
|
29
|
+
attr_reader cascaded: Integer
|
|
30
|
+
attr_reader status: status
|
|
31
|
+
def self.new: (Array[Due] due, Integer cascaded, status status) -> Drain
|
|
32
|
+
| (due: Array[Due], cascaded: Integer, status: status) -> Drain
|
|
33
|
+
# The core's `emitted`: how many entries are in `due`.
|
|
34
|
+
def emitted: () -> Integer
|
|
35
|
+
def work: () -> Integer
|
|
36
|
+
def caught_up?: () -> bool
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Mirrors the core's `Handle`: names one registration on the wheel that
|
|
40
|
+
# issued it and carries that wheel's scope. Opaque; it cannot be built or
|
|
41
|
+
# copied by hand. A wheel in another scope answers :stale to it. Sealed:
|
|
42
|
+
# subclassing raises TypeError.
|
|
43
|
+
class Handle
|
|
44
|
+
# The scope of the wheel that issued it, 0..65535.
|
|
45
|
+
def scope: () -> Integer
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Sealed: no allocator, and subclassing raises TypeError.
|
|
49
|
+
class Wheel
|
|
50
|
+
# Tick zero, scope zero, nothing issued.
|
|
51
|
+
def self.new: () -> Wheel
|
|
52
|
+
# Tick zero. `scope` is an Integer in 0..65535: TypeError otherwise,
|
|
53
|
+
# RangeError outside the range.
|
|
54
|
+
def self.with_scope: (Integer scope) -> Wheel
|
|
55
|
+
def self.starting_at: (Integer scope, Integer now) -> Wheel
|
|
56
|
+
# Continues `scope` after a restart from a saved `issued` count. Restores
|
|
57
|
+
# only the handle sequence: the wheel is empty. RangeError when `issued`
|
|
58
|
+
# is above 2**48 - 1.
|
|
59
|
+
def self.resumed: (Integer scope, Integer issued, Integer now) -> Wheel
|
|
60
|
+
# The scope carried by every Handle this wheel issues.
|
|
61
|
+
def scope: () -> Integer
|
|
62
|
+
# Handles issued in this scope so far: the count to save for `resumed`.
|
|
63
|
+
def issued: () -> Integer
|
|
64
|
+
def capacity: () -> Integer
|
|
65
|
+
def size: () -> Integer
|
|
66
|
+
def empty?: () -> bool
|
|
67
|
+
def now: () -> Integer
|
|
68
|
+
def next_wake: () -> Integer?
|
|
69
|
+
# Refusals are symbols, one per core variant, and a refused call changes
|
|
70
|
+
# nothing. `Refusal::Regressed { now }` is `:regressed`; the tick it
|
|
71
|
+
# carries is `now`.
|
|
72
|
+
# Honoured: a Handle. Refused: :elapsed, :full, :exhausted.
|
|
73
|
+
def register: (Integer id, Integer deadline) -> (Handle | :elapsed | :full | :exhausted)
|
|
74
|
+
# Honoured: the id that was registered. Refused: :stale, a Handle from a
|
|
75
|
+
# wheel in another scope included.
|
|
76
|
+
def cancel: (Handle) -> (Integer | :stale)
|
|
77
|
+
# nil for a Handle that names no live registration here.
|
|
78
|
+
def deadline: (Handle) -> Integer?
|
|
79
|
+
# Honoured: :advanced (repeating the current tick included). Records
|
|
80
|
+
# `now`: the answer depends on it. Refused: :regressed.
|
|
81
|
+
def advance: (Integer now) -> (:advanced | :regressed)
|
|
82
|
+
# Never refuses and records no clock: it drains against `now`. A budget
|
|
83
|
+
# or limit past usize raises RangeError before anything is drained.
|
|
84
|
+
def drain: (Integer budget, Integer limit) -> Drain
|
|
85
|
+
# Honoured: a Drain; records `now`. Refused: :regressed. A budget or
|
|
86
|
+
# limit past usize raises RangeError before the tick moves.
|
|
87
|
+
def tick: (Integer now, Integer budget, Integer limit) -> (Drain | :regressed)
|
|
88
|
+
end
|
|
89
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: wheel_machines
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.7.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Abdelkader Boudih
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
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: 'An expiry index: register ids with the tick they expire at, cancel them
|
|
33
|
+
by handle, and drain the due ids when you tell it the time. No threads, callbacks
|
|
34
|
+
or clock.'
|
|
35
|
+
email:
|
|
36
|
+
- oss@seuros.com
|
|
37
|
+
executables: []
|
|
38
|
+
extensions:
|
|
39
|
+
- ext/wheel_machines_native/extconf.rb
|
|
40
|
+
extra_rdoc_files: []
|
|
41
|
+
files:
|
|
42
|
+
- CHANGELOG.md
|
|
43
|
+
- Cargo.lock
|
|
44
|
+
- Cargo.toml
|
|
45
|
+
- LICENSE.txt
|
|
46
|
+
- README.md
|
|
47
|
+
- ext/wheel_machines_native/core/CHANGELOG.md
|
|
48
|
+
- ext/wheel_machines_native/core/Cargo.toml
|
|
49
|
+
- ext/wheel_machines_native/core/README.md
|
|
50
|
+
- ext/wheel_machines_native/core/src/lib.rs
|
|
51
|
+
- ext/wheel_machines_native/core/src/test_support.rs
|
|
52
|
+
- ext/wheel_machines_native/core/src/values.rs
|
|
53
|
+
- ext/wheel_machines_native/extconf.rb
|
|
54
|
+
- ext/wheel_machines_native/ffi/Cargo.toml
|
|
55
|
+
- ext/wheel_machines_native/ffi/extconf.rb
|
|
56
|
+
- ext/wheel_machines_native/ffi/src/init.rs
|
|
57
|
+
- ext/wheel_machines_native/ffi/src/lib.rs
|
|
58
|
+
- ext/wheel_machines_native/ffi/src/values.rs
|
|
59
|
+
- lib/wheel_machines.rb
|
|
60
|
+
- lib/wheel_machines/version.rb
|
|
61
|
+
- sig/wheel_machines.rbs
|
|
62
|
+
homepage: https://github.com/seuros/wheel_machines
|
|
63
|
+
licenses:
|
|
64
|
+
- MIT
|
|
65
|
+
metadata:
|
|
66
|
+
source_code_uri: https://github.com/seuros/wheel_machines
|
|
67
|
+
rubygems_mfa_required: 'true'
|
|
68
|
+
cargo_crate_name: wheel_machines_native
|
|
69
|
+
cargo_manifest_path: ext/wheel_machines_native/ffi/Cargo.toml
|
|
70
|
+
rdoc_options: []
|
|
71
|
+
require_paths:
|
|
72
|
+
- lib
|
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: 4.0.0
|
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
requirements: []
|
|
84
|
+
rubygems_version: 4.0.20
|
|
85
|
+
specification_version: 4
|
|
86
|
+
summary: Hierarchical timing wheel over a native Rust core
|
|
87
|
+
test_files: []
|