hyperion-rb 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53235f97fb1e384507f62373cd12180ade1eecc8df6f9ce75145ba60403a983e
4
- data.tar.gz: 1c58ede296c54a098d26cae2b69837f23bf60fb5ce4239c033446f583f12a4df
3
+ metadata.gz: 94ad9628c5683d6b9c9d2d8359066e4bda37d29c7ec2e4fb4e48ae726f8ff939
4
+ data.tar.gz: 7ebf3c87d10a384e388cf7f2c28d276c0975044a49fe09e1174ffa331cab76b4
5
5
  SHA512:
6
- metadata.gz: 8484e7168d8ba27312edece5c86af770ef0604bf85d13cdde37c5f4c87b9de0417216f241e073340bedeabef292fc4ec032a7379a76a836e236f6f129c97bcd3
7
- data.tar.gz: 89ac23881d0ddd4beff79d08551fa6f7e8399948c1607a3a32475202780170dc9c036f1ef93bd1f17dc5a34e1d91d9901bf3e4119e9efea05d5aa528b22271ff
6
+ metadata.gz: 9fb844690d5a06aa55335211bdaf3a5276b48f7cfd8a248bb714258ae02549a7e595bc1088987ae9c77d3f28aa1efec91fcd8cc401c26264620f48d78d85faf3
7
+ data.tar.gz: d050bffe3c8fed2ae1ce434fb7ccf8904a418e3fa18c6afd3e4314b841215ca0dec621edb7bc5f80350e4807e04503b1e785786fb57503b716e52afaf2e30a20
data/CHANGELOG.md CHANGED
@@ -1,5 +1,47 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.4.0] - 2026-04-27
4
+
5
+ Default-behaviour change for TLS users: HTTP/1.1-over-TLS now dispatches inline on the calling fiber instead of hopping through the worker thread pool. Fiber-cooperative libraries (`hyperion-async-pg`, `async-redis`) work on the TLS h1 path without `--async-io`. No code-path changes for plain HTTP/1.1 default behaviour.
6
+
7
+ ### Changed
8
+ - **TLS h1 inline dispatch by default** — `Hyperion::Server#dispatch` now serves HTTP/1.1-over-TLS inline on the accept-loop fiber under `Async::Scheduler`. Rationale: the TLS path already wraps the accept loop in `Async {}` for ALPN handshake + h2 streams; handing the post-handshake socket to a worker thread strips that scheduler context for no perf benefit (the Async-loop cost is already paid) and defeats fiber-cooperative I/O on TLS. Operators no longer need to pair `--tls-cert/--tls-key` with `--async-io` to get `hyperion-async-pg` working on TLS — it just works.
9
+ - **`async_io` config is now three-way** — was Boolean (`true` / `false`, default `false`). Now `nil` (default, "auto" — pool on plain HTTP/1.1, inline on TLS h1), `true` (force inline-on-fiber everywhere — required for `hyperion-async-pg` on plain HTTP/1.1), `false` (force pool hop everywhere — explicit opt-out for the rare operator who wants TLS+threadpool, e.g. CPU-bound synchronous handlers competing for OS threads).
10
+ - **Server / Worker constructor defaults** — `Hyperion::Server#initialize` and `Hyperion::Worker#initialize` now default `async_io: nil`. `Hyperion::Config::DEFAULTS[:async_io]` is `nil`.
11
+
12
+ ### Migration
13
+ - **Most users want the new default and should do nothing.** Wait-bound TLS workloads paired with fiber-cooperative I/O libraries (async-pg, async-redis) are now strictly faster on TLS — no flag flip required.
14
+ - **CPU-bound TLS handlers that want true OS-thread parallelism** (synchronous Rack handlers holding a global mutex, no Async-aware libraries in the stack) should set `async_io false` in their `config/hyperion.rb` (or pass `async_io: false` to `Server.new`). This restores the 1.3.x pool-hop behaviour for TLS h1.
15
+ - The plain HTTP/1.1 default path is unchanged: still pool dispatch, still the raw-loop perf-bypass; `--async-io` / `async_io: true` semantics for plain HTTP/1.1 are unchanged.
16
+
17
+ ### Added
18
+ - **`spec/hyperion/server_tls_dispatch_spec.rb`** — three new examples covering the matrix (nil + TLS → inline; false + TLS → pool; true + TLS → inline). Behavioural assertions verify `Fiber.scheduler` presence and which OS thread ran the handler (accept-loop vs pool worker).
19
+ - **README** — TLS + async-pg note rewritten for 1.4.0; config-DSL example block now documents the three-way `async_io` setting.
20
+
21
+ ### Fixed
22
+ - N/A — pure default-behaviour change with explicit opt-out.
23
+
24
+ ## [1.3.1] - 2026-04-27
25
+
26
+ Documentation + observability follow-ups for the 1.3.0 `--async-io` feature. No behaviour changes to existing code paths.
27
+
28
+ ### Added
29
+ - **Dispatch-path metrics** — `Hyperion::Server` now bumps two new counters so operators can verify which path served their requests:
30
+ - `:requests_threadpool_dispatched` — HTTP/1.1 connection handed to the worker pool (or served inline in `start_raw_loop` when `thread_count: 0`).
31
+ - `:requests_async_dispatched` — HTTP/1.1 connection served inline on the accept-loop fiber under `--async-io`.
32
+ HTTP/2 streams are not bucketed (per-stream counters cover them); the rare TLS+`thread_count: 0` config is also un-counted to avoid misclassification.
33
+ - **`docs/MIGRATING_FROM_PUMA.md`** — new "Fiber-cooperative I/O for PG-bound apps" section near the top, with the Linux 50 ms `pg_sleep` bench summary and the three-prerequisite checklist (`async_io: true` + `hyperion-async-pg` + fiber-aware pool).
34
+ - **README** — `async_io` documented in the config-DSL example block; the new dispatch-path counters listed in the Metrics table.
35
+ - **Specs** — two new examples in `spec/hyperion/server_async_io_spec.rb`:
36
+ - `async_io: true` + `thread_count: 0` boots cleanly and serves a request under a scheduler.
37
+ - Thread-decoupling proof: 5 concurrent requests against a 200 ms fiber-yielding handler complete in <600 ms wall (vs. ~1.0 s if serialized), locking in the architectural promise from the README.
38
+
39
+ ### Changed
40
+ - N/A — no behavioural changes; metrics are additive, docs are additive.
41
+
42
+ ### Fixed
43
+ - N/A.
44
+
3
45
  ## [1.3.0] - 2026-04-27
4
46
 
5
47
  Adds the structural moat for fiber-cooperative I/O. No breaking changes.
data/README.md CHANGED
@@ -65,21 +65,52 @@ Bench is **wait-bound** — ~3-4 ms median is the PG + Redis round-trip, dwarfin
65
65
 
66
66
  ### Async I/O — fiber concurrency on PG-bound apps
67
67
 
68
- `bench/pg_concurrent.ru` (50 ms PG query per request, pool sized for the server's concurrency model). macOS, Postgres over WAN, wrk `-t4 -c200 -d20s`:
68
+ Ubuntu 24.04 / 16 vCPU / Ruby 3.3.3, Postgres 17 over WAN, `wrk -t4 -c200 -d20s`. Single worker (`-w 1`) unless noted. All configs returned 0 non-2xx and 0 timeouts. RSS sampled mid-run via `ps -o rss`.
69
69
 
70
- | | r/s | p99 |
71
- |---|---:|---:|
72
- | Puma 7.2 `-t 5` + plain pg (pool=5) | 88.9 | 2.31 s |
73
- | **Hyperion 1.3.0 `--async-io -t 5` + hyperion-async-pg (FiberPool=64)** | **1,103.7** | **237 ms** |
70
+ **Wait-bound workload** (`bench/pg_concurrent.ru`: `SELECT pg_sleep(0.05)` + tiny JSON):
74
71
 
75
- **12.4× throughput, 9.7× lower p99.** Puma is bottlenecked at `threads × 1 in-flight query` because plain `pg` blocks the OS thread on `recv()`. Hyperion + async-pg + a fiber-aware pool decouples concurrency from threads: 5 OS threads serve 64 concurrent in-flight queries via fiber cooperation. Theoretical ceiling at pool=64 + 50 ms query = 1280 r/s; achieved 1103 r/s = 86% of it.
72
+ | | r/s | p99 | RSS | vs Puma `-t 5` |
73
+ |---|---:|---:|---:|---:|
74
+ | Puma 8.0 `-t 5` pool=5 | 56.5 | 3.88 s | 87 MB | 1.0× |
75
+ | Puma 8.0 `-t 30` pool=30 | 402.1 | 880 ms | 99 MB | 7.1× |
76
+ | Puma 8.0 `-t 100` pool=100 | 1067.4 | 557 ms | 121 MB | 18.9× |
77
+ | **Hyperion `--async-io -t 5`** pool=32 | 400.4 | 878 ms | 123 MB | 7.1× |
78
+ | **Hyperion `--async-io -t 5`** pool=64 | 778.9 | 638 ms | 133 MB | 13.8× |
79
+ | **Hyperion `--async-io -t 5`** pool=128 | 1344.2 | 536 ms | 148 MB | 23.8× |
80
+ | **Hyperion `--async-io -t 5` pool=200** | **2381.4** | **471 ms** | **164 MB** | **42.2×** |
81
+ | Hyperion `--async-io -w 4 -t 5` pool=64 | 1937.5 | 4.84 s | 416 MB | 34.3× (cold-start p99 — see note) |
82
+ | Falcon 0.55.3 `--count 1` pool=128 | 1665.7 | 516 ms | 141 MB | 29.5× |
83
+
84
+ **Mixed CPU+wait** (`bench/pg_mixed.ru`: same query + 50-key JSON serialization, ~5 ms CPU):
85
+
86
+ | | r/s | p99 | RSS | vs Puma `-t 30` |
87
+ |---|---:|---:|---:|---:|
88
+ | Puma 8.0 `-t 30` pool=30 | 351.7 | 963 ms | 127 MB | 1.0× |
89
+ | Hyperion `--async-io -t 5` pool=32 | 371.2 | 919 ms | 151 MB | 1.05× |
90
+ | Hyperion `--async-io -t 5` pool=64 | 741.5 | 681 ms | 161 MB | 2.1× |
91
+ | **Hyperion `--async-io -t 5` pool=128** | **1739.9** | **512 ms** | **201 MB** | **4.9×** |
92
+ | Falcon `--count 1` pool=128 | 1642.1 | 531 ms | 213 MB | 4.7× |
93
+
94
+ **Takeaways:**
95
+ 1. **Linear scaling with pool size** under `--async-io` — `r/s ≈ pool × 12` on this WAN bench. Single-worker pool=200 hits 2381 r/s, **42× Puma `-t 5`** and **5.9× Puma's best** (`-t 30`).
96
+ 2. **Mixed workload doesn't kill the win** — Hyperion `--async-io` pool=128 actually goes *up* on mixed (1740 vs 1344 r/s) because CPU work overlaps other fibers' PG-wait windows. This is the honest "what happens to a real Rails handler" answer.
97
+ 3. **Hyperion ≈ Falcon within 3-7%** across pool sizes; both fiber-native architectures extract similar value from `hyperion-async-pg`.
98
+ 4. **RSS at single-worker scale isn't the architectural moat** — Linux thread stacks are demand-paged; PG connection buffers dominate RSS at pool sizes ≤ 200. The MB-vs-GB story shows up at **idle keep-alive connection scale** (10k+ conns), not in this PG-bound throughput bench. See [Concurrency at scale](#concurrency-at-scale-architectural-advantages) for the connection-count win.
99
+ 5. **`-w 4` cold-start caveat** — multi-worker p99 inflates because the bench rackup uses lazy per-process pool init (each worker pays full pool fill on its first request). Production apps avoid this with `on_worker_boot { Hyperion::AsyncPg::FiberPool.new(...).fill }`.
76
100
 
77
101
  Three things must all be true to get this win:
78
102
  1. **`async_io: true`** in your Hyperion config (or `--async-io` CLI flag). Default is off to keep 1.2.0's raw-loop perf for fiber-unaware apps.
79
103
  2. **`hyperion-async-pg`** installed: `gem 'hyperion-async-pg', require: 'hyperion/async_pg'` + `Hyperion::AsyncPg.install!` at boot.
80
- 3. **Fiber-aware connection pool.** The popular `connection_pool` gem is NOT — its Mutex blocks the OS thread. Use [`async-pool`](https://github.com/socketry/async-pool), `Async::Semaphore`, or hand-roll one (see `bench/pg_concurrent.ru` for a 30-line FiberPool example).
104
+ 3. **Fiber-aware connection pool.** The popular `connection_pool` gem is NOT — its Mutex blocks the OS thread. Use `Hyperion::AsyncPg::FiberPool` (ships with hyperion-async-pg 0.3.0+), [`async-pool`](https://github.com/socketry/async-pool), or `Async::Semaphore`.
81
105
 
82
- Skip any of these and you get parity with Puma at the same `-t`. Run the bench yourself: `MODE=async DATABASE_URL=... PG_POOL_SIZE=64 bundle exec hyperion --async-io -t 5 bench/pg_concurrent.ru` (in the [hyperion-async-pg](https://github.com/andrew-woblavobla/hyperion-async-pg) repo).
106
+ Skip any of these and you get parity with Puma at the same `-t`. Run the bench yourself: `MODE=async DATABASE_URL=... PG_POOL_SIZE=200 bundle exec hyperion --async-io -t 5 bench/pg_concurrent.ru` (in the [hyperion-async-pg](https://github.com/andrew-woblavobla/hyperion-async-pg) repo).
107
+
108
+ > **TLS + async-pg note (1.4.0+).** TLS / HTTPS already runs each connection on a fiber under `Async::Scheduler` (the TLS path always uses `start_async_loop` for the ALPN handshake). **As of 1.4.0, the post-handshake `app.call` for HTTP/1.1-over-TLS dispatches inline on the calling fiber by default** — so fiber-cooperative libraries (`hyperion-async-pg`, `async-redis`) work on the TLS h1 path without needing `--async-io`. The Async-loop cost is already paid for the handshake; running the handler under the existing scheduler just preserves that context instead of stripping it on a thread-pool hop. h2 streams are always fiber-dispatched and benefit from async-pg without the flag.
109
+ >
110
+ > Operators who specifically want **TLS + threadpool dispatch** (e.g. CPU-heavy handlers competing for OS threads, where you'd rather not pay fiber yields and want true OS-thread parallelism on a synchronous handler) can pass `async_io: false` in the config to force the pool branch back on. The three-way `async_io` setting:
111
+ > - `nil` (default): plain HTTP/1.1 → pool, TLS h1 → inline.
112
+ > - `true`: plain HTTP/1.1 → inline, TLS h1 → inline (force fiber dispatch everywhere; needed for `hyperion-async-pg` on plain HTTP).
113
+ > - `false`: plain HTTP/1.1 → pool, TLS h1 → pool (explicit opt-out for TLS+threadpool).
83
114
 
84
115
  ### CPU-bound JSON workload
85
116
 
@@ -240,6 +271,8 @@ log_requests true
240
271
 
241
272
  fiber_local_shim false
242
273
 
274
+ async_io nil # Three-way (1.4.0+): nil (default, auto: inline-on-fiber for TLS h1, pool hop for plain HTTP/1.1), true (force inline-on-fiber everywhere — required for hyperion-async-pg on plain HTTP/1.1), false (force pool hop everywhere — explicit opt-out for TLS+threadpool with CPU-heavy handlers). ~5% throughput hit on hello-world when inline; in exchange one OS thread serves N concurrent in-flight DB queries on wait-bound workloads. TLS / HTTP/2 accept loops always run under Async::Scheduler regardless of this flag.
275
+
243
276
  before_fork do
244
277
  ActiveRecord::Base.connection_handler.clear_all_connections! if defined?(ActiveRecord)
245
278
  end
@@ -304,6 +337,8 @@ The default-ON access log path is engineered to stay near-zero cost:
304
337
  | `parse_errors` | HTTP parse failures → 400. |
305
338
  | `app_errors` | Rack app raised → 500. |
306
339
  | `read_timeouts` | Per-connection read deadline hit. |
340
+ | `requests_threadpool_dispatched` | HTTP/1.1 connection handed to the worker pool (or served inline in `start_raw_loop` when `thread_count: 0`). The default dispatch path. |
341
+ | `requests_async_dispatched` | HTTP/1.1 connection served inline on the accept-loop fiber under `--async-io`. Operators can use the ratio against `requests_threadpool_dispatched` to verify fiber-cooperative I/O is actually engaged. |
307
342
 
308
343
  ```ruby
309
344
  require 'hyperion'
@@ -31,7 +31,7 @@ module Hyperion
31
31
  admin_token: nil, # String. When set, exposes admin endpoints (POST /-/quit triggers graceful drain; GET /-/metrics returns Prometheus-format Hyperion.stats). Same token guards both. nil disables admin entirely (paths fall through to the app).
32
32
  max_pending: nil, # Integer, e.g. 256. When the per-worker accept inbox has this many queued connections, additional accepts are rejected with HTTP 503 + Retry-After:1 instead of being queued. nil disables (current behaviour: unbounded queue).
33
33
  max_request_read_seconds: 60, # Numeric. Total wallclock budget (seconds) for reading the request line + headers + body for ONE request. Defends against slowloris-style drips that satisfy the per-recv read_timeout but never finish the request. Resets between requests on a keep-alive connection. nil disables.
34
- async_io: false, # When true, the plain HTTP/1.1 accept loop runs each connection on a fiber under Async::Scheduler instead of handing it to a worker thread. Required for fiber-cooperative I/O (e.g. hyperion-async-pg). Costs ~5% throughput on hello-world; in exchange one OS thread can serve N concurrent in-flight DB queries on wait-bound workloads. TLS / HTTP/2 paths always use the async loop and ignore this flag.
34
+ async_io: nil, # Three-way: nil (default, auto: inline on TLS h1 / pool on plain HTTP/1.1), true (force inline-on-fiber for plain HTTP/1.1 too required for fiber-cooperative I/O like hyperion-async-pg on plain HTTP), false (force pool hop everywhere — explicit opt-out for operators who specifically want TLS+threadpool with CPU-bound handlers). Costs ~5% throughput on hello-world when inline; in exchange one OS thread can serve N concurrent in-flight DB queries on wait-bound workloads. TLS / HTTP/2 paths always run the Async accept loop regardless of this flag.
35
35
  h2_max_concurrent_streams: 128, # HTTP/2 SETTINGS_MAX_CONCURRENT_STREAMS — cap on simultaneously-open streams per connection. Falcon: 64. nil leaves protocol-http2 default (0xFFFFFFFF).
36
36
  h2_initial_window_size: 1_048_576, # HTTP/2 SETTINGS_INITIAL_WINDOW_SIZE (octets) — flow-control window per stream at open. Bigger = fewer WINDOW_UPDATE round-trips on large bodies. Spec default is 65535. nil → leave protocol default.
37
37
  h2_max_frame_size: 1_048_576, # HTTP/2 SETTINGS_MAX_FRAME_SIZE (octets) — biggest DATA/HEADERS frame we'll accept. Spec floor 16384, ceiling 16777215. We pick 1 MiB to match common CDNs without unbounded buffer growth. nil → leave protocol default (16384).
@@ -42,7 +42,7 @@ module Hyperion
42
42
 
43
43
  def initialize(app:, host: '127.0.0.1', port: 9292, read_timeout: DEFAULT_READ_TIMEOUT_SECONDS,
44
44
  tls: nil, thread_count: DEFAULT_THREAD_COUNT, max_pending: nil,
45
- max_request_read_seconds: 60, h2_settings: nil, async_io: false)
45
+ max_request_read_seconds: 60, h2_settings: nil, async_io: nil)
46
46
  @host = host
47
47
  @port = port
48
48
  @app = app
@@ -111,7 +111,10 @@ module Hyperion
111
111
  if @tls || @async_io
112
112
  # TLS path: ALPN may pick `h2`, and h2 spawns one fiber per stream
113
113
  # inside Http2Handler. Keep the Async wrapper so the scheduler is
114
- # available for those fibers and for handshake yields.
114
+ # available for those fibers and for handshake yields. Plain
115
+ # HTTP/1.1-over-TLS dispatch is also handled inline on the calling
116
+ # fiber by default in 1.4.0+ (see #dispatch) — fiber-cooperative
117
+ # libraries (async-pg, async-redis) work without --async-io.
115
118
  #
116
119
  # async_io: true: operator opt-in for plain HTTP/1.1. The Async wrap
117
120
  # is required when callers want fiber cooperative I/O — e.g.
@@ -120,9 +123,10 @@ module Hyperion
120
123
  # OS thread can serve N concurrent in-flight DB queries instead of 1.
121
124
  start_async_loop
122
125
  else
123
- # Plain HTTP/1.1, async_io: false (default): the worker thread owns
124
- # each connection for its lifetime, so the Async wrapper adds zero
125
- # value (no fibers ever run on this loop's task). Skip it — pure
126
+ # Plain HTTP/1.1, async_io: nil (default with no TLS) or
127
+ # async_io: false (explicit opt-out): the worker thread owns each
128
+ # connection for its lifetime, so the Async wrapper adds zero value
129
+ # (no fibers ever run on this loop's task). Skip it — pure
126
130
  # IO.select + accept_nonblock shaves measurable overhead off the
127
131
  # accept hot path.
128
132
  start_raw_loop
@@ -151,11 +155,14 @@ module Hyperion
151
155
 
152
156
  apply_timeout(socket)
153
157
  if @thread_pool
154
- unless @thread_pool.submit_connection(socket, @app,
155
- max_request_read_seconds: @max_request_read_seconds)
158
+ if @thread_pool.submit_connection(socket, @app,
159
+ max_request_read_seconds: @max_request_read_seconds)
160
+ Hyperion.metrics.increment(:requests_threadpool_dispatched)
161
+ else
156
162
  reject_connection(socket)
157
163
  end
158
164
  else
165
+ Hyperion.metrics.increment(:requests_threadpool_dispatched)
159
166
  Connection.new.serve(socket, @app, max_request_read_seconds: @max_request_read_seconds)
160
167
  end
161
168
  end
@@ -180,31 +187,61 @@ module Hyperion
180
187
  if socket.is_a?(::OpenSSL::SSL::SSLSocket) && socket.alpn_protocol == 'h2'
181
188
  # HTTP/2: each stream runs on a fiber inside Http2Handler. The
182
189
  # handler still uses the pool's `#call` for app.call hops on each
183
- # stream (one per stream, not one per connection).
190
+ # stream (one per stream, not one per connection). Per-stream
191
+ # counters live inside Http2Handler; we don't bump either of the
192
+ # H1 dispatch buckets here — neither fits the h2 model cleanly.
184
193
  Http2Handler.new(app: @app, thread_pool: @thread_pool, h2_settings: @h2_settings).serve(socket)
185
- elsif @async_io
186
- # async_io plain HTTP/1.1: serve inline on the calling fiber so the
187
- # request runs *under* Async::Scheduler. This is what makes
188
- # hyperion-async-pg (and other Async-aware libraries) actually
189
- # cooperate each fiber yields the OS thread on socket waits, so
190
- # one thread can serve N concurrent in-flight DB queries. The
191
- # thread pool is intentionally bypassed here: handing the socket
192
- # to a worker thread strips the scheduler context.
194
+ elsif inline_h1_dispatch?
195
+ # Inline-on-fiber HTTP/1.1 dispatch. Two ways to land here:
196
+ # 1. async_io: true operator explicitly opted into fiber I/O on
197
+ # the plain HTTP/1.1 path.
198
+ # 2. async_io: nil (default) AND TLS configured TLS already
199
+ # runs the Async accept loop for ALPN handshake + h2 streams,
200
+ # so the scheduler is current on this fiber. Handing the
201
+ # socket to a worker thread would strip the scheduler context
202
+ # for no perf benefit (we paid the Async-loop cost already)
203
+ # and would defeat hyperion-async-pg / async-redis on the
204
+ # TLS h1 path.
205
+ # Operators who specifically want TLS+threadpool (e.g. CPU-heavy
206
+ # handlers competing for OS threads) can pass async_io: false to
207
+ # force the pool branch below.
208
+ Hyperion.metrics.increment(:requests_async_dispatched)
193
209
  Connection.new.serve(socket, @app, max_request_read_seconds: @max_request_read_seconds)
194
210
  elsif @thread_pool
195
- # HTTP/1.1 (e.g. TLS-wrapped after ALPN picked http/1.1): hand the
196
- # connection to a worker thread. The fiber that called dispatch
211
+ # HTTP/1.1 default plain-HTTP path, OR explicit async_io: false on
212
+ # TLS (operator opted out of inline-on-fiber dispatch). Hand the
213
+ # connection to a worker thread; the fiber that called dispatch
197
214
  # returns immediately. On overflow, reject with 503 + close.
198
- unless @thread_pool.submit_connection(socket, @app,
199
- max_request_read_seconds: @max_request_read_seconds)
215
+ if @thread_pool.submit_connection(socket, @app,
216
+ max_request_read_seconds: @max_request_read_seconds)
217
+ Hyperion.metrics.increment(:requests_threadpool_dispatched)
218
+ else
200
219
  reject_connection(socket)
201
220
  end
202
221
  else
203
- # No pool (thread_count: 0): inline on the calling fiber.
222
+ # No pool (thread_count: 0) on the TLS / async-wrap path with
223
+ # async_io: false. Rare config — neither dispatch bucket fits
224
+ # cleanly. Leave un-counted rather than misclassify; the request
225
+ # still shows up in :requests_total via Connection.
204
226
  Connection.new.serve(socket, @app, max_request_read_seconds: @max_request_read_seconds)
205
227
  end
206
228
  end
207
229
 
230
+ # Decide whether to serve HTTP/1.1 inline on the calling fiber instead
231
+ # of hopping through the worker thread pool. The matrix:
232
+ # async_io == true → inline always (plain h1 + TLS h1).
233
+ # async_io == nil + TLS → inline (TLS already runs Async loop, so
234
+ # the scheduler is current; preserve it).
235
+ # async_io == nil + plain → pool (pure HTTP/1.1 fast path; no scheduler).
236
+ # async_io == false → pool always (explicit opt-out).
237
+ def inline_h1_dispatch?
238
+ return true if @async_io == true
239
+ return false if @async_io == false
240
+
241
+ # @async_io.nil? — auto: inline on TLS, pool on plain.
242
+ !@tls.nil?
243
+ end
244
+
208
245
  # Backpressure rejection. Emits a pre-built 503 + closes the socket.
209
246
  # No Rack env, no app dispatch, no access-log line — the overload
210
247
  # path must stay cheap so we don't pile rejection cost on top of the
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hyperion
4
- VERSION = '1.3.0'
4
+ VERSION = '1.4.0'
5
5
  end
@@ -20,7 +20,7 @@ module Hyperion
20
20
  thread_count: Server::DEFAULT_THREAD_COUNT,
21
21
  config: nil, worker_index: 0, listener: nil,
22
22
  max_pending: nil, max_request_read_seconds: 60,
23
- h2_settings: nil, async_io: false)
23
+ h2_settings: nil, async_io: nil)
24
24
  @host = host
25
25
  @port = port
26
26
  @app = app
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperion-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Lobanov