kino 0.7.2 → 0.8.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/.yardopts +5 -0
- data/CHANGELOG.md +281 -0
- data/Cargo.lock +164 -95
- data/README.md +269 -438
- data/doc/README.md +27 -2
- data/doc/architecture.md +862 -43
- data/doc/benchmarks.md +444 -214
- data/doc/configuration.md +215 -0
- data/doc/operations.md +258 -0
- data/doc/rails-on-ractors.md +161 -49
- data/doc/streaming.md +52 -0
- data/doc/websockets.md +469 -0
- data/doc/why-kino.md +6 -6
- data/doc/worker-loop.md +462 -0
- data/ext/kino/Cargo.toml +17 -1
- data/ext/kino/build.rs +32 -1
- data/ext/kino/clippy.toml +10 -0
- data/ext/kino/src/access_log.rs +70 -92
- data/ext/kino/src/control.rs +26 -685
- data/ext/kino/src/counters.rs +132 -0
- data/ext/kino/src/cpus.rs +1 -18
- data/ext/kino/src/dispatch/tests.rs +731 -0
- data/ext/kino/src/dispatch.rs +276 -0
- data/ext/kino/src/doorbell.rs +108 -0
- data/ext/kino/src/env_strings.rs +26 -30
- data/ext/kino/src/framing.rs +110 -0
- data/ext/kino/src/gvl.rs +292 -66
- data/ext/kino/src/hijack/tests.rs +416 -0
- data/ext/kino/src/hijack.rs +471 -0
- data/ext/kino/src/inline/answer.rs +488 -0
- data/ext/kino/src/inline/conn.rs +1124 -0
- data/ext/kino/src/inline/deadline.rs +421 -0
- data/ext/kino/src/inline/encode.rs +119 -0
- data/ext/kino/src/inline/io.rs +487 -0
- data/ext/kino/src/inline/mod.rs +766 -0
- data/ext/kino/src/inline/parity.rs +862 -0
- data/ext/kino/src/inline/parse.rs +131 -0
- data/ext/kino/src/inline/spin.rs +428 -0
- data/ext/kino/src/inline/testing.rs +628 -0
- data/ext/kino/src/io_shards.rs +240 -192
- data/ext/kino/src/lane_room.rs +99 -0
- data/ext/kino/src/lib.rs +139 -77
- data/ext/kino/src/listen.rs +180 -12
- data/ext/kino/src/log.rs +12 -47
- data/ext/kino/src/logsink.rs +3 -9
- data/ext/kino/src/mono.rs +12 -17
- data/ext/kino/src/pin.rs +3 -3
- data/ext/kino/src/queue/mod.rs +54 -0
- data/ext/kino/src/queue/take.rs +503 -0
- data/ext/kino/src/queue/testing.rs +107 -0
- data/ext/kino/src/queue/tests.rs +581 -0
- data/ext/kino/src/queue/worker.rs +501 -0
- data/ext/kino/src/registry.rs +168 -379
- data/ext/kino/src/request.rs +834 -235
- data/ext/kino/src/response.rs +532 -94
- data/ext/kino/src/server/accept.rs +322 -0
- data/ext/kino/src/server/config.rs +138 -0
- data/ext/kino/src/server/handle.rs +247 -0
- data/ext/kino/src/server/intake.rs +217 -0
- data/ext/kino/src/server/mod.rs +157 -0
- data/ext/kino/src/server/natives.rs +223 -0
- data/ext/kino/src/server/serve/intake_tests.rs +458 -0
- data/ext/kino/src/server/serve.rs +436 -0
- data/ext/kino/src/server/tests.rs +532 -0
- data/ext/kino/src/shared_gvl.rs +50 -0
- data/ext/kino/src/slot/lane.rs +138 -0
- data/ext/kino/src/slot/life.rs +111 -0
- data/ext/kino/src/slot/load.rs +193 -0
- data/ext/kino/src/slot/mod.rs +494 -0
- data/ext/kino/src/slot/park.rs +82 -0
- data/ext/kino/src/stats.rs +936 -0
- data/ext/kino/src/symbols.rs +66 -0
- data/ext/kino/src/test_support.rs +24 -14
- data/ext/kino/src/testing.rs +409 -0
- data/ext/kino/src/ticker.rs +87 -0
- data/ext/kino/src/timer.rs +4 -7
- data/ext/kino/src/tls.rs +101 -131
- data/ext/kino/src/typed_data.rs +131 -0
- data/ext/kino/src/upgrade.rs +77 -0
- data/ext/kino/src/ws/codec.rs +470 -0
- data/ext/kino/src/ws/drive.rs +600 -0
- data/ext/kino/src/ws/handshake.rs +267 -0
- data/ext/kino/src/ws/mod.rs +317 -0
- data/ext/kino/src/ws/server_tests.rs +759 -0
- data/ext/kino/src/ws/socket.rs +279 -0
- data/ext/kino/src/ws/stats.rs +88 -0
- data/ext/kino/src/ws/testing.rs +156 -0
- data/ext/kino/src/ws/topics.rs +550 -0
- data/lib/kino/cli.rb +100 -9
- data/lib/kino/configuration.rb +32 -10
- data/lib/kino/crash_backoff.rb +56 -0
- data/lib/kino/input.rb +36 -17
- data/lib/kino/log.rb +8 -1
- data/lib/kino/monitor.rb +1 -1
- data/lib/kino/pool.rb +169 -0
- data/lib/kino/pool_scaler.rb +12 -13
- data/lib/kino/quarantine_monitor.rb +3 -3
- data/lib/kino/ractor_supervisor.rb +201 -161
- data/lib/kino/server.rb +291 -185
- data/lib/kino/slot_bank.rb +21 -9
- data/lib/kino/stream.rb +2 -2
- data/lib/kino/teardown.rb +33 -0
- data/lib/kino/templates/kino.rb.tt +41 -6
- data/lib/kino/threaded_pool.rb +87 -102
- data/lib/kino/version.rb +1 -1
- data/lib/kino/web_socket.rb +125 -0
- data/lib/kino/worker/loop.rb +319 -0
- data/lib/kino/worker.rb +345 -79
- data/lib/kino/worker_hooks.rb +7 -1
- data/lib/kino/worker_row.rb +15 -0
- data/lib/kino.rb +27 -3
- data/lib/rackup/handler/kino.rb +1 -1
- data/sig/kino.rbs +93 -10
- metadata +66 -3
- data/ext/kino/src/queue.rs +0 -333
- data/ext/kino/src/server.rs +0 -1785
data/ext/kino/src/lib.rs
CHANGED
|
@@ -6,10 +6,17 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
|
|
6
6
|
|
|
7
7
|
mod access_log;
|
|
8
8
|
mod control;
|
|
9
|
+
mod counters;
|
|
9
10
|
mod cpus;
|
|
11
|
+
mod dispatch;
|
|
12
|
+
mod doorbell;
|
|
10
13
|
mod env_strings;
|
|
14
|
+
mod framing;
|
|
11
15
|
mod gvl;
|
|
16
|
+
mod hijack;
|
|
17
|
+
mod inline;
|
|
12
18
|
mod io_shards;
|
|
19
|
+
mod lane_room;
|
|
13
20
|
mod listen;
|
|
14
21
|
mod log;
|
|
15
22
|
mod logsink;
|
|
@@ -20,15 +27,39 @@ mod registry;
|
|
|
20
27
|
mod request;
|
|
21
28
|
mod response;
|
|
22
29
|
mod server;
|
|
30
|
+
mod shared_gvl;
|
|
31
|
+
mod slot;
|
|
32
|
+
mod stats;
|
|
23
33
|
mod style;
|
|
34
|
+
mod symbols;
|
|
24
35
|
mod test_support;
|
|
36
|
+
#[cfg(test)]
|
|
37
|
+
mod testing;
|
|
38
|
+
mod ticker;
|
|
25
39
|
mod timer;
|
|
26
40
|
mod tls;
|
|
41
|
+
mod typed_data;
|
|
42
|
+
mod upgrade;
|
|
43
|
+
mod ws;
|
|
27
44
|
|
|
28
45
|
use magnus::{function, method, prelude::*, Error, Ruby};
|
|
29
46
|
|
|
30
47
|
use crate::request::Request;
|
|
31
48
|
|
|
49
|
+
/// Each `"name" => function, arity;` row as a singleton function of `$on`.
|
|
50
|
+
macro_rules! functions {
|
|
51
|
+
($on:ident: $($name:literal => $function:path, $arity:tt;)*) => {
|
|
52
|
+
$($on.define_singleton_method($name, function!($function, $arity))?;)*
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// Each `"name" => method, arity;` row as an instance method of `$on`.
|
|
57
|
+
macro_rules! methods {
|
|
58
|
+
($on:ident: $($name:literal => $method:path, $arity:tt;)*) => {
|
|
59
|
+
$($on.define_method($name, method!($method, $arity))?;)*
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
32
63
|
#[magnus::init]
|
|
33
64
|
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
34
65
|
// Must come before any method definition: methods defined while this
|
|
@@ -39,94 +70,125 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
39
70
|
let module = ruby.define_module("Kino")?;
|
|
40
71
|
|
|
41
72
|
let native = module.define_module("Native")?;
|
|
42
|
-
native.define_singleton_method("server_start", function!(server::server_start, 1))?;
|
|
43
|
-
native.define_singleton_method("register_worker", function!(server::register_worker, 1))?;
|
|
44
|
-
native.define_singleton_method("worker", function!(queue::worker, 2))?;
|
|
45
|
-
native.define_singleton_method("stop_accepting", function!(server::stop_accepting, 1))?;
|
|
46
|
-
native.define_singleton_method("close_queue", function!(server::close_queue, 1))?;
|
|
47
|
-
native.define_singleton_method("queue_stats", function!(server::queue_stats, 1))?;
|
|
48
|
-
native.define_singleton_method("server_stats", function!(server::server_stats, 1))?;
|
|
49
|
-
native.define_singleton_method("worker_stats", function!(server::worker_stats, 1))?;
|
|
50
|
-
native.define_singleton_method("queue_time", function!(server::queue_time, 1))?;
|
|
51
|
-
native.define_singleton_method("quarantine_slot", function!(server::quarantine_slot, 2))?;
|
|
52
|
-
native.define_singleton_method("retire_slot", function!(server::retire_slot, 2))?;
|
|
53
|
-
native.define_singleton_method("reset_slot", function!(server::reset_slot, 2))?;
|
|
54
|
-
native.define_singleton_method(
|
|
55
|
-
"set_active_workers",
|
|
56
|
-
function!(server::set_active_workers, 2),
|
|
57
|
-
)?;
|
|
58
|
-
native.define_singleton_method("record_scale_up", function!(server::record_scale_up, 1))?;
|
|
59
|
-
native.define_singleton_method("record_scale_down", function!(server::record_scale_down, 1))?;
|
|
60
|
-
native.define_singleton_method("pool_stats", function!(server::pool_stats, 1))?;
|
|
61
|
-
native.define_singleton_method(
|
|
62
|
-
"record_quarantine_replacement",
|
|
63
|
-
function!(server::record_quarantine_replacement, 1),
|
|
64
|
-
)?;
|
|
65
|
-
native.define_singleton_method("control_ready", function!(server::control_ready, 1))?;
|
|
66
|
-
native.define_singleton_method("record_respawn", function!(server::record_respawn, 1))?;
|
|
67
|
-
native.define_singleton_method("control_stop", function!(control::control_stop, 1))?;
|
|
68
|
-
native.define_singleton_method("abort_inflight", function!(server::abort_inflight, 2))?;
|
|
69
|
-
native.define_singleton_method(
|
|
70
|
-
"abort_all_inflight",
|
|
71
|
-
function!(server::abort_all_inflight, 1),
|
|
72
|
-
)?;
|
|
73
|
-
native.define_singleton_method(
|
|
74
|
-
"interrupt_all_workers",
|
|
75
|
-
function!(server::interrupt_all_workers, 1),
|
|
76
|
-
)?;
|
|
77
|
-
native.define_singleton_method("shutdown_runtime", function!(server::shutdown_runtime, 2))?;
|
|
78
|
-
native.define_singleton_method("log_line", function!(log::log_line, 3))?;
|
|
79
|
-
native.define_singleton_method("sleep_chunk", function!(timer::sleep_chunk, 1))?;
|
|
80
|
-
native.define_singleton_method(
|
|
81
|
-
"available_parallelism",
|
|
82
|
-
function!(cpus::available_parallelism, 0),
|
|
83
|
-
)?;
|
|
84
|
-
native.define_singleton_method("log_device_open", function!(logsink::device_open, 1))?;
|
|
85
|
-
native.define_singleton_method("log_device_write", function!(logsink::device_write, 2))?;
|
|
86
|
-
native.define_singleton_method("log_device_close", function!(logsink::device_close, 1))?;
|
|
87
|
-
native.define_singleton_method(
|
|
88
|
-
"register_defaults",
|
|
89
|
-
function!(env_strings::register_defaults, 2),
|
|
90
|
-
)?;
|
|
91
|
-
|
|
92
73
|
native.define_class("PinKeeper", ruby.class_object())?;
|
|
93
|
-
|
|
74
|
+
// A worker's tick; the ticker rings a worker_loop's doorbell wait as often.
|
|
75
|
+
native.const_set("TICK_SECONDS", queue::TICK.as_secs_f64())?;
|
|
76
|
+
functions!(native:
|
|
77
|
+
"server_start" => server::server_start, 1;
|
|
78
|
+
"register_worker" => server::register_worker, 1;
|
|
79
|
+
"worker" => queue::worker, 2;
|
|
80
|
+
"wake_worker" => queue::wake_worker, 3;
|
|
81
|
+
"lingering_loops" => queue::lingering_loops, 2;
|
|
82
|
+
"stop_accepting" => server::stop_accepting, 1;
|
|
83
|
+
"close_queue" => server::close_queue, 1;
|
|
84
|
+
"close_bridges" => server::close_bridges, 1;
|
|
85
|
+
"queue_stats" => server::queue_stats, 1;
|
|
86
|
+
"stats" => server::stats, 1;
|
|
87
|
+
"worker_stats" => server::worker_stats, 1;
|
|
88
|
+
"quarantine_slot" => server::quarantine_slot, 3;
|
|
89
|
+
"retire_slot" => server::retire_slot, 3;
|
|
90
|
+
"reset_slot" => server::reset_slot, 2;
|
|
91
|
+
"ws_leave_all" => server::ws_leave_all, 2;
|
|
92
|
+
"set_active_workers" => server::set_active_workers, 2;
|
|
93
|
+
"record_scale_up" => server::record_scale_up, 1;
|
|
94
|
+
"record_scale_down" => server::record_scale_down, 1;
|
|
95
|
+
"record_quarantine_replacement" => server::record_quarantine_replacement, 1;
|
|
96
|
+
"control_ready" => server::control_ready, 1;
|
|
97
|
+
"record_respawn" => server::record_respawn, 1;
|
|
98
|
+
"control_stop" => control::control_stop, 1;
|
|
99
|
+
"abort_inflight" => server::abort_inflight, 2;
|
|
100
|
+
"abort_all_inflight" => server::abort_all_inflight, 1;
|
|
101
|
+
"interrupt_all_workers" => server::interrupt_all_workers, 1;
|
|
102
|
+
"shutdown_runtime" => server::shutdown_runtime, 2;
|
|
103
|
+
"log_line" => log::log_line, 3;
|
|
104
|
+
"sleep_chunk" => timer::sleep_chunk, 1;
|
|
105
|
+
"available_parallelism" => cpus::available_parallelism, 0;
|
|
106
|
+
"log_device_open" => logsink::device_open, 1;
|
|
107
|
+
"log_device_write" => logsink::device_write, 2;
|
|
108
|
+
"log_device_close" => logsink::device_close, 1;
|
|
109
|
+
"register_defaults" => env_strings::register_defaults, 2;
|
|
110
|
+
"pin_keeper" => server::pin_keeper, 1;
|
|
111
|
+
"ws_write" => ws::ws_write, 2;
|
|
112
|
+
"ws_write_binary" => ws::ws_write_binary, 2;
|
|
113
|
+
"ws_write_raw" => ws::ws_write_raw, 2;
|
|
114
|
+
"ws_close" => ws::ws_close, 2;
|
|
115
|
+
"ws_abort" => ws::abort, 1;
|
|
116
|
+
"ws_fail" => ws::fail, 1;
|
|
117
|
+
"ws_handled" => ws::handled, 1;
|
|
118
|
+
"ws_shutdown_ran" => ws::shutdown_ran, 1;
|
|
119
|
+
"ws_pending" => ws::pending, 1;
|
|
120
|
+
"ws_registered" => ws::registered, 1;
|
|
121
|
+
"ws_subscribe" => ws::topics::ws_subscribe, 5;
|
|
122
|
+
"ws_unsubscribe" => ws::topics::ws_unsubscribe, 3;
|
|
123
|
+
"ws_publish" => ws::topics::ws_publish, 3;
|
|
124
|
+
);
|
|
94
125
|
|
|
95
126
|
let worker = native.define_class("Worker", ruby.class_object())?;
|
|
96
|
-
|
|
97
|
-
|
|
127
|
+
methods!(worker:
|
|
128
|
+
"take_one" => queue::Worker::take_one, 0;
|
|
129
|
+
"take_batch" => queue::Worker::take_batch, 1;
|
|
130
|
+
"identity" => queue::Worker::identity, 0;
|
|
131
|
+
"release" => queue::Worker::release, 0;
|
|
132
|
+
"crashed" => queue::Worker::crashed, 0;
|
|
133
|
+
"drained" => queue::Worker::drained, 1;
|
|
134
|
+
"ring" => queue::Worker::ring, 0;
|
|
135
|
+
"ready_fd" => queue::Worker::ready_fd, 0;
|
|
136
|
+
"woke" => queue::Worker::woke, 0;
|
|
137
|
+
"capacity=" => queue::Worker::set_capacity, 1;
|
|
138
|
+
"lanes?" => queue::Worker::lanes, 0;
|
|
139
|
+
"claim_warning" => queue::Worker::claim_warning, 1;
|
|
140
|
+
);
|
|
141
|
+
// Variadic (an optional `only`), which the table's one-token arity cannot say.
|
|
142
|
+
worker.define_method("take_nonblock", method!(queue::Worker::take_nonblock, -1))?;
|
|
143
|
+
worker.define_method("take_woken", method!(queue::Worker::take_woken, -1))?;
|
|
144
|
+
|
|
145
|
+
let conn = native.define_class("Conn", ruby.class_object())?;
|
|
146
|
+
methods!(conn:
|
|
147
|
+
"fd" => inline::ConnHandle::fd, 0;
|
|
148
|
+
"next_request" => inline::ConnHandle::next_request, 1;
|
|
149
|
+
"close" => inline::ConnHandle::close, 0;
|
|
150
|
+
);
|
|
98
151
|
|
|
99
152
|
let request = native.define_class("Request", ruby.class_object())?;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
"respond_and_take_one",
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
153
|
+
methods!(request:
|
|
154
|
+
"respond_and_take" => queue::respond_and_take, 5;
|
|
155
|
+
"respond_and_take_one" => queue::respond_and_take_one, 4;
|
|
156
|
+
"read_body" => Request::read_body, 1;
|
|
157
|
+
"read_body_nonblock" => Request::read_body_nonblock, 1;
|
|
158
|
+
"send_simple" => crate::request::respond_simple, 3;
|
|
159
|
+
"send_headers" => Request::send_headers, 2;
|
|
160
|
+
// Request#write_chunk (worker.rb) waits on what this hands back.
|
|
161
|
+
"write_chunk_or_wait" => Request::write_chunk, 1;
|
|
162
|
+
"write_chunk_nonblock" => Request::write_chunk_nonblock, 1;
|
|
163
|
+
"waited" => Request::waited, 0;
|
|
164
|
+
"waited_writable" => Request::waited_writable, 0;
|
|
165
|
+
"close_waits" => Request::close_waits, 0;
|
|
166
|
+
"finish" => Request::finish, 0;
|
|
167
|
+
"abort" => Request::abort, 0;
|
|
168
|
+
"timing" => Request::set_timing, 2;
|
|
169
|
+
"upgrade_websocket" => Request::upgrade_websocket, 2;
|
|
170
|
+
"switch_protocols" => Request::switch_protocols, 0;
|
|
171
|
+
"hijack" => Request::hijack, 0;
|
|
172
|
+
"hijack_switched" => Request::hijack_switched, 1;
|
|
173
|
+
"hijacked?" => Request::hijacked, 0;
|
|
174
|
+
);
|
|
112
175
|
|
|
113
|
-
|
|
114
|
-
// resolves them lazily on first wrap, and a racy first resolution from
|
|
115
|
-
// two worker ractors is the failure mode we must rule out.
|
|
116
|
-
let _ = <Request as magnus::TypedData>::class(ruby);
|
|
117
|
-
let _ = <queue::Worker as magnus::TypedData>::class(ruby);
|
|
118
|
-
let _ = <pin::PinKeeper as magnus::TypedData>::class(ruby);
|
|
176
|
+
typed_data::resolve_classes(ruby);
|
|
119
177
|
|
|
120
178
|
// Frozen env key/value cache: built once here (main ractor, GVL held),
|
|
121
179
|
// shared by every worker ractor afterwards.
|
|
122
180
|
env_strings::init(ruby);
|
|
181
|
+
// So are the Symbols natives tell apart or hand Ruby.
|
|
182
|
+
symbols::init(ruby);
|
|
123
183
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
184
|
+
functions!(native:
|
|
185
|
+
"_test_channel_create" => test_support::create, 1;
|
|
186
|
+
"_test_push" => test_support::push, 2;
|
|
187
|
+
"_test_take" => test_support::take, 1;
|
|
188
|
+
"_test_close" => test_support::close, 1;
|
|
189
|
+
"_test_panic" => test_support::panic_in_release, 0;
|
|
190
|
+
"_test_env_probe" => test_support::env_probe, 4;
|
|
191
|
+
);
|
|
130
192
|
|
|
131
193
|
Ok(())
|
|
132
194
|
}
|
data/ext/kino/src/listen.rs
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
//! nginx). Binding is synchronous so an address conflict surfaces at boot.
|
|
4
4
|
|
|
5
5
|
use std::io;
|
|
6
|
+
use std::os::fd::{AsFd, AsRawFd, BorrowedFd};
|
|
7
|
+
use std::os::unix::fs::FileTypeExt;
|
|
6
8
|
use std::os::unix::net::UnixListener;
|
|
7
9
|
use std::path::{Path, PathBuf};
|
|
8
10
|
|
|
@@ -15,6 +17,10 @@ pub fn unix_path(bind: &str) -> Option<&Path> {
|
|
|
15
17
|
bind.strip_prefix(UNIX_SCHEME).map(Path::new)
|
|
16
18
|
}
|
|
17
19
|
|
|
20
|
+
/// The client listener's accept queue: as long as the kernel allows (its somaxconn). At std's
|
|
21
|
+
/// 128, Linux drops the SYNs of a connect burst past it, and those clients retry a second later.
|
|
22
|
+
const CLIENT_BACKLOG: libc::c_int = libc::c_int::MAX;
|
|
23
|
+
|
|
18
24
|
/// A bound, non-blocking listener of either kind.
|
|
19
25
|
pub enum Listener {
|
|
20
26
|
Tcp(std::net::TcpListener),
|
|
@@ -23,16 +29,19 @@ pub enum Listener {
|
|
|
23
29
|
|
|
24
30
|
impl Listener {
|
|
25
31
|
/// Bind `bind:port` (TCP; a hostname resolves to its addresses and the
|
|
26
|
-
/// first that binds wins) or `unix://path
|
|
32
|
+
/// first that binds wins) or `unix://path`, with CLIENT_BACKLOG.
|
|
27
33
|
pub fn bind(bind: &str, port: u16) -> io::Result<Listener> {
|
|
28
|
-
match unix_path(bind) {
|
|
29
|
-
Some(path) =>
|
|
34
|
+
let listener = match unix_path(bind) {
|
|
35
|
+
Some(path) => Listener::Unix(bind_unix(path)?, path.to_path_buf()),
|
|
30
36
|
None => {
|
|
31
37
|
let listener = std::net::TcpListener::bind((bind, port))?;
|
|
32
38
|
listener.set_nonblocking(true)?;
|
|
33
|
-
|
|
39
|
+
Listener::Tcp(listener)
|
|
34
40
|
}
|
|
35
|
-
}
|
|
41
|
+
};
|
|
42
|
+
// Failing that, the listener keeps std's queue: no reason to refuse the boot.
|
|
43
|
+
let _ = set_backlog(&listener, CLIENT_BACKLOG);
|
|
44
|
+
Ok(listener)
|
|
36
45
|
}
|
|
37
46
|
|
|
38
47
|
/// The bound TCP port; 0 for a unix socket, which has none.
|
|
@@ -44,20 +53,47 @@ impl Listener {
|
|
|
44
53
|
}
|
|
45
54
|
}
|
|
46
55
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
impl AsFd for Listener {
|
|
57
|
+
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
58
|
+
match self {
|
|
59
|
+
Listener::Tcp(listener) => listener.as_fd(),
|
|
60
|
+
Listener::Unix(listener, _) => listener.as_fd(),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// listen(2) again on a listening socket: only its queue changes.
|
|
66
|
+
pub fn set_backlog(socket: &impl AsFd, backlog: libc::c_int) -> io::Result<()> {
|
|
67
|
+
// SAFETY: a BorrowedFd of `socket`, open for the call; listen(2) reads no memory of ours.
|
|
68
|
+
if unsafe { libc::listen(socket.as_fd().as_raw_fd(), backlog) } == -1 {
|
|
69
|
+
return Err(io::Error::last_os_error());
|
|
70
|
+
}
|
|
71
|
+
Ok(())
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/// Bind a unix domain socket at `path`. A socket file already there is a live listener's
|
|
75
|
+
/// (refused: never taken) or a stale one a crashed process left (unlinked and reclaimed), which
|
|
76
|
+
/// `listening` tells apart. Anything else there is a mistyped path: refused, and never unlinked.
|
|
77
|
+
/// Nothing locks the path, so two boots at the same instant on a stale one may both bind it,
|
|
78
|
+
/// leaving the first with a socket no path names.
|
|
51
79
|
pub fn bind_unix(path: &Path) -> io::Result<UnixListener> {
|
|
52
|
-
match std::
|
|
53
|
-
Ok(
|
|
54
|
-
|
|
80
|
+
match std::fs::symlink_metadata(path) {
|
|
81
|
+
Ok(found) if found.file_type().is_socket() => {
|
|
82
|
+
if listening(path)? {
|
|
83
|
+
return Err(io::Error::new(io::ErrorKind::AddrInUse, "socket is in use"));
|
|
84
|
+
}
|
|
55
85
|
match std::fs::remove_file(path) {
|
|
56
86
|
Ok(()) => {}
|
|
57
87
|
Err(e) if e.kind() == io::ErrorKind::NotFound => {}
|
|
58
88
|
Err(e) => return Err(e),
|
|
59
89
|
}
|
|
60
90
|
}
|
|
91
|
+
Ok(_) => {
|
|
92
|
+
return Err(io::Error::new(
|
|
93
|
+
io::ErrorKind::AlreadyExists,
|
|
94
|
+
format!("{}: exists and is not a socket", path.display()),
|
|
95
|
+
))
|
|
96
|
+
}
|
|
61
97
|
Err(e) if e.kind() == io::ErrorKind::NotFound => {}
|
|
62
98
|
Err(e) => return Err(e),
|
|
63
99
|
}
|
|
@@ -66,6 +102,27 @@ pub fn bind_unix(path: &Path) -> io::Result<UnixListener> {
|
|
|
66
102
|
Ok(listener)
|
|
67
103
|
}
|
|
68
104
|
|
|
105
|
+
/// Whether anything listens at `path`, by a connect that never waits. A listener whose accept
|
|
106
|
+
/// queue is full answers EAGAIN on Linux, which is in use, where a blocking connect would wait
|
|
107
|
+
/// for room; macOS refuses that connect as it refuses one to a stale file, so there a live
|
|
108
|
+
/// server with a full queue reads as stale.
|
|
109
|
+
fn listening(path: &Path) -> io::Result<bool> {
|
|
110
|
+
match connect_nonblocking(path) {
|
|
111
|
+
Ok(_) => Ok(true),
|
|
112
|
+
Err(e) if e.kind() == io::ErrorKind::WouldBlock => Ok(true),
|
|
113
|
+
Err(e) if e.kind() == io::ErrorKind::ConnectionRefused => Ok(false),
|
|
114
|
+
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(false),
|
|
115
|
+
Err(e) => Err(e),
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
fn connect_nonblocking(path: &Path) -> io::Result<socket2::Socket> {
|
|
120
|
+
let socket = socket2::Socket::new(socket2::Domain::UNIX, socket2::Type::STREAM, None)?;
|
|
121
|
+
socket.set_nonblocking(true)?;
|
|
122
|
+
socket.connect(&socket2::SockAddr::unix(path)?)?;
|
|
123
|
+
Ok(socket)
|
|
124
|
+
}
|
|
125
|
+
|
|
69
126
|
/// Remove a socket file at shutdown; one that is already gone is fine.
|
|
70
127
|
pub fn cleanup_unix(path: &Path) {
|
|
71
128
|
let _ = std::fs::remove_file(path);
|
|
@@ -75,6 +132,12 @@ pub fn cleanup_unix(path: &Path) {
|
|
|
75
132
|
mod tests {
|
|
76
133
|
use super::{bind_unix, unix_path, Listener};
|
|
77
134
|
use std::path::PathBuf;
|
|
135
|
+
#[cfg(target_os = "linux")]
|
|
136
|
+
use {
|
|
137
|
+
super::{connect_nonblocking, set_backlog},
|
|
138
|
+
std::io,
|
|
139
|
+
std::time::Duration,
|
|
140
|
+
};
|
|
78
141
|
|
|
79
142
|
/// A socket path unique to this process and test. macOS caps sun_path
|
|
80
143
|
/// at 104 bytes, so the name stays short.
|
|
@@ -128,10 +191,115 @@ mod tests {
|
|
|
128
191
|
let _ = std::fs::remove_file(&path);
|
|
129
192
|
}
|
|
130
193
|
|
|
194
|
+
/// On Linux a connect to a full accept queue fails with EAGAIN, which reads as in use, and
|
|
195
|
+
/// a blocking one would wait for room: the bind returns within two seconds, on a thread of
|
|
196
|
+
/// its own. (macOS refuses that connect as it does one to a stale file: a documented limit.)
|
|
197
|
+
#[cfg(target_os = "linux")]
|
|
198
|
+
#[test]
|
|
199
|
+
fn refuses_at_once_a_socket_whose_live_listener_has_a_full_queue_and_leaves_it_its_path() {
|
|
200
|
+
let path = socket_path("full");
|
|
201
|
+
let live = bind_unix(&path).unwrap();
|
|
202
|
+
set_backlog(&live, 8).unwrap();
|
|
203
|
+
// Every client stays open and unaccepted, until the kernel turns one away.
|
|
204
|
+
let mut clients = Vec::new();
|
|
205
|
+
loop {
|
|
206
|
+
match connect_nonblocking(&path) {
|
|
207
|
+
Ok(client) => clients.push(client),
|
|
208
|
+
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
|
|
209
|
+
Err(e) => panic!("filling the accept queue: {e}"),
|
|
210
|
+
}
|
|
211
|
+
assert!(clients.len() <= 32, "a queue of 8 never filled");
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let (tx, rx) = std::sync::mpsc::channel();
|
|
215
|
+
let second = path.clone();
|
|
216
|
+
std::thread::spawn(move || tx.send(bind_unix(&second)));
|
|
217
|
+
let refused = rx
|
|
218
|
+
.recv_timeout(Duration::from_secs(2))
|
|
219
|
+
.expect("bind_unix still waits after 2 s: it should never wait on a listener");
|
|
220
|
+
assert_eq!(refused.unwrap_err().kind(), io::ErrorKind::AddrInUse);
|
|
221
|
+
|
|
222
|
+
// Its queue drained, a new client still reaches the live listener at the path.
|
|
223
|
+
drop(clients);
|
|
224
|
+
live.set_nonblocking(true).unwrap();
|
|
225
|
+
while live.accept().is_ok() {}
|
|
226
|
+
let _client = std::os::unix::net::UnixStream::connect(&path).unwrap();
|
|
227
|
+
assert!(live.accept().is_ok(), "the live server lost its path");
|
|
228
|
+
drop(live);
|
|
229
|
+
let _ = std::fs::remove_file(&path);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
#[test]
|
|
233
|
+
fn refuses_a_path_that_holds_anything_but_a_socket_and_leaves_it_there() {
|
|
234
|
+
let path = socket_path("notsock");
|
|
235
|
+
std::fs::write(&path, "an app's data").unwrap();
|
|
236
|
+
let refused = bind_unix(&path).unwrap_err().to_string();
|
|
237
|
+
let named = refused.contains(&path.display().to_string());
|
|
238
|
+
assert!(named && refused.contains("not a socket"), "{refused}");
|
|
239
|
+
assert_eq!(std::fs::read_to_string(&path).unwrap(), "an app's data");
|
|
240
|
+
std::fs::remove_file(&path).unwrap();
|
|
241
|
+
}
|
|
242
|
+
|
|
131
243
|
#[test]
|
|
132
244
|
fn binds_tcp_on_an_ephemeral_port() {
|
|
133
245
|
let listener = Listener::bind("127.0.0.1", 0).unwrap();
|
|
134
246
|
assert!(matches!(listener, Listener::Tcp(_)));
|
|
135
247
|
assert_ne!(listener.port().unwrap(), 0);
|
|
136
248
|
}
|
|
249
|
+
|
|
250
|
+
/// Linux keeps a finished handshake in a listener's accept queue until
|
|
251
|
+
/// the server accepts it, backlog + 1 of them at most. Past that it
|
|
252
|
+
/// fails a nonblocking unix connect with EAGAIN and drops a TCP SYN, and
|
|
253
|
+
/// every retry of it while the queue stays full (Linux 4.10 on). macOS
|
|
254
|
+
/// caps every backlog at 128 by default, so there is nothing to tell
|
|
255
|
+
/// apart there: Linux only. Skipped, saying so, where somaxconn is not
|
|
256
|
+
/// above the burst.
|
|
257
|
+
#[cfg(target_os = "linux")]
|
|
258
|
+
#[test]
|
|
259
|
+
fn the_client_listener_queues_as_many_as_the_kernel_allows() {
|
|
260
|
+
use std::net::{SocketAddr, TcpStream};
|
|
261
|
+
use std::os::fd::OwnedFd;
|
|
262
|
+
|
|
263
|
+
// Clients connected one after another: well past 128, and within
|
|
264
|
+
// the kernel's default somaxconn.
|
|
265
|
+
const BURST: usize = 300;
|
|
266
|
+
let somaxconn = std::fs::read_to_string("/proc/sys/net/core/somaxconn");
|
|
267
|
+
let somaxconn: Option<usize> = somaxconn.ok().and_then(|s| s.trim().parse().ok());
|
|
268
|
+
if !somaxconn.is_some_and(|n| n > BURST) {
|
|
269
|
+
eprintln!("skipped: net.core.somaxconn is {somaxconn:?}, not above {BURST}");
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
let path = socket_path("queue");
|
|
273
|
+
let tcp = Listener::bind("127.0.0.1", 0).unwrap();
|
|
274
|
+
let unix = Listener::bind(&format!("unix://{}", path.display()), 0).unwrap();
|
|
275
|
+
let port = tcp.port().unwrap();
|
|
276
|
+
for unix_socket in [false, true] {
|
|
277
|
+
// A TCP connect a full queue holds back never finishes, as
|
|
278
|
+
// nothing accepts: its SYN and the retries are all dropped; a
|
|
279
|
+
// nonblocking unix one fails at once.
|
|
280
|
+
let connect = || -> io::Result<OwnedFd> {
|
|
281
|
+
if unix_socket {
|
|
282
|
+
return connect_nonblocking(&path).map(OwnedFd::from);
|
|
283
|
+
}
|
|
284
|
+
let addr = SocketAddr::from(([127, 0, 0, 1], port));
|
|
285
|
+
TcpStream::connect_timeout(&addr, Duration::from_secs(2)).map(OwnedFd::from)
|
|
286
|
+
};
|
|
287
|
+
let mut clients = Vec::with_capacity(BURST);
|
|
288
|
+
let refused = loop {
|
|
289
|
+
match connect() {
|
|
290
|
+
Ok(client) if clients.len() + 1 < BURST => clients.push(client),
|
|
291
|
+
Ok(_) => break None,
|
|
292
|
+
Err(e) => break Some(e),
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
assert!(
|
|
296
|
+
refused.is_none(),
|
|
297
|
+
"unix {unix_socket}: the client listener held {} handshakes, then {refused:?}: \
|
|
298
|
+
its backlog should be net.core.somaxconn, not 128",
|
|
299
|
+
clients.len()
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
drop(unix);
|
|
303
|
+
let _ = std::fs::remove_file(&path);
|
|
304
|
+
}
|
|
137
305
|
}
|
data/ext/kino/src/log.rs
CHANGED
|
@@ -94,59 +94,24 @@ pub fn log_line(ruby: &Ruby, level: String, source: String, message: String) ->
|
|
|
94
94
|
|
|
95
95
|
#[cfg(test)]
|
|
96
96
|
mod tests {
|
|
97
|
-
use super::{format_line,
|
|
98
|
-
|
|
99
|
-
#[test]
|
|
100
|
-
fn label_is_a_syslog_tag_plus_the_source() {
|
|
101
|
-
assert_eq!(label(4213, "main"), "kino[4213] main:");
|
|
102
|
-
assert_eq!(
|
|
103
|
-
label(4213, "worker-3/thread-2"),
|
|
104
|
-
"kino[4213] worker-3/thread-2:"
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
#[test]
|
|
109
|
-
fn plain_line_is_label_then_message() {
|
|
110
|
-
assert_eq!(
|
|
111
|
-
format_line(Level::Info, "kino[1] main:", "hello", false),
|
|
112
|
-
"kino[1] main: hello"
|
|
113
|
-
);
|
|
114
|
-
}
|
|
97
|
+
use super::{format_line, Level};
|
|
115
98
|
|
|
99
|
+
// The label, a plain line and a report's, each on its level's stream: spec/log_spec.rb, which
|
|
100
|
+
// sees no color.
|
|
116
101
|
#[test]
|
|
117
102
|
fn color_styles_only_the_label_by_level() {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
"\x1b[
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
"
|
|
125
|
-
|
|
126
|
-
assert_eq!(
|
|
127
|
-
format_line(Level::Error, "kino[1] main:", "broke", true),
|
|
128
|
-
"\x1b[91mkino[1] main:\x1b[0m broke"
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
#[test]
|
|
133
|
-
fn a_report_is_labelled_on_its_first_line_only() {
|
|
134
|
-
assert_eq!(
|
|
135
|
-
format_line(
|
|
136
|
-
Level::Error,
|
|
137
|
-
"kino[1] main:",
|
|
138
|
-
"500 GET / · X: y\n a.rb:1",
|
|
139
|
-
false
|
|
140
|
-
),
|
|
141
|
-
"kino[1] main: 500 GET / · X: y\n a.rb:1"
|
|
142
|
-
);
|
|
103
|
+
let rows = [
|
|
104
|
+
(Level::Info, "\x1b[90mkino[1] main:\x1b[0m hello"),
|
|
105
|
+
(Level::Warn, "\x1b[33mkino[1] main:\x1b[0m hello"),
|
|
106
|
+
(Level::Error, "\x1b[91mkino[1] main:\x1b[0m hello"),
|
|
107
|
+
];
|
|
108
|
+
for (level, line) in rows {
|
|
109
|
+
assert_eq!(format_line(level, "kino[1] main:", "hello", true), line);
|
|
110
|
+
}
|
|
143
111
|
}
|
|
144
112
|
|
|
145
113
|
#[test]
|
|
146
|
-
fn
|
|
147
|
-
assert_eq!(Level::parse("info"), Some(Level::Info));
|
|
148
|
-
assert_eq!(Level::parse("warn"), Some(Level::Warn));
|
|
149
|
-
assert_eq!(Level::parse("error"), Some(Level::Error));
|
|
114
|
+
fn a_level_kino_log_never_names_is_refused() {
|
|
150
115
|
assert_eq!(Level::parse("debug"), None);
|
|
151
116
|
}
|
|
152
117
|
}
|
data/ext/kino/src/logsink.rs
CHANGED
|
@@ -118,7 +118,6 @@ mod tests {
|
|
|
118
118
|
use super::Sink;
|
|
119
119
|
use std::io::Write;
|
|
120
120
|
use std::sync::Arc;
|
|
121
|
-
use std::time::{Duration, Instant};
|
|
122
121
|
|
|
123
122
|
#[derive(Clone, Default)]
|
|
124
123
|
struct SharedBuf(Arc<parking_lot::Mutex<Vec<u8>>>);
|
|
@@ -143,13 +142,8 @@ mod tests {
|
|
|
143
142
|
sink.write_line("second".to_string());
|
|
144
143
|
drop(sink); // sender gone: the flusher drains everything and exits
|
|
145
144
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
151
|
-
assert!(Instant::now() < deadline, "flusher never drained");
|
|
152
|
-
std::thread::sleep(Duration::from_millis(5));
|
|
153
|
-
}
|
|
145
|
+
crate::testing::eventually_blocking("the flusher to drain", || {
|
|
146
|
+
(*buf.0.lock() == b"first\nsecond\n").then_some(())
|
|
147
|
+
});
|
|
154
148
|
}
|
|
155
149
|
}
|
data/ext/kino/src/mono.rs
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
//! Process-monotonic
|
|
2
|
-
//! (recording) and the control thread (reading busy
|
|
3
|
-
//! wall-clock and of Ruby, so it is identical in
|
|
1
|
+
//! Process-monotonic microsecond clock, shared by the request hot path
|
|
2
|
+
//! (recording), the lane dispatcher and the control thread (reading busy
|
|
3
|
+
//! age). Independent of wall-clock and of Ruby, so it is identical in
|
|
4
|
+
//! :ractor and :threaded.
|
|
4
5
|
|
|
5
6
|
use std::sync::OnceLock;
|
|
6
7
|
use std::time::Instant;
|
|
7
8
|
|
|
8
9
|
static MONO_EPOCH: OnceLock<Instant> = OnceLock::new();
|
|
9
10
|
|
|
10
|
-
///
|
|
11
|
-
///
|
|
12
|
-
pub fn
|
|
13
|
-
MONO_EPOCH.get_or_init(Instant::now).elapsed().
|
|
11
|
+
/// Microseconds since the first call anywhere in the process, from 1 (0 means never). The u128
|
|
12
|
+
/// micros fit u64 for any realistic uptime (u64 us is ~584,000 years).
|
|
13
|
+
pub fn mono_us() -> u64 {
|
|
14
|
+
MONO_EPOCH.get_or_init(Instant::now).elapsed().as_micros() as u64 + 1
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
#[test]
|
|
21
|
-
fn mono_ms_is_monotonic_nondecreasing() {
|
|
22
|
-
let a = mono_ms();
|
|
23
|
-
let b = mono_ms();
|
|
24
|
-
assert!(b >= a, "clock went backwards: {a} then {b}");
|
|
25
|
-
}
|
|
17
|
+
/// mono_us at `at`, for a caller that read the clock already (1 for a moment before the epoch).
|
|
18
|
+
pub fn us_at(at: Instant) -> u64 {
|
|
19
|
+
let epoch = *MONO_EPOCH.get_or_init(Instant::now);
|
|
20
|
+
at.saturating_duration_since(epoch).as_micros() as u64 + 1
|
|
26
21
|
}
|