kino 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/Cargo.lock +1 -1
- data/Cargo.toml +6 -2
- data/README.md +17 -0
- data/ext/kino/Cargo.toml +1 -1
- data/ext/kino/src/gvl.rs +41 -8
- data/ext/kino/src/lib.rs +5 -0
- data/ext/kino/src/pin.rs +223 -0
- data/ext/kino/src/queue.rs +19 -17
- data/ext/kino/src/registry.rs +4 -0
- data/ext/kino/src/request.rs +20 -6
- data/ext/kino/src/server.rs +12 -0
- data/ext/kino/src/test_support.rs +13 -1
- data/ext/kino/src/timer.rs +1 -1
- data/lib/kino/server.rb +87 -12
- data/lib/kino/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f66c88f09e6d65c5b9ca4b0ed4f1a10f735343286dfed32fbd54d13216faf886
|
|
4
|
+
data.tar.gz: 871d3eeaed7cf34ed8fc83a53fba42d5dfe28b916e9b6c695b332fcf17be5ab8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 22b189c9301618dfe411965cfea6f91cd7511b79265f5826e7e158110752104ec3c8c9da55c0e1dcfa4ad4f883bcca5e392505661244c030e3fac000de06fc18
|
|
7
|
+
data.tar.gz: 3ef719d98e7fcc5b5570d9dc606f24c51df1651abc4814007e48cdfa46eaf43e49cbc9680b08e344002670a51e2f9d0d29ee6f3ace2327f27cdbe4c15fc6fe6c
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [0.2.0] - 2026-07-13
|
|
2
|
+
|
|
3
|
+
- Strip debug info from release builds.
|
|
4
|
+
- A panic in the native layer now raises a RuntimeError on the affected
|
|
5
|
+
worker (visible to `on_error` and the error log) instead of killing the
|
|
6
|
+
server process.
|
|
7
|
+
- The pidfile is claimed exclusively: starting refuses (instead of silently
|
|
8
|
+
overwriting) while the pidfile's owner is alive, a leftover file from a
|
|
9
|
+
dead process is replaced, symlinks are never followed, and shutdown
|
|
10
|
+
removes the file only while it still holds our pid.
|
|
11
|
+
- Zero-copy response bodies: bodies of 4 KB and up ride to the network
|
|
12
|
+
layer by reference instead of being copied at the FFI boundary, in both
|
|
13
|
+
dispatch modes. A 10 KB-body endpoint now serves at plaintext speed.
|
|
14
|
+
|
|
1
15
|
## [0.1.3] - 2026-07-04
|
|
2
16
|
|
|
3
17
|
- Non-String response header names and values (booleans, numbers, symbols)
|
data/Cargo.lock
CHANGED
data/Cargo.toml
CHANGED
|
@@ -7,8 +7,12 @@ members = ["./ext/kino"]
|
|
|
7
7
|
resolver = "2"
|
|
8
8
|
|
|
9
9
|
[profile.release]
|
|
10
|
-
#
|
|
11
|
-
|
|
10
|
+
# Ship lean artifacts: no debug info, and leftover debug sections (e.g.
|
|
11
|
+
# from the precompiled std) are stripped. The symbol table stays, so
|
|
12
|
+
# crash backtraces keep function names. For a debuggable local build,
|
|
13
|
+
# compile with RB_SYS_CARGO_PROFILE=dev.
|
|
14
|
+
debug = false
|
|
15
|
+
strip = "debuginfo"
|
|
12
16
|
opt-level = 3
|
|
13
17
|
lto = "fat"
|
|
14
18
|
codegen-units = 1
|
data/README.md
CHANGED
|
@@ -21,6 +21,8 @@ and a threaded fallback mode runs everything else, Rails included.
|
|
|
21
21
|
small process.
|
|
22
22
|
* **Production plumbing included.** Graceful drain, crash supervision
|
|
23
23
|
and respawn, bounded queues with 503 backpressure, request timeouts,
|
|
24
|
+
hardened intake (slowloris and TLS-handshake deadlines, connection
|
|
25
|
+
and body-size caps), an `on_error` hook for your error tracker,
|
|
24
26
|
TLS (rustls), live stats, async access and app logging.
|
|
25
27
|
* **Tells you why.** `kino --check` lists exactly what blocks your app
|
|
26
28
|
from ractor mode, finding by finding, so you do not have to decode
|
|
@@ -224,6 +226,9 @@ server = Kino::Server.new(app,
|
|
|
224
226
|
queue_depth: 1024, # bounded queue; overflow → 503
|
|
225
227
|
queue_timeout: 5.0, # seconds before 503 on a full queue
|
|
226
228
|
request_timeout: nil, # seconds before a slow response becomes a 504 (nil = off)
|
|
229
|
+
max_connections: 8192, # cap concurrent connections; default: most of ulimit -n
|
|
230
|
+
max_body_size: 50 * 1024 * 1024, # bytes before a 413; nil = let a proxy handle it
|
|
231
|
+
on_error: ->(e, env) { ErrorTracker.capture(e) }, # after the client got its 500
|
|
227
232
|
shutdown_timeout: 30, # drain deadline
|
|
228
233
|
tls: { cert: "cert.pem", key: "key.pem" }, # file paths or inline PEM
|
|
229
234
|
)
|
|
@@ -303,6 +308,18 @@ is unsafe. A stuck handler still occupies its worker slot until it
|
|
|
303
308
|
returns, so set the deadline above your slowest legitimate endpoint and
|
|
304
309
|
watch `stats[:timeouts]`.
|
|
305
310
|
|
|
311
|
+
Timeouts guard your app; the network intake guards itself. New
|
|
312
|
+
connections past `max_connections` (default: most of `ulimit -n`) wait
|
|
313
|
+
in the kernel backlog; request bodies past `max_body_size` (default
|
|
314
|
+
50 MB, `nil` delegates to a fronting proxy) get a **413**; and fixed
|
|
315
|
+
deadlines drop slow-header clients (15 s), stalled TLS handshakes
|
|
316
|
+
(10 s), and uploads stalled mid-body (30 s). When a worker catches an
|
|
317
|
+
app or delivery error,
|
|
318
|
+
`on_error ->(error, env) { ErrorTracker.capture(error) }` is called
|
|
319
|
+
after the client got its 500—the only place a tracker sees errors
|
|
320
|
+
raised while the response was being written (in `:ractor` mode, build
|
|
321
|
+
the handler with `Ractor.shareable_proc`).
|
|
322
|
+
|
|
306
323
|
## Stats
|
|
307
324
|
|
|
308
325
|
`server.stats` returns a live snapshot: the configuration plus counters
|
data/ext/kino/Cargo.toml
CHANGED
data/ext/kino/src/gvl.rs
CHANGED
|
@@ -5,7 +5,11 @@
|
|
|
5
5
|
//! body reads/writes) must go through `without_gvl` so other Ruby threads
|
|
6
6
|
//! in the same ractor keep running and the VM can interrupt us via the UBF.
|
|
7
7
|
|
|
8
|
+
use std::any::Any;
|
|
8
9
|
use std::ffi::c_void;
|
|
10
|
+
use std::panic::{catch_unwind, AssertUnwindSafe};
|
|
11
|
+
|
|
12
|
+
use magnus::{Error, Ruby};
|
|
9
13
|
|
|
10
14
|
/// An unblock function: called by the Ruby VM from another thread when it
|
|
11
15
|
/// needs to interrupt the blocking region (Thread#kill, VM shutdown, ...).
|
|
@@ -20,23 +24,48 @@ unsafe extern "C" fn trampoline<F, R>(arg: *mut c_void) -> *mut c_void
|
|
|
20
24
|
where
|
|
21
25
|
F: FnOnce() -> R,
|
|
22
26
|
{
|
|
23
|
-
let slot = unsafe { &mut *(arg as *mut (Option<F>, Option<R
|
|
27
|
+
let slot = unsafe { &mut *(arg as *mut (Option<F>, Option<std::thread::Result<R>>)) };
|
|
24
28
|
let f = slot.0.take().expect("without_gvl trampoline called twice");
|
|
25
|
-
|
|
29
|
+
// A panic must not unwind into the VM's C frame beneath us (that
|
|
30
|
+
// aborts the process); catch it here and let without_gvl rethrow it
|
|
31
|
+
// as a Ruby exception once the GVL is held again.
|
|
32
|
+
slot.1 = Some(catch_unwind(AssertUnwindSafe(f)));
|
|
26
33
|
std::ptr::null_mut()
|
|
27
34
|
}
|
|
28
35
|
|
|
36
|
+
/// The caught panic payload as a plain RuntimeError, built with the GVL
|
|
37
|
+
/// held. Deliberately not magnus's own panic conversion: that raises
|
|
38
|
+
/// `fatal`, which still ends the process; a RuntimeError flows through
|
|
39
|
+
/// the worker's normal rescue path (500, on_error, error log, respawn).
|
|
40
|
+
fn panic_error(payload: Box<dyn Any + Send>) -> Error {
|
|
41
|
+
let msg = if let Some(m) = payload.downcast_ref::<&'static str>() {
|
|
42
|
+
(*m).to_string()
|
|
43
|
+
} else if let Some(m) = payload.downcast_ref::<String>() {
|
|
44
|
+
m.clone()
|
|
45
|
+
} else {
|
|
46
|
+
"opaque panic payload".to_string()
|
|
47
|
+
};
|
|
48
|
+
let ruby = Ruby::get().expect("without_gvl requires a Ruby thread");
|
|
49
|
+
Error::new(
|
|
50
|
+
ruby.exception_runtime_error(),
|
|
51
|
+
format!("Kino: panic in native blocking call: {msg}"),
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
29
55
|
/// Run `f` with the GVL released. Blocks the current Ruby thread but lets
|
|
30
56
|
/// every other Ruby thread (in this ractor) run in parallel.
|
|
31
57
|
///
|
|
32
58
|
/// `f` MUST NOT touch any Ruby API. If `ubf` fires, `f` is responsible for
|
|
33
59
|
/// noticing (e.g. a message on an interrupt channel) and returning promptly;
|
|
34
60
|
/// pending VM interrupts are then delivered once we're back in Ruby.
|
|
35
|
-
|
|
61
|
+
///
|
|
62
|
+
/// A panic in `f` comes back as `Err` (RuntimeError with the panic
|
|
63
|
+
/// message); the default panic hook still reports it to stderr first.
|
|
64
|
+
pub fn without_gvl<F, R>(f: F, ubf: Option<Ubf>) -> Result<R, Error>
|
|
36
65
|
where
|
|
37
66
|
F: FnOnce() -> R,
|
|
38
67
|
{
|
|
39
|
-
let mut slot: (Option<F>, Option<R
|
|
68
|
+
let mut slot: (Option<F>, Option<std::thread::Result<R>>) = (Some(f), None);
|
|
40
69
|
let (ubf_func, ubf_data) = match ubf {
|
|
41
70
|
Some(u) => (Some(u.func), u.data),
|
|
42
71
|
None => (None, std::ptr::null_mut()),
|
|
@@ -49,7 +78,10 @@ where
|
|
|
49
78
|
ubf_data,
|
|
50
79
|
);
|
|
51
80
|
}
|
|
52
|
-
slot.1.expect("without_gvl block did not run")
|
|
81
|
+
match slot.1.expect("without_gvl block did not run") {
|
|
82
|
+
Ok(value) => Ok(value),
|
|
83
|
+
Err(payload) => Err(panic_error(payload)),
|
|
84
|
+
}
|
|
53
85
|
}
|
|
54
86
|
|
|
55
87
|
/// The standard UBF used across this crate: `data` points at an `AtomicBool`
|
|
@@ -64,12 +96,13 @@ pub unsafe extern "C" fn ubf_interrupt(data: *mut c_void) {
|
|
|
64
96
|
/// The crate's one blocking idiom: release the GVL and run `attempt` in a
|
|
65
97
|
/// loop (`Some(T)` finishes, `None` means "tick elapsed, go around"), and
|
|
66
98
|
/// wake within a tick when `flag` is raised (by the VM's UBF or by
|
|
67
|
-
/// interrupt_all_workers). Returns None on interruption
|
|
68
|
-
/// bound each wait (recv_timeout-style)
|
|
99
|
+
/// interrupt_all_workers). Returns Ok(None) on interruption, Err when
|
|
100
|
+
/// `attempt` panicked. `attempt` must bound each wait (recv_timeout-style)
|
|
101
|
+
/// and must not touch the Ruby API.
|
|
69
102
|
pub fn interruptible<T>(
|
|
70
103
|
flag: &std::sync::atomic::AtomicBool,
|
|
71
104
|
mut attempt: impl FnMut() -> Option<T>,
|
|
72
|
-
) -> Option<T> {
|
|
105
|
+
) -> Result<Option<T>, Error> {
|
|
73
106
|
use std::sync::atomic::Ordering;
|
|
74
107
|
|
|
75
108
|
without_gvl(
|
data/ext/kino/src/lib.rs
CHANGED
|
@@ -7,6 +7,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
|
|
7
7
|
mod env_strings;
|
|
8
8
|
mod gvl;
|
|
9
9
|
mod logsink;
|
|
10
|
+
mod pin;
|
|
10
11
|
mod queue;
|
|
11
12
|
mod registry;
|
|
12
13
|
mod request;
|
|
@@ -59,6 +60,9 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
59
60
|
function!(env_strings::register_defaults, 2),
|
|
60
61
|
)?;
|
|
61
62
|
|
|
63
|
+
native.define_class("PinKeeper", ruby.class_object())?;
|
|
64
|
+
native.define_singleton_method("pin_keeper", function!(server::pin_keeper, 1))?;
|
|
65
|
+
|
|
62
66
|
let request = native.define_class("Request", ruby.class_object())?;
|
|
63
67
|
request.define_method("respond_and_take", method!(queue::respond_and_take, 6))?;
|
|
64
68
|
request.define_method(
|
|
@@ -85,6 +89,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
85
89
|
native.define_singleton_method("_test_push", function!(test_support::push, 2))?;
|
|
86
90
|
native.define_singleton_method("_test_take", function!(test_support::take, 1))?;
|
|
87
91
|
native.define_singleton_method("_test_close", function!(test_support::close, 1))?;
|
|
92
|
+
native.define_singleton_method("_test_panic", function!(test_support::panic_in_release, 0))?;
|
|
88
93
|
|
|
89
94
|
Ok(())
|
|
90
95
|
}
|
data/ext/kino/src/pin.rs
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
//! Zero-copy response bodies: a large body's bytes ride to hyper in
|
|
2
|
+
//! place instead of being copied at the FFI boundary, with the backing
|
|
3
|
+
//! Ruby string kept alive until hyper drops the buffer.
|
|
4
|
+
//!
|
|
5
|
+
//! Buffer stability is the same mechanism io.c uses to hold a string
|
|
6
|
+
//! across a GVL-released write: `rb_str_tmp_frozen_acquire` returns a
|
|
7
|
+
//! frozen string sharing the original's byte buffer (a frozen original
|
|
8
|
+
//! is returned as-is). Mutating the original afterwards copy-on-writes
|
|
9
|
+
//! the ORIGINAL side, so the shared buffer stays byte-stable for as
|
|
10
|
+
//! long as the acquired string lives.
|
|
11
|
+
//!
|
|
12
|
+
//! Liveness is a fixed slab of atomic VALUE slots per server, marked
|
|
13
|
+
//! from a `PinKeeper` TypedData object that the Ruby `Kino::Server`
|
|
14
|
+
//! holds for the server's lifetime (pin.rs never registers global GC
|
|
15
|
+
//! roots: `rb_gc_register_address` is not ractor-safe in Ruby 4.0).
|
|
16
|
+
//! The mark hook uses the pinning `rb_gc_mark`, so compaction never
|
|
17
|
+
//! moves a pinned string either. Because the keeper lives on the main
|
|
18
|
+
//! ractor, pinned buffers survive worker-ractor crashes while hyper is
|
|
19
|
+
//! still flushing them.
|
|
20
|
+
//!
|
|
21
|
+
//! Concurrency: inserts happen on worker threads (any ractor, its own
|
|
22
|
+
//! GVL held), release is a single atomic store from whatever tokio
|
|
23
|
+
//! thread drops the buffer, and the mark hook only loads atomics, so
|
|
24
|
+
//! no path takes a lock. A full slab degrades to the copy path.
|
|
25
|
+
|
|
26
|
+
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
|
27
|
+
use std::sync::Arc;
|
|
28
|
+
|
|
29
|
+
use bytes::Bytes;
|
|
30
|
+
use magnus::rb_sys::AsRawValue;
|
|
31
|
+
use magnus::RString;
|
|
32
|
+
|
|
33
|
+
/// Floor for the zero-copy path. Below it the pin machinery costs more
|
|
34
|
+
/// than the memcpy it saves. It is also a SOUNDNESS bound: it must
|
|
35
|
+
/// exceed the largest embeddable string (a 640-byte GC slot in Ruby
|
|
36
|
+
/// 4.0), so the acquired string's bytes are guaranteed to live in a
|
|
37
|
+
/// stable malloc'd buffer rather than inside a movable RVALUE, and the
|
|
38
|
+
/// string can never be re-embedded by compaction.
|
|
39
|
+
pub const ZERO_COPY_MIN: usize = 4096;
|
|
40
|
+
|
|
41
|
+
/// In-flight pins per server. Bounds slab memory (8 bytes per slot) and
|
|
42
|
+
/// GC mark work; overflow falls back to copying, so the cap is a
|
|
43
|
+
/// throughput heuristic, not a limit on concurrency.
|
|
44
|
+
const SLAB_CAPACITY: usize = 4096;
|
|
45
|
+
|
|
46
|
+
extern "C" {
|
|
47
|
+
// Exported by libruby but absent from the public headers: io.c's
|
|
48
|
+
// buffer-stabilizing primitive (see module docs). Signature per
|
|
49
|
+
// string.c: VALUE rb_str_tmp_frozen_acquire(VALUE orig).
|
|
50
|
+
fn rb_str_tmp_frozen_acquire(orig: rb_sys::VALUE) -> rb_sys::VALUE;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// GC roots for in-flight response buffers. Slot value 0 = empty (a
|
|
54
|
+
/// VALUE of 0 is Qfalse, which can never be a pinned string).
|
|
55
|
+
pub struct PinSlab {
|
|
56
|
+
slots: Box<[AtomicU64]>,
|
|
57
|
+
/// Rotating claim cursor: keeps the free-slot scan O(1) amortized.
|
|
58
|
+
cursor: AtomicUsize,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
impl PinSlab {
|
|
62
|
+
pub fn new() -> Self {
|
|
63
|
+
PinSlab {
|
|
64
|
+
slots: (0..SLAB_CAPACITY).map(|_| AtomicU64::new(0)).collect(),
|
|
65
|
+
cursor: AtomicUsize::new(0),
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// Root `value`; None when the slab is full (caller copies instead).
|
|
70
|
+
fn insert(&self, value: rb_sys::VALUE) -> Option<usize> {
|
|
71
|
+
let start = self.cursor.fetch_add(1, Ordering::Relaxed);
|
|
72
|
+
for offset in 0..self.slots.len() {
|
|
73
|
+
let index = (start + offset) % self.slots.len();
|
|
74
|
+
if self.slots[index]
|
|
75
|
+
.compare_exchange(0, value, Ordering::SeqCst, Ordering::Relaxed)
|
|
76
|
+
.is_ok()
|
|
77
|
+
{
|
|
78
|
+
return Some(index);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
None
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// Drop the root. Called from tokio threads: a plain atomic store,
|
|
85
|
+
/// no Ruby API. The string stays alive until the next GC sweep.
|
|
86
|
+
fn release(&self, index: usize) {
|
|
87
|
+
self.slots[index].store(0, Ordering::SeqCst);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// GC mark hook (PinKeeper). Runs during stop-the-world, so it must
|
|
91
|
+
/// not allocate, lock, or panic: it only loads atomics. A slot
|
|
92
|
+
/// concurrently released by a tokio thread is seen as either the
|
|
93
|
+
/// live value (marked one cycle longer, harmless) or 0 (skipped).
|
|
94
|
+
pub fn mark_all(&self) {
|
|
95
|
+
for slot in self.slots.iter() {
|
|
96
|
+
let value = slot.load(Ordering::SeqCst);
|
|
97
|
+
if value != 0 {
|
|
98
|
+
// SAFETY: non-zero slots hold VALUEs inserted by
|
|
99
|
+
// pinned_bytes and not yet released, i.e. live objects;
|
|
100
|
+
// rb_gc_mark is the pinning mark, callable from a mark
|
|
101
|
+
// hook by design.
|
|
102
|
+
unsafe { rb_sys::rb_gc_mark(value) };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
#[cfg(test)]
|
|
108
|
+
fn occupied(&self) -> usize {
|
|
109
|
+
self.slots
|
|
110
|
+
.iter()
|
|
111
|
+
.filter(|s| s.load(Ordering::SeqCst) != 0)
|
|
112
|
+
.count()
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/// The GC-visible face of a server's PinSlab: `Kino::Server` holds one
|
|
117
|
+
/// for its lifetime (surviving worker-ractor crashes), and Ruby's GC
|
|
118
|
+
/// marks every pinned string through it.
|
|
119
|
+
#[derive(magnus::TypedData)]
|
|
120
|
+
#[magnus(class = "Kino::Native::PinKeeper", free_immediately, mark)]
|
|
121
|
+
pub struct PinKeeper(pub Arc<PinSlab>);
|
|
122
|
+
|
|
123
|
+
impl magnus::DataTypeFunctions for PinKeeper {
|
|
124
|
+
fn mark(&self, _marker: &magnus::gc::Marker) {
|
|
125
|
+
self.0.mark_all();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/// Owns one in-flight buffer: the raw view plus the slab slot rooting
|
|
130
|
+
/// the acquired string.
|
|
131
|
+
struct PinnedBuf {
|
|
132
|
+
slab: Arc<PinSlab>,
|
|
133
|
+
index: usize,
|
|
134
|
+
ptr: *const u8,
|
|
135
|
+
len: usize,
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// SAFETY: the only operations performed off the Ruby thread are reading
|
|
139
|
+
// the byte buffer (the acquired string is frozen and slab-rooted: no
|
|
140
|
+
// writer exists and the buffer neither moves nor frees while the slot
|
|
141
|
+
// holds it) and Drop's atomic store. No Ruby API is called off-thread.
|
|
142
|
+
unsafe impl Send for PinnedBuf {}
|
|
143
|
+
|
|
144
|
+
impl AsRef<[u8]> for PinnedBuf {
|
|
145
|
+
fn as_ref(&self) -> &[u8] {
|
|
146
|
+
// SAFETY: ptr/len were captured under the GVL from the acquired
|
|
147
|
+
// frozen string, which stays alive and byte-stable until this
|
|
148
|
+
// owner releases its slot (see module docs).
|
|
149
|
+
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
impl Drop for PinnedBuf {
|
|
154
|
+
fn drop(&mut self) {
|
|
155
|
+
self.slab.release(self.index);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/// Bytes borrowing `body`'s buffer, with the string rooted until hyper
|
|
160
|
+
/// drops it; None when the body is below ZERO_COPY_MIN or the slab is
|
|
161
|
+
/// full (caller copies). Requires the calling worker's GVL; safe from
|
|
162
|
+
/// any ractor.
|
|
163
|
+
pub fn pinned_bytes(slab: &Arc<PinSlab>, body: RString) -> Option<Bytes> {
|
|
164
|
+
if body.len() < ZERO_COPY_MIN {
|
|
165
|
+
return None;
|
|
166
|
+
}
|
|
167
|
+
// SAFETY: this worker's GVL is held; body is a live RString rooted
|
|
168
|
+
// by the caller. The acquired tmp is rooted by this thread's machine
|
|
169
|
+
// stack (conservatively scanned) until the slab insert publishes it.
|
|
170
|
+
let tmp = unsafe { rb_str_tmp_frozen_acquire(body.as_raw()) };
|
|
171
|
+
let index = slab.insert(tmp)?;
|
|
172
|
+
// SAFETY: tmp is frozen, alive, and slab-rooted; len >= ZERO_COPY_MIN
|
|
173
|
+
// rules out an embedded buffer, so ptr is a stable heap allocation.
|
|
174
|
+
let (ptr, len) = unsafe {
|
|
175
|
+
(
|
|
176
|
+
rb_sys::macros::RSTRING_PTR(tmp) as *const u8,
|
|
177
|
+
rb_sys::macros::RSTRING_LEN(tmp) as usize,
|
|
178
|
+
)
|
|
179
|
+
};
|
|
180
|
+
Some(Bytes::from_owner(PinnedBuf {
|
|
181
|
+
slab: slab.clone(),
|
|
182
|
+
index,
|
|
183
|
+
ptr,
|
|
184
|
+
len,
|
|
185
|
+
}))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#[cfg(test)]
|
|
189
|
+
mod tests {
|
|
190
|
+
use super::*;
|
|
191
|
+
|
|
192
|
+
#[test]
|
|
193
|
+
fn insert_release_reuse_cycle() {
|
|
194
|
+
let slab = PinSlab::new();
|
|
195
|
+
let a = slab.insert(0x1000).expect("slot free");
|
|
196
|
+
let b = slab.insert(0x2000).expect("slot free");
|
|
197
|
+
assert_ne!(a, b);
|
|
198
|
+
assert_eq!(slab.occupied(), 2);
|
|
199
|
+
|
|
200
|
+
slab.release(a);
|
|
201
|
+
assert_eq!(slab.occupied(), 1);
|
|
202
|
+
|
|
203
|
+
// The freed slot is claimable again.
|
|
204
|
+
let c = slab.insert(0x3000).expect("slot free");
|
|
205
|
+
assert_eq!(slab.occupied(), 2);
|
|
206
|
+
slab.release(b);
|
|
207
|
+
slab.release(c);
|
|
208
|
+
assert_eq!(slab.occupied(), 0);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
#[test]
|
|
212
|
+
fn full_slab_refuses_instead_of_evicting() {
|
|
213
|
+
let slab = PinSlab::new();
|
|
214
|
+
let indexes: Vec<usize> = (0..SLAB_CAPACITY)
|
|
215
|
+
.map(|i| slab.insert(0x1000 + i as rb_sys::VALUE).expect("capacity"))
|
|
216
|
+
.collect();
|
|
217
|
+
assert_eq!(slab.occupied(), SLAB_CAPACITY);
|
|
218
|
+
assert!(slab.insert(0x9999).is_none());
|
|
219
|
+
|
|
220
|
+
slab.release(indexes[7]);
|
|
221
|
+
assert_eq!(slab.insert(0x9999), Some(indexes[7]));
|
|
222
|
+
}
|
|
223
|
+
}
|
data/ext/kino/src/queue.rs
CHANGED
|
@@ -30,7 +30,7 @@ type Taken = Option<BoxedCtx>;
|
|
|
30
30
|
/// cost is real (~20% of cycles at saturation, per perf), but a measured
|
|
31
31
|
/// 20µs spin made things WORSE on oversubscribed cores: spinners steal
|
|
32
32
|
/// exactly the CPU the tokio threads need.
|
|
33
|
-
fn block_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
|
|
33
|
+
fn block_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Result<Taken, Error> {
|
|
34
34
|
if slot.lane_rx.is_some() {
|
|
35
35
|
return lane_take(server, slot);
|
|
36
36
|
}
|
|
@@ -40,15 +40,16 @@ fn block_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
|
|
|
40
40
|
// try_recv never blocks, so the whole GVL release/reacquire (two
|
|
41
41
|
// scheduler round-trips per request) is skipped entirely.
|
|
42
42
|
match req_rx.try_recv() {
|
|
43
|
-
Ok(ctx) => Some(ctx),
|
|
44
|
-
Err(flume::TryRecvError::Disconnected) => None,
|
|
43
|
+
Ok(ctx) => Ok(Some(ctx)),
|
|
44
|
+
Err(flume::TryRecvError::Disconnected) => Ok(None),
|
|
45
45
|
Err(flume::TryRecvError::Empty) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
let taken =
|
|
47
|
+
gvl::interruptible(&slot.interrupted, || match req_rx.recv_timeout(TICK) {
|
|
48
|
+
Ok(ctx) => Some(Some(ctx)),
|
|
49
|
+
Err(flume::RecvTimeoutError::Timeout) => None,
|
|
50
|
+
Err(flume::RecvTimeoutError::Disconnected) => Some(None),
|
|
51
|
+
})?;
|
|
52
|
+
Ok(taken.flatten())
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
55
|
}
|
|
@@ -56,7 +57,7 @@ fn block_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
|
|
|
56
57
|
/// Lane-mode take: own lane first (no wake needed while the dispatcher
|
|
57
58
|
/// keeps feeding an awake lane), then steal from siblings, then park on
|
|
58
59
|
/// the own lane with the parked flag raised so the dispatcher avoids it.
|
|
59
|
-
fn lane_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
|
|
60
|
+
fn lane_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Result<Taken, Error> {
|
|
60
61
|
let lane_rx = slot.lane_rx.as_ref().expect("lane_take without lane");
|
|
61
62
|
|
|
62
63
|
let steal = || -> Option<BoxedCtx> {
|
|
@@ -76,12 +77,12 @@ fn lane_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
|
|
|
76
77
|
|
|
77
78
|
// Hot path, GVL still held: own lane, then a steal sweep.
|
|
78
79
|
match lane_rx.try_recv() {
|
|
79
|
-
Ok(ctx) => return Some(ctx),
|
|
80
|
-
Err(flume::TryRecvError::Disconnected) => return None,
|
|
80
|
+
Ok(ctx) => return Ok(Some(ctx)),
|
|
81
|
+
Err(flume::TryRecvError::Disconnected) => return Ok(None),
|
|
81
82
|
Err(flume::TryRecvError::Empty) => {}
|
|
82
83
|
}
|
|
83
84
|
if let Some(ctx) = steal() {
|
|
84
|
-
return Some(ctx);
|
|
85
|
+
return Ok(Some(ctx));
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
// Park. The flag-then-recheck order closes the race with a dispatcher
|
|
@@ -96,10 +97,11 @@ fn lane_take(server: &ServerInner, slot: &Arc<WorkerSlot>) -> Taken {
|
|
|
96
97
|
Err(flume::RecvTimeoutError::Timeout) => steal().map(Some),
|
|
97
98
|
Err(flume::RecvTimeoutError::Disconnected) => Some(None),
|
|
98
99
|
}
|
|
99
|
-
})
|
|
100
|
-
|
|
100
|
+
});
|
|
101
|
+
// Unpark before propagating any panic, or the dispatcher shuns this
|
|
102
|
+
// lane for good.
|
|
101
103
|
slot.parked.store(false, Ordering::SeqCst);
|
|
102
|
-
taken
|
|
104
|
+
Ok(taken?.flatten())
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
/// Wrap a ctx into its env Hash, with the Ruby request handle embedded
|
|
@@ -137,7 +139,7 @@ fn checkout(ruby: &Ruby, server_id: u64, worker_id: usize) -> Result<Option<Chec
|
|
|
137
139
|
slot.current.lock().clear();
|
|
138
140
|
slot.interrupted.store(false, Ordering::SeqCst);
|
|
139
141
|
|
|
140
|
-
Ok(block_take(&server, &slot)
|
|
142
|
+
Ok(block_take(&server, &slot)?.map(|ctx| (server, slot, ctx)))
|
|
141
143
|
}
|
|
142
144
|
|
|
143
145
|
/// Take one request; returns its env Hash (request handle inside under
|
data/ext/kino/src/registry.rs
CHANGED
|
@@ -52,6 +52,9 @@ pub struct ServerInner {
|
|
|
52
52
|
pub lanes: bool,
|
|
53
53
|
/// Round-robin cursor for lane dispatch.
|
|
54
54
|
pub lane_cursor: AtomicUsize,
|
|
55
|
+
/// GC roots for zero-copy response buffers (pin.rs). The Ruby Server
|
|
56
|
+
/// object holds the marking PinKeeper for this slab.
|
|
57
|
+
pub pin_slab: Arc<crate::pin::PinSlab>,
|
|
55
58
|
}
|
|
56
59
|
|
|
57
60
|
/// One per worker *thread* (slot count = workers × threads). The interrupt
|
|
@@ -189,6 +192,7 @@ pub fn test_server(lanes: bool, queue_depth: usize) -> Arc<ServerInner> {
|
|
|
189
192
|
access_log: None,
|
|
190
193
|
lanes,
|
|
191
194
|
lane_cursor: AtomicUsize::new(0),
|
|
195
|
+
pin_slab: Arc::new(crate::pin::PinSlab::new()),
|
|
192
196
|
})
|
|
193
197
|
}
|
|
194
198
|
|
data/ext/kino/src/request.rs
CHANGED
|
@@ -37,6 +37,9 @@ pub struct RequestCtx {
|
|
|
37
37
|
/// The owning worker slot (set at admit time, queue.rs); its interrupt
|
|
38
38
|
/// flag makes blocked body reads/writes interruptible like the queue pop.
|
|
39
39
|
pub slot: Option<Arc<crate::registry::WorkerSlot>>,
|
|
40
|
+
/// The server's zero-copy roots: large response bodies are pinned
|
|
41
|
+
/// here and ride to hyper without a copy (pin.rs).
|
|
42
|
+
pub pin_slab: Arc<crate::pin::PinSlab>,
|
|
40
43
|
pub responder: Arc<Responder>,
|
|
41
44
|
}
|
|
42
45
|
|
|
@@ -52,12 +55,12 @@ impl Drop for RequestCtx {
|
|
|
52
55
|
|
|
53
56
|
/// Block on a channel operation with the GVL released, waking on the
|
|
54
57
|
/// slot's interrupt flag. `attempt` performs one bounded tick and returns
|
|
55
|
-
/// Some when done; None on timeout.
|
|
58
|
+
/// Some when done; None on timeout. Ok(None) = interrupted. Every Ruby
|
|
56
59
|
/// handle is created by `admit` (queue.rs), which always sets the slot.
|
|
57
60
|
fn block_on<T>(
|
|
58
61
|
slot: &Option<Arc<crate::registry::WorkerSlot>>,
|
|
59
62
|
attempt: impl FnMut() -> Option<T>,
|
|
60
|
-
) -> Option<T> {
|
|
63
|
+
) -> Result<Option<T>, Error> {
|
|
61
64
|
let slot = slot.as_ref().expect("slot set by admit");
|
|
62
65
|
gvl::interruptible(&slot.interrupted, attempt)
|
|
63
66
|
}
|
|
@@ -228,13 +231,23 @@ pub fn respond_simple(
|
|
|
228
231
|
) -> Result<bool, Error> {
|
|
229
232
|
let ctx = request.0.borrow();
|
|
230
233
|
let builder = build_head(status, headers)?;
|
|
231
|
-
let bytes =
|
|
234
|
+
let bytes = body_bytes(&ctx, body);
|
|
232
235
|
let response = builder
|
|
233
236
|
.body(full_body(bytes))
|
|
234
237
|
.map_err(|e| invalid_response(ruby, e))?;
|
|
235
238
|
Ok(ctx.responder.send_response(response))
|
|
236
239
|
}
|
|
237
240
|
|
|
241
|
+
/// Body bytes for hyper: large bodies are pinned in place (zero-copy),
|
|
242
|
+
/// everything else is copied out of the Ruby string, which may be
|
|
243
|
+
/// mutated or collected the moment we return.
|
|
244
|
+
fn body_bytes(ctx: &RequestCtx, body: RString) -> Bytes {
|
|
245
|
+
if let Some(bytes) = crate::pin::pinned_bytes(&ctx.pin_slab, body) {
|
|
246
|
+
return bytes;
|
|
247
|
+
}
|
|
248
|
+
Bytes::copy_from_slice(unsafe { body.as_slice() })
|
|
249
|
+
}
|
|
250
|
+
|
|
238
251
|
impl Request {
|
|
239
252
|
/// Next chunk of the request body, at most `max_len` bytes; nil at EOF.
|
|
240
253
|
/// Blocks (GVL released) until the client sends more.
|
|
@@ -256,7 +269,7 @@ impl Request {
|
|
|
256
269
|
Err(flume::RecvTimeoutError::Timeout) => None,
|
|
257
270
|
Err(flume::RecvTimeoutError::Disconnected) => Some(None),
|
|
258
271
|
}
|
|
259
|
-
})
|
|
272
|
+
})?;
|
|
260
273
|
match outcome {
|
|
261
274
|
Some(Some(bytes)) => bytes,
|
|
262
275
|
Some(None) => {
|
|
@@ -306,7 +319,7 @@ impl Request {
|
|
|
306
319
|
"Kino: response stream not started or already finished",
|
|
307
320
|
));
|
|
308
321
|
};
|
|
309
|
-
let bytes =
|
|
322
|
+
let bytes = body_bytes(&ctx, chunk);
|
|
310
323
|
let mut pending = Some(Ok(BodyFrame::data(bytes)));
|
|
311
324
|
|
|
312
325
|
let outcome = block_on(&ctx.slot, || {
|
|
@@ -319,7 +332,7 @@ impl Request {
|
|
|
319
332
|
}
|
|
320
333
|
Err(flume::SendTimeoutError::Disconnected(_)) => Some(false),
|
|
321
334
|
}
|
|
322
|
-
})
|
|
335
|
+
})?;
|
|
323
336
|
match outcome {
|
|
324
337
|
// Receiver dropped = client went away; the app keeps writing
|
|
325
338
|
// into the void harmlessly (Rack has no error contract here).
|
|
@@ -410,6 +423,7 @@ pub fn test_ctx() -> crate::registry::BoxedCtx {
|
|
|
410
423
|
body_timeout: Arc::new(std::sync::atomic::AtomicBool::new(false)),
|
|
411
424
|
leftover: None,
|
|
412
425
|
slot: None,
|
|
426
|
+
pin_slab: Arc::new(crate::pin::PinSlab::new()),
|
|
413
427
|
responder: Arc::new(Responder::new(head_tx)),
|
|
414
428
|
})
|
|
415
429
|
}
|
data/ext/kino/src/server.rs
CHANGED
|
@@ -112,6 +112,7 @@ pub fn server_start(ruby: &Ruby, config: magnus::RHash) -> Result<(u64, u16), Er
|
|
|
112
112
|
access_log: log_requests.then(|| crate::logsink::Sink::new(std::io::stdout())),
|
|
113
113
|
lanes,
|
|
114
114
|
lane_cursor: std::sync::atomic::AtomicUsize::new(0),
|
|
115
|
+
pin_slab: Arc::new(crate::pin::PinSlab::new()),
|
|
115
116
|
});
|
|
116
117
|
|
|
117
118
|
let tokio_listener = {
|
|
@@ -350,6 +351,7 @@ async fn handle_request(
|
|
|
350
351
|
body_timeout,
|
|
351
352
|
leftover: None,
|
|
352
353
|
slot: None,
|
|
354
|
+
pin_slab: server.pin_slab.clone(),
|
|
353
355
|
responder,
|
|
354
356
|
});
|
|
355
357
|
|
|
@@ -589,6 +591,16 @@ pub fn shutdown_runtime(_ruby: &Ruby, server_id: u64, timeout_ms: u64) -> Result
|
|
|
589
591
|
Ok(())
|
|
590
592
|
}
|
|
591
593
|
|
|
594
|
+
/// The GC anchor for this server's zero-copy pins: the Ruby Server holds
|
|
595
|
+
/// it for its lifetime, so pinned buffers survive worker-ractor crashes.
|
|
596
|
+
pub fn pin_keeper(
|
|
597
|
+
ruby: &Ruby,
|
|
598
|
+
server_id: u64,
|
|
599
|
+
) -> Result<magnus::typed_data::Obj<crate::pin::PinKeeper>, Error> {
|
|
600
|
+
let server = registry::get(ruby, server_id)?;
|
|
601
|
+
Ok(ruby.obj_wrap(crate::pin::PinKeeper(server.pin_slab.clone())))
|
|
602
|
+
}
|
|
603
|
+
|
|
592
604
|
/// Errors print in red on color terminals. Covers worker errors,
|
|
593
605
|
/// supervisor crash reports, and everything apps write to rack.errors.
|
|
594
606
|
pub fn log_error(message: String) {
|
|
@@ -71,10 +71,22 @@ pub fn take(ruby: &magnus::Ruby, id: u64) -> Result<Option<String>, magnus::Erro
|
|
|
71
71
|
Err(flume::RecvTimeoutError::Timeout) => None,
|
|
72
72
|
Err(flume::RecvTimeoutError::Disconnected) => Some(None),
|
|
73
73
|
}
|
|
74
|
-
})
|
|
74
|
+
})?;
|
|
75
75
|
Ok(taken.flatten())
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
/// Panic inside a GVL-released block: proves it surfaces as a Ruby
|
|
79
|
+
/// exception instead of unwinding into the VM and killing the process.
|
|
80
|
+
/// The default panic hook is silenced around the intentional panic so
|
|
81
|
+
/// spec output stays clean.
|
|
82
|
+
pub fn panic_in_release() -> Result<(), magnus::Error> {
|
|
83
|
+
let hook = std::panic::take_hook();
|
|
84
|
+
std::panic::set_hook(Box::new(|_| {}));
|
|
85
|
+
let result = gvl::without_gvl(|| panic!("intentional test panic"), None);
|
|
86
|
+
std::panic::set_hook(hook);
|
|
87
|
+
result
|
|
88
|
+
}
|
|
89
|
+
|
|
78
90
|
pub fn close(ruby: &magnus::Ruby, id: u64) -> Result<(), magnus::Error> {
|
|
79
91
|
let chan = lookup(ruby, id)?;
|
|
80
92
|
chan.tx.lock().take();
|
data/ext/kino/src/timer.rs
CHANGED
data/lib/kino/server.rb
CHANGED
|
@@ -73,17 +73,28 @@ module Kino
|
|
|
73
73
|
def start
|
|
74
74
|
raise Error, "server already started" if @started
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
76
|
+
# Claim the pidfile before binding: refusing to start (another
|
|
77
|
+
# instance is alive) must not leave a booted native runtime behind.
|
|
78
|
+
write_pidfile if @pidfile
|
|
79
|
+
booted = false
|
|
80
|
+
begin
|
|
81
|
+
@id, @port = Native.server_start(
|
|
82
|
+
bind: @bind, port: @requested_port,
|
|
83
|
+
queue_depth: @queue_depth, queue_timeout_ms: @queue_timeout_ms,
|
|
84
|
+
request_timeout_ms: @request_timeout_ms,
|
|
85
|
+
max_connections: @max_connections,
|
|
86
|
+
max_body_size: @max_body_size,
|
|
87
|
+
tokio_threads: @tokio_threads,
|
|
88
|
+
tls_cert: @tls&.fetch(:cert), tls_key: @tls&.fetch(:key),
|
|
89
|
+
lanes: @lanes, log_requests: @log_requests
|
|
90
|
+
)
|
|
91
|
+
booted = true
|
|
92
|
+
ensure
|
|
93
|
+
remove_pidfile if @pidfile && !booted
|
|
94
|
+
end
|
|
95
|
+
# GC anchor for zero-copy response buffers: held for the server's
|
|
96
|
+
# lifetime so in-flight buffers survive even a worker ractor crash.
|
|
97
|
+
@pin_keeper = Native.pin_keeper(@id)
|
|
87
98
|
if @mode == :ractor
|
|
88
99
|
@supervisor = RactorSupervisor.new(@id, @app, workers: @workers, threads: @threads,
|
|
89
100
|
batch: @batch, on_error: @on_error).start
|
|
@@ -133,9 +144,12 @@ module Kino
|
|
|
133
144
|
end
|
|
134
145
|
|
|
135
146
|
Native.shutdown_runtime(@id, 1_000)
|
|
147
|
+
# The runtime is gone, so hyper has dropped every pinned buffer;
|
|
148
|
+
# the keeper (and the strings it marked) may now be collected.
|
|
149
|
+
@pin_keeper = nil
|
|
136
150
|
@worker_threads.clear
|
|
137
151
|
@started = false
|
|
138
|
-
|
|
152
|
+
remove_pidfile if @pidfile
|
|
139
153
|
nil
|
|
140
154
|
end
|
|
141
155
|
|
|
@@ -241,6 +255,67 @@ module Kino
|
|
|
241
255
|
[soft * 8 / 10, 64].max
|
|
242
256
|
end
|
|
243
257
|
|
|
258
|
+
# Claim the pidfile for this process. O_EXCL creation fails on ANY
|
|
259
|
+
# existing directory entry (regular file, symlink, even a dangling
|
|
260
|
+
# one), so a live instance's pidfile is never overwritten and a
|
|
261
|
+
# symlink is never followed. A leftover entry whose owner is gone is
|
|
262
|
+
# replaced; one that does not hold a pid is refused, not clobbered.
|
|
263
|
+
def write_pidfile
|
|
264
|
+
claim_pidfile
|
|
265
|
+
rescue Errno::EEXIST
|
|
266
|
+
refuse_unless_stale
|
|
267
|
+
begin
|
|
268
|
+
# Unlink removes the entry itself; a symlink's target is untouched.
|
|
269
|
+
File.unlink(@pidfile)
|
|
270
|
+
rescue Errno::ENOENT
|
|
271
|
+
# Vanished on its own; the claim below settles any remaining race.
|
|
272
|
+
end
|
|
273
|
+
begin
|
|
274
|
+
claim_pidfile
|
|
275
|
+
rescue Errno::EEXIST
|
|
276
|
+
raise Error, "lost the race for #{@pidfile}: another instance is starting"
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def claim_pidfile
|
|
281
|
+
File.open(@pidfile, File::WRONLY | File::CREAT | File::EXCL, 0o644) do |file|
|
|
282
|
+
file.write("#{Process.pid}\n")
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# @raise [Kino::Error] when the pidfile's owner is still alive, or the
|
|
287
|
+
# file does not look like a pidfile at all
|
|
288
|
+
def refuse_unless_stale
|
|
289
|
+
content = begin
|
|
290
|
+
File.read(@pidfile)
|
|
291
|
+
rescue Errno::ENOENT
|
|
292
|
+
return # already gone; nothing to refuse
|
|
293
|
+
end
|
|
294
|
+
pid = Integer(content.strip, exception: false)
|
|
295
|
+
unless pid&.positive?
|
|
296
|
+
raise Error, "refusing to overwrite #{@pidfile}: does not hold a pid"
|
|
297
|
+
end
|
|
298
|
+
raise Error, "already running (pid #{pid}, per #{@pidfile})" if process_alive?(pid)
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def process_alive?(pid)
|
|
302
|
+
Process.kill(0, pid)
|
|
303
|
+
true
|
|
304
|
+
rescue Errno::ESRCH
|
|
305
|
+
false
|
|
306
|
+
rescue Errno::EPERM
|
|
307
|
+
true # exists, just not ours to signal
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# Delete only a pidfile that is still ours: by shutdown time the path
|
|
311
|
+
# may belong to a replacement instance, or an operator may have
|
|
312
|
+
# repointed it at something that is not a pidfile at all.
|
|
313
|
+
def remove_pidfile
|
|
314
|
+
File.unlink(@pidfile) if File.read(@pidfile) == "#{Process.pid}\n"
|
|
315
|
+
rescue Errno::ENOENT
|
|
316
|
+
nil
|
|
317
|
+
end
|
|
318
|
+
|
|
244
319
|
def join_workers(deadline)
|
|
245
320
|
if @supervisor
|
|
246
321
|
@supervisor.shutdown([deadline - monotonic_now, 0].max)
|
data/lib/kino/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: kino
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yaroslav Markin
|
|
@@ -165,6 +165,7 @@ files:
|
|
|
165
165
|
- ext/kino/src/gvl.rs
|
|
166
166
|
- ext/kino/src/lib.rs
|
|
167
167
|
- ext/kino/src/logsink.rs
|
|
168
|
+
- ext/kino/src/pin.rs
|
|
168
169
|
- ext/kino/src/queue.rs
|
|
169
170
|
- ext/kino/src/registry.rs
|
|
170
171
|
- ext/kino/src/request.rs
|