kino 0.7.1 → 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.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +5 -0
  3. data/CHANGELOG.md +286 -0
  4. data/Cargo.lock +209 -132
  5. data/README.md +269 -438
  6. data/doc/README.md +27 -2
  7. data/doc/architecture.md +862 -43
  8. data/doc/benchmarks.md +444 -214
  9. data/doc/configuration.md +215 -0
  10. data/doc/operations.md +258 -0
  11. data/doc/rails-on-ractors.md +161 -49
  12. data/doc/streaming.md +52 -0
  13. data/doc/websockets.md +469 -0
  14. data/doc/why-kino.md +6 -6
  15. data/doc/worker-loop.md +462 -0
  16. data/ext/kino/Cargo.toml +19 -3
  17. data/ext/kino/build.rs +32 -1
  18. data/ext/kino/clippy.toml +10 -0
  19. data/ext/kino/src/access_log.rs +70 -92
  20. data/ext/kino/src/control.rs +26 -685
  21. data/ext/kino/src/counters.rs +132 -0
  22. data/ext/kino/src/cpus.rs +1 -18
  23. data/ext/kino/src/dispatch/tests.rs +731 -0
  24. data/ext/kino/src/dispatch.rs +276 -0
  25. data/ext/kino/src/doorbell.rs +108 -0
  26. data/ext/kino/src/env_strings.rs +26 -30
  27. data/ext/kino/src/framing.rs +110 -0
  28. data/ext/kino/src/gvl.rs +292 -66
  29. data/ext/kino/src/hijack/tests.rs +416 -0
  30. data/ext/kino/src/hijack.rs +471 -0
  31. data/ext/kino/src/inline/answer.rs +488 -0
  32. data/ext/kino/src/inline/conn.rs +1124 -0
  33. data/ext/kino/src/inline/deadline.rs +421 -0
  34. data/ext/kino/src/inline/encode.rs +119 -0
  35. data/ext/kino/src/inline/io.rs +487 -0
  36. data/ext/kino/src/inline/mod.rs +766 -0
  37. data/ext/kino/src/inline/parity.rs +862 -0
  38. data/ext/kino/src/inline/parse.rs +131 -0
  39. data/ext/kino/src/inline/spin.rs +428 -0
  40. data/ext/kino/src/inline/testing.rs +628 -0
  41. data/ext/kino/src/io_shards.rs +240 -192
  42. data/ext/kino/src/lane_room.rs +99 -0
  43. data/ext/kino/src/lib.rs +139 -77
  44. data/ext/kino/src/listen.rs +180 -12
  45. data/ext/kino/src/log.rs +12 -47
  46. data/ext/kino/src/logsink.rs +3 -9
  47. data/ext/kino/src/mono.rs +12 -17
  48. data/ext/kino/src/pin.rs +3 -3
  49. data/ext/kino/src/queue/mod.rs +54 -0
  50. data/ext/kino/src/queue/take.rs +503 -0
  51. data/ext/kino/src/queue/testing.rs +107 -0
  52. data/ext/kino/src/queue/tests.rs +581 -0
  53. data/ext/kino/src/queue/worker.rs +501 -0
  54. data/ext/kino/src/registry.rs +168 -379
  55. data/ext/kino/src/request.rs +834 -235
  56. data/ext/kino/src/response.rs +532 -94
  57. data/ext/kino/src/server/accept.rs +322 -0
  58. data/ext/kino/src/server/config.rs +138 -0
  59. data/ext/kino/src/server/handle.rs +247 -0
  60. data/ext/kino/src/server/intake.rs +217 -0
  61. data/ext/kino/src/server/mod.rs +157 -0
  62. data/ext/kino/src/server/natives.rs +223 -0
  63. data/ext/kino/src/server/serve/intake_tests.rs +458 -0
  64. data/ext/kino/src/server/serve.rs +436 -0
  65. data/ext/kino/src/server/tests.rs +532 -0
  66. data/ext/kino/src/shared_gvl.rs +50 -0
  67. data/ext/kino/src/slot/lane.rs +138 -0
  68. data/ext/kino/src/slot/life.rs +111 -0
  69. data/ext/kino/src/slot/load.rs +193 -0
  70. data/ext/kino/src/slot/mod.rs +494 -0
  71. data/ext/kino/src/slot/park.rs +82 -0
  72. data/ext/kino/src/stats.rs +936 -0
  73. data/ext/kino/src/symbols.rs +66 -0
  74. data/ext/kino/src/test_support.rs +24 -14
  75. data/ext/kino/src/testing.rs +409 -0
  76. data/ext/kino/src/ticker.rs +87 -0
  77. data/ext/kino/src/timer.rs +4 -7
  78. data/ext/kino/src/tls.rs +101 -131
  79. data/ext/kino/src/typed_data.rs +131 -0
  80. data/ext/kino/src/upgrade.rs +77 -0
  81. data/ext/kino/src/ws/codec.rs +470 -0
  82. data/ext/kino/src/ws/drive.rs +600 -0
  83. data/ext/kino/src/ws/handshake.rs +267 -0
  84. data/ext/kino/src/ws/mod.rs +317 -0
  85. data/ext/kino/src/ws/server_tests.rs +759 -0
  86. data/ext/kino/src/ws/socket.rs +279 -0
  87. data/ext/kino/src/ws/stats.rs +88 -0
  88. data/ext/kino/src/ws/testing.rs +156 -0
  89. data/ext/kino/src/ws/topics.rs +550 -0
  90. data/lib/kino/cli.rb +100 -9
  91. data/lib/kino/configuration.rb +32 -10
  92. data/lib/kino/crash_backoff.rb +56 -0
  93. data/lib/kino/input.rb +36 -17
  94. data/lib/kino/log.rb +8 -1
  95. data/lib/kino/monitor.rb +1 -1
  96. data/lib/kino/pool.rb +169 -0
  97. data/lib/kino/pool_scaler.rb +12 -13
  98. data/lib/kino/quarantine_monitor.rb +3 -3
  99. data/lib/kino/ractor_supervisor.rb +201 -161
  100. data/lib/kino/server.rb +291 -185
  101. data/lib/kino/slot_bank.rb +21 -9
  102. data/lib/kino/stream.rb +2 -2
  103. data/lib/kino/teardown.rb +33 -0
  104. data/lib/kino/templates/kino.rb.tt +41 -6
  105. data/lib/kino/threaded_pool.rb +87 -102
  106. data/lib/kino/version.rb +1 -1
  107. data/lib/kino/web_socket.rb +125 -0
  108. data/lib/kino/worker/loop.rb +319 -0
  109. data/lib/kino/worker.rb +345 -79
  110. data/lib/kino/worker_hooks.rb +7 -1
  111. data/lib/kino/worker_row.rb +15 -0
  112. data/lib/kino.rb +27 -3
  113. data/lib/rackup/handler/kino.rb +1 -1
  114. data/sig/kino.rbs +93 -10
  115. metadata +66 -3
  116. data/ext/kino/src/queue.rs +0 -333
  117. data/ext/kino/src/server.rs +0 -1785
@@ -0,0 +1,132 @@
1
+ //! Counts a guard holds while what they count lives, and process-wide counts by cause.
2
+
3
+ use std::marker::PhantomData;
4
+ use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
5
+ use std::sync::Arc;
6
+
7
+ /// A count held on an atomic counter while this lives, and given back as
8
+ /// it drops, however its holder ends: closed, cancelled at teardown, or
9
+ /// unwound. The counter is `owner` itself, or one of its fields.
10
+ pub(crate) struct Counted<O = AtomicUsize> {
11
+ owner: Arc<O>,
12
+ counter: fn(&O) -> &AtomicUsize,
13
+ n: usize,
14
+ }
15
+
16
+ impl Counted {
17
+ pub(crate) fn hold(counter: &Arc<AtomicUsize>, n: usize) -> Self {
18
+ counter.fetch_add(n, Ordering::SeqCst);
19
+ Self::adopt(counter.clone(), n)
20
+ }
21
+
22
+ /// Hold the `n` the caller already counted on `counter`.
23
+ pub(crate) fn adopt(counter: Arc<AtomicUsize>, n: usize) -> Self {
24
+ Counted {
25
+ owner: counter,
26
+ counter: |counter| counter,
27
+ n,
28
+ }
29
+ }
30
+ }
31
+
32
+ impl<O> Counted<O> {
33
+ pub(crate) fn on(owner: &Arc<O>, counter: fn(&O) -> &AtomicUsize) -> Self {
34
+ counter(owner).fetch_add(1, Ordering::SeqCst);
35
+ Counted {
36
+ owner: owner.clone(),
37
+ counter,
38
+ n: 1,
39
+ }
40
+ }
41
+ pub(crate) fn owner(&self) -> &Arc<O> {
42
+ &self.owner
43
+ }
44
+ }
45
+
46
+ impl<O> Drop for Counted<O> {
47
+ fn drop(&mut self) {
48
+ (self.counter)(&self.owner).fetch_sub(self.n, Ordering::SeqCst);
49
+ }
50
+ }
51
+
52
+ /// An enum whose ALL lists each variant once, named for /stats, Server#stats and the
53
+ /// metrics' `cause` label; declared with `tallied!`.
54
+ pub(crate) trait Tallied<const N: usize>: Copy {
55
+ const ALL: [Self; N];
56
+
57
+ fn name(self) -> &'static str;
58
+
59
+ fn index(self) -> usize;
60
+
61
+ /// `counts`, indexed as ALL, by name.
62
+ fn named(counts: [u64; N]) -> [(&'static str, u64); N] {
63
+ Self::ALL.map(|cause| (cause.name(), counts[cause.index()]))
64
+ }
65
+ }
66
+
67
+ /// An enum and its `Tallied` impl: each variant, in ALL's order, with
68
+ /// its name. The first may set where the discriminants start.
69
+ macro_rules! tallied {
70
+ (
71
+ $(#[$meta:meta])*
72
+ $vis:vis enum $enum:ident {
73
+ $(#[$first_doc:meta])* $first:ident $(= $start:literal)? => $first_name:literal,
74
+ $($(#[$doc:meta])* $variant:ident => $name:literal,)*
75
+ }
76
+ ) => {
77
+ $(#[$meta])*
78
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
79
+ $vis enum $enum {
80
+ $(#[$first_doc])* $first $(= $start)?,
81
+ $($(#[$doc])* $variant,)*
82
+ }
83
+
84
+ impl $crate::counters::Tallied<{ [$first_name $(, $name)*].len() }> for $enum {
85
+ const ALL: [$enum; [$first_name $(, $name)*].len()] =
86
+ [$enum::$first $(, $enum::$variant)*];
87
+
88
+ fn name(self) -> &'static str {
89
+ match self {
90
+ $enum::$first => $first_name,
91
+ $($enum::$variant => $name,)*
92
+ }
93
+ }
94
+
95
+ fn index(self) -> usize {
96
+ self as usize - Self::ALL[0] as usize
97
+ }
98
+ }
99
+ };
100
+ }
101
+ pub(crate) use tallied;
102
+
103
+ /// Process-wide counts by cause (Relaxed, advisory).
104
+ pub(crate) struct Tally<K, const N: usize> {
105
+ counts: [AtomicU64; N],
106
+ by: PhantomData<fn(K)>,
107
+ }
108
+
109
+ impl<K: Tallied<N>, const N: usize> Tally<K, N> {
110
+ pub(crate) const fn new() -> Self {
111
+ Tally {
112
+ counts: [const { AtomicU64::new(0) }; N],
113
+ by: PhantomData,
114
+ }
115
+ }
116
+
117
+ pub(crate) fn add(&self, cause: K) {
118
+ self.add_n(cause, 1);
119
+ }
120
+
121
+ #[inline]
122
+ pub(crate) fn add_n(&self, cause: K, n: u64) {
123
+ self.counts[cause.index()].fetch_add(n, Ordering::Relaxed);
124
+ }
125
+
126
+ /// The counts now, indexed as K::ALL.
127
+ pub(crate) fn read(&self) -> [u64; N] {
128
+ self.counts
129
+ .each_ref()
130
+ .map(|count| count.load(Ordering::Relaxed))
131
+ }
132
+ }
data/ext/kino/src/cpus.rs CHANGED
@@ -15,23 +15,6 @@ pub fn available_parallelism(_ruby: &Ruby) -> Result<usize, Error> {
15
15
  Ok(count())
16
16
  }
17
17
 
18
- fn count() -> usize {
18
+ pub(crate) fn count() -> usize {
19
19
  std::thread::available_parallelism().map_or(1, |n| n.get())
20
20
  }
21
-
22
- #[cfg(test)]
23
- mod tests {
24
- use super::count;
25
-
26
- #[test]
27
- fn reports_at_least_one_cpu() {
28
- assert!(count() >= 1);
29
- }
30
-
31
- #[test]
32
- fn never_exceeds_what_the_os_reports_as_online() {
33
- // A quota can only lower the count below the online CPUs; never raise it.
34
- let online = std::thread::available_parallelism().map_or(1, |n| n.get());
35
- assert!(count() <= online);
36
- }
37
- }