raiha 0.1.0 → 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.
Files changed (347) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/LICENSE +202 -0
  4. data/README.md +94 -22
  5. data/docs/configuration.md +227 -0
  6. data/docs/development.md +99 -0
  7. data/docs/fiber-scheduler.md +591 -0
  8. data/docs/getting-started.md +190 -0
  9. data/docs/http3-client.md +303 -0
  10. data/docs/http3-server.md +301 -0
  11. data/docs/platforms.md +98 -0
  12. data/docs/qlog.md +86 -0
  13. data/docs/quic.md +268 -0
  14. data/docs/threads.md +127 -0
  15. data/examples/_certificate.rb +55 -0
  16. data/examples/high_level_loopback.rb +59 -0
  17. data/examples/http3_get_public.rb +68 -0
  18. data/examples/in_memory_handshake.rb +38 -0
  19. data/examples/qlog_output.rb +58 -0
  20. data/examples/qlog_roundtrips.rb +66 -0
  21. data/examples/quic_gem_loopback.rb +62 -0
  22. data/examples/stream_echo.rb +68 -0
  23. data/examples/udp_handshake.rb +76 -0
  24. data/lib/raiha/http3/action.rb +21 -0
  25. data/lib/raiha/http3/capsule.rb +86 -0
  26. data/lib/raiha/http3/connection.rb +983 -0
  27. data/lib/raiha/http3/error.rb +24 -0
  28. data/lib/raiha/http3/error_code.rb +27 -0
  29. data/lib/raiha/http3/event.rb +63 -0
  30. data/lib/raiha/http3/frame.rb +148 -0
  31. data/lib/raiha/http3/frame_reader.rb +114 -0
  32. data/lib/raiha/http3/message.rb +137 -0
  33. data/lib/raiha/http3/output.rb +61 -0
  34. data/lib/raiha/http3/priority.rb +52 -0
  35. data/lib/raiha/http3/qlog.rb +145 -0
  36. data/lib/raiha/http3/qpack/decoder.rb +302 -0
  37. data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
  38. data/lib/raiha/http3/qpack/encoder.rb +94 -0
  39. data/lib/raiha/http3/qpack/error.rb +39 -0
  40. data/lib/raiha/http3/qpack/error_code.rb +13 -0
  41. data/lib/raiha/http3/qpack/field.rb +15 -0
  42. data/lib/raiha/http3/qpack/huffman.rb +397 -0
  43. data/lib/raiha/http3/qpack/reader.rb +82 -0
  44. data/lib/raiha/http3/qpack/result.rb +26 -0
  45. data/lib/raiha/http3/qpack/static_table.rb +139 -0
  46. data/lib/raiha/http3/qpack/writer.rb +44 -0
  47. data/lib/raiha/http3/qpack.rb +26 -0
  48. data/lib/raiha/http3/receiving.rb +46 -0
  49. data/lib/raiha/http3/sending.rb +23 -0
  50. data/lib/raiha/http3/session.rb +31 -0
  51. data/lib/raiha/http3/settings.rb +81 -0
  52. data/lib/raiha/http3/structured_field.rb +120 -0
  53. data/lib/raiha/http3/tracer.rb +26 -0
  54. data/lib/raiha/http3/varint.rb +45 -0
  55. data/lib/raiha/http3.rb +19 -0
  56. data/lib/raiha/http3_binding.rb +139 -0
  57. data/lib/raiha/namespace.rb +11 -0
  58. data/lib/raiha/quic/config.rb +229 -0
  59. data/lib/raiha/quic/congestion/algorithms.rb +17 -0
  60. data/lib/raiha/quic/congestion/cubic.rb +258 -0
  61. data/lib/raiha/quic/congestion/new_reno.rb +161 -0
  62. data/lib/raiha/quic/congestion/pacing.rb +21 -0
  63. data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
  64. data/lib/raiha/quic/congestion/startup.rb +13 -0
  65. data/lib/raiha/quic/congestion.rb +15 -0
  66. data/lib/raiha/quic/connection.rb +2022 -0
  67. data/lib/raiha/quic/connection_ids.rb +62 -0
  68. data/lib/raiha/quic/crypto/aead.rb +68 -0
  69. data/lib/raiha/quic/crypto/header_protection.rb +42 -0
  70. data/lib/raiha/quic/crypto/initial.rb +23 -0
  71. data/lib/raiha/quic/crypto/keys.rb +32 -0
  72. data/lib/raiha/quic/crypto/mac.rb +15 -0
  73. data/lib/raiha/quic/crypto/random.rb +13 -0
  74. data/lib/raiha/quic/crypto.rb +18 -0
  75. data/lib/raiha/quic/crypto_stream.rb +161 -0
  76. data/lib/raiha/quic/datagrams.rb +91 -0
  77. data/lib/raiha/quic/endpoint.rb +785 -0
  78. data/lib/raiha/quic/error.rb +61 -0
  79. data/lib/raiha/quic/error_code.rb +34 -0
  80. data/lib/raiha/quic/event.rb +59 -0
  81. data/lib/raiha/quic/issued_connection_ids.rb +137 -0
  82. data/lib/raiha/quic/kept_packets.rb +41 -0
  83. data/lib/raiha/quic/mtu_search.rb +75 -0
  84. data/lib/raiha/quic/negotiation.rb +143 -0
  85. data/lib/raiha/quic/new_token.rb +55 -0
  86. data/lib/raiha/quic/pacer.rb +52 -0
  87. data/lib/raiha/quic/packet_protection.rb +310 -0
  88. data/lib/raiha/quic/path.rb +473 -0
  89. data/lib/raiha/quic/peer_connection_ids.rb +160 -0
  90. data/lib/raiha/quic/qlog.rb +260 -0
  91. data/lib/raiha/quic/ranges.rb +32 -0
  92. data/lib/raiha/quic/recovery/duty.rb +102 -0
  93. data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
  94. data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
  95. data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
  96. data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
  97. data/lib/raiha/quic/recovery.rb +641 -0
  98. data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
  99. data/lib/raiha/quic/retry_token.rb +54 -0
  100. data/lib/raiha/quic/route.rb +37 -0
  101. data/lib/raiha/quic/scheduler.rb +112 -0
  102. data/lib/raiha/quic/session.rb +47 -0
  103. data/lib/raiha/quic/stateless_reset.rb +38 -0
  104. data/lib/raiha/quic/stats.rb +24 -0
  105. data/lib/raiha/quic/streams/receive_window.rb +119 -0
  106. data/lib/raiha/quic/streams/receiver.rb +217 -0
  107. data/lib/raiha/quic/streams/sender.rb +200 -0
  108. data/lib/raiha/quic/streams.rb +687 -0
  109. data/lib/raiha/quic/test/bottleneck.rb +55 -0
  110. data/lib/raiha/quic/test/congestion_contract.rb +106 -0
  111. data/lib/raiha/quic/test/network.rb +244 -0
  112. data/lib/raiha/quic/test/random_loss.rb +34 -0
  113. data/lib/raiha/quic/test/reorder.rb +51 -0
  114. data/lib/raiha/quic/test/server.rb +54 -0
  115. data/lib/raiha/quic/test/vectors.rb +337 -0
  116. data/lib/raiha/quic/test/virtual_clock.rb +21 -0
  117. data/lib/raiha/quic/test.rb +17 -0
  118. data/lib/raiha/quic/tls_binding.rb +179 -0
  119. data/lib/raiha/quic/token.rb +28 -0
  120. data/lib/raiha/quic/tracer.rb +127 -0
  121. data/lib/raiha/quic/transmit.rb +14 -0
  122. data/lib/raiha/quic/transport_parameters.rb +244 -0
  123. data/lib/raiha/quic/version.rb +63 -0
  124. data/lib/raiha/quic/wire/codec.rb +136 -0
  125. data/lib/raiha/quic/wire/frame.rb +521 -0
  126. data/lib/raiha/quic/wire/header.rb +90 -0
  127. data/lib/raiha/quic/wire/packet.rb +130 -0
  128. data/lib/raiha/quic/wire/packet_number.rb +47 -0
  129. data/lib/raiha/quic/wire/retry.rb +62 -0
  130. data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
  131. data/lib/raiha/quic/wire.rb +9 -0
  132. data/lib/raiha/quic.rb +46 -0
  133. data/lib/raiha/runtime/addresses.rb +35 -0
  134. data/lib/raiha/runtime/admission.rb +37 -0
  135. data/lib/raiha/runtime/client.rb +190 -0
  136. data/lib/raiha/runtime/driver.rb +165 -0
  137. data/lib/raiha/runtime/error.rb +16 -0
  138. data/lib/raiha/runtime/http3.rb +363 -0
  139. data/lib/raiha/runtime/http3_fields.rb +38 -0
  140. data/lib/raiha/runtime/http3_server.rb +416 -0
  141. data/lib/raiha/runtime/kept.rb +36 -0
  142. data/lib/raiha/runtime/qlog.rb +204 -0
  143. data/lib/raiha/runtime/rack.rb +180 -0
  144. data/lib/raiha/runtime/server.rb +334 -0
  145. data/lib/raiha/runtime/session.rb +447 -0
  146. data/lib/raiha/runtime/threaded/client.rb +161 -0
  147. data/lib/raiha/runtime/threaded/driver.rb +230 -0
  148. data/lib/raiha/runtime/threaded/http3.rb +287 -0
  149. data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
  150. data/lib/raiha/runtime/threaded/server.rb +290 -0
  151. data/lib/raiha/runtime/threaded/session.rb +604 -0
  152. data/lib/raiha/runtime/threaded.rb +45 -0
  153. data/lib/raiha/runtime/udp.rb +410 -0
  154. data/lib/raiha/runtime/worker.rb +138 -0
  155. data/lib/raiha/runtime/workers.rb +248 -0
  156. data/lib/raiha/runtime.rb +37 -0
  157. data/lib/raiha/tls/anti_replay.rb +139 -0
  158. data/lib/raiha/tls/cipher_suite.rb +62 -0
  159. data/lib/raiha/tls/codec/error.rb +43 -0
  160. data/lib/raiha/tls/codec/reader.rb +95 -0
  161. data/lib/raiha/tls/codec/writer.rb +81 -0
  162. data/lib/raiha/tls/codec.rb +5 -0
  163. data/lib/raiha/tls/config.rb +318 -0
  164. data/lib/raiha/tls/crypto/aead.rb +64 -0
  165. data/lib/raiha/tls/crypto/certificate.rb +28 -0
  166. data/lib/raiha/tls/crypto/digest.rb +30 -0
  167. data/lib/raiha/tls/crypto/ecdh.rb +74 -0
  168. data/lib/raiha/tls/crypto/hash.rb +22 -0
  169. data/lib/raiha/tls/crypto/hkdf.rb +28 -0
  170. data/lib/raiha/tls/crypto/signature.rb +87 -0
  171. data/lib/raiha/tls/crypto.rb +19 -0
  172. data/lib/raiha/tls/error.rb +36 -0
  173. data/lib/raiha/tls/event.rb +45 -0
  174. data/lib/raiha/tls/extension/alpn.rb +35 -0
  175. data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
  176. data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
  177. data/lib/raiha/tls/extension/cookie.rb +27 -0
  178. data/lib/raiha/tls/extension/early_data.rb +54 -0
  179. data/lib/raiha/tls/extension/key_share.rb +90 -0
  180. data/lib/raiha/tls/extension/list.rb +49 -0
  181. data/lib/raiha/tls/extension/opaque.rb +21 -0
  182. data/lib/raiha/tls/extension/placement.rb +55 -0
  183. data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
  184. data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
  185. data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
  186. data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
  187. data/lib/raiha/tls/extension/registry.rb +50 -0
  188. data/lib/raiha/tls/extension/server_name.rb +63 -0
  189. data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
  190. data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
  191. data/lib/raiha/tls/extension/supported_groups.rb +32 -0
  192. data/lib/raiha/tls/extension/supported_versions.rb +53 -0
  193. data/lib/raiha/tls/extension.rb +32 -0
  194. data/lib/raiha/tls/handshake/acceptor.rb +164 -0
  195. data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
  196. data/lib/raiha/tls/handshake/certificate.rb +56 -0
  197. data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
  198. data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
  199. data/lib/raiha/tls/handshake/client_hello.rb +208 -0
  200. data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
  201. data/lib/raiha/tls/handshake/client_side.rb +578 -0
  202. data/lib/raiha/tls/handshake/conductor.rb +390 -0
  203. data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
  204. data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
  205. data/lib/raiha/tls/handshake/finished.rb +36 -0
  206. data/lib/raiha/tls/handshake/key_update.rb +38 -0
  207. data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
  208. data/lib/raiha/tls/handshake/machine.rb +238 -0
  209. data/lib/raiha/tls/handshake/message.rb +25 -0
  210. data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
  211. data/lib/raiha/tls/handshake/reassembler.rb +88 -0
  212. data/lib/raiha/tls/handshake/server_hello.rb +98 -0
  213. data/lib/raiha/tls/handshake/server_side.rb +672 -0
  214. data/lib/raiha/tls/handshake/violation.rb +12 -0
  215. data/lib/raiha/tls/handshake.rb +62 -0
  216. data/lib/raiha/tls/iana.rb +113 -0
  217. data/lib/raiha/tls/key_exchange.rb +58 -0
  218. data/lib/raiha/tls/key_schedule.rb +269 -0
  219. data/lib/raiha/tls/keylog.rb +40 -0
  220. data/lib/raiha/tls/keyring.rb +180 -0
  221. data/lib/raiha/tls/masked_inspect.rb +33 -0
  222. data/lib/raiha/tls/output.rb +32 -0
  223. data/lib/raiha/tls/profile.rb +82 -0
  224. data/lib/raiha/tls/record/layer.rb +228 -0
  225. data/lib/raiha/tls/record/protection.rb +67 -0
  226. data/lib/raiha/tls/record.rb +11 -0
  227. data/lib/raiha/tls/server_name.rb +42 -0
  228. data/lib/raiha/tls/session.rb +106 -0
  229. data/lib/raiha/tls/signature_scheme.rb +35 -0
  230. data/lib/raiha/tls/tcp/acceptor.rb +121 -0
  231. data/lib/raiha/tls/tcp/connection.rb +371 -0
  232. data/lib/raiha/tls/tcp/event.rb +15 -0
  233. data/lib/raiha/tls/tcp/socket.rb +161 -0
  234. data/lib/raiha/tls/tcp.rb +14 -0
  235. data/lib/raiha/tls/test/fixed_kem.rb +30 -0
  236. data/lib/raiha/tls/test/fixed_random.rb +18 -0
  237. data/lib/raiha/tls/test/rfc8448.rb +923 -0
  238. data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
  239. data/lib/raiha/tls/test.rb +13 -0
  240. data/lib/raiha/tls/ticket.rb +184 -0
  241. data/lib/raiha/tls/ticket_keys.rb +206 -0
  242. data/lib/raiha/tls/trace.rb +58 -0
  243. data/lib/raiha/tls/transcript.rb +94 -0
  244. data/lib/raiha/tls/verifier/default.rb +72 -0
  245. data/lib/raiha/tls/verifier/host_name.rb +75 -0
  246. data/lib/raiha/tls/verifier/none.rb +14 -0
  247. data/lib/raiha/tls/verifier.rb +24 -0
  248. data/lib/raiha/tls.rb +34 -0
  249. data/lib/raiha/version.rb +3 -1
  250. data/lib/raiha.rb +8 -5
  251. data/sig/patches/array.rbs +9 -0
  252. data/sig/patches/io_buffer.rbs +13 -0
  253. data/sig/patches/openssl.rbs +25 -0
  254. data/sig/patches/socket.rbs +29 -0
  255. data/sig/patches/string.rbs +26 -0
  256. data/sig/patches/strscan.rbs +12 -0
  257. data/sig/raiha/http3/capsule.rbs +32 -0
  258. data/sig/raiha/http3/connection.rbs +324 -0
  259. data/sig/raiha/http3/frame.rbs +138 -0
  260. data/sig/raiha/http3/frame_reader.rbs +39 -0
  261. data/sig/raiha/http3/message.rbs +23 -0
  262. data/sig/raiha/http3/priority.rbs +40 -0
  263. data/sig/raiha/http3/qlog.rbs +54 -0
  264. data/sig/raiha/http3/qpack.rbs +246 -0
  265. data/sig/raiha/http3/session.rbs +11 -0
  266. data/sig/raiha/http3/settings.rbs +26 -0
  267. data/sig/raiha/http3.rbs +2 -0
  268. data/sig/raiha/http3_binding.rbs +26 -0
  269. data/sig/raiha/namespace.rbs +2 -0
  270. data/sig/raiha/quic/config.rbs +89 -0
  271. data/sig/raiha/quic/congestion.rbs +213 -0
  272. data/sig/raiha/quic/connection.rbs +249 -0
  273. data/sig/raiha/quic/connection_ids.rbs +34 -0
  274. data/sig/raiha/quic/crypto.rbs +78 -0
  275. data/sig/raiha/quic/crypto_stream.rbs +34 -0
  276. data/sig/raiha/quic/datagrams.rbs +32 -0
  277. data/sig/raiha/quic/endpoint.rbs +179 -0
  278. data/sig/raiha/quic/frame.rbs +387 -0
  279. data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
  280. data/sig/raiha/quic/kept_packets.rbs +25 -0
  281. data/sig/raiha/quic/mtu_search.rbs +21 -0
  282. data/sig/raiha/quic/negotiation.rbs +33 -0
  283. data/sig/raiha/quic/new_token.rbs +14 -0
  284. data/sig/raiha/quic/pacer.rbs +16 -0
  285. data/sig/raiha/quic/packet_protection.rbs +63 -0
  286. data/sig/raiha/quic/path.rbs +106 -0
  287. data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
  288. data/sig/raiha/quic/qlog.rbs +74 -0
  289. data/sig/raiha/quic/ranges.rbs +7 -0
  290. data/sig/raiha/quic/recovery.rbs +376 -0
  291. data/sig/raiha/quic/retry_token.rbs +21 -0
  292. data/sig/raiha/quic/route.rbs +51 -0
  293. data/sig/raiha/quic/scheduler.rbs +36 -0
  294. data/sig/raiha/quic/session.rbs +17 -0
  295. data/sig/raiha/quic/stateless_reset.rbs +10 -0
  296. data/sig/raiha/quic/streams.rbs +244 -0
  297. data/sig/raiha/quic/test.rbs +354 -0
  298. data/sig/raiha/quic/tls_binding.rbs +58 -0
  299. data/sig/raiha/quic/token.rbs +11 -0
  300. data/sig/raiha/quic/tracer.rbs +71 -0
  301. data/sig/raiha/quic/transport_parameters.rbs +113 -0
  302. data/sig/raiha/quic/values.rbs +200 -0
  303. data/sig/raiha/quic/version.rbs +30 -0
  304. data/sig/raiha/quic/wire.rbs +199 -0
  305. data/sig/raiha/quic.rbs +2 -0
  306. data/sig/raiha/runtime/threaded.rbs +616 -0
  307. data/sig/raiha/runtime.rbs +982 -0
  308. data/sig/raiha/tls/anti_replay.rbs +41 -0
  309. data/sig/raiha/tls/cipher_suite.rbs +41 -0
  310. data/sig/raiha/tls/codec.rbs +66 -0
  311. data/sig/raiha/tls/config.rbs +122 -0
  312. data/sig/raiha/tls/crypto.rbs +86 -0
  313. data/sig/raiha/tls/error.rbs +21 -0
  314. data/sig/raiha/tls/event.rbs +61 -0
  315. data/sig/raiha/tls/extension.rbs +331 -0
  316. data/sig/raiha/tls/handshake.rbs +311 -0
  317. data/sig/raiha/tls/iana.rbs +13 -0
  318. data/sig/raiha/tls/key_exchange.rbs +33 -0
  319. data/sig/raiha/tls/key_schedule.rbs +58 -0
  320. data/sig/raiha/tls/keylog.rbs +20 -0
  321. data/sig/raiha/tls/keyring.rbs +64 -0
  322. data/sig/raiha/tls/machine.rbs +429 -0
  323. data/sig/raiha/tls/masked_inspect.rbs +12 -0
  324. data/sig/raiha/tls/output.rbs +37 -0
  325. data/sig/raiha/tls/profile.rbs +26 -0
  326. data/sig/raiha/tls/record.rbs +79 -0
  327. data/sig/raiha/tls/server_name.rbs +10 -0
  328. data/sig/raiha/tls/session.rbs +48 -0
  329. data/sig/raiha/tls/signature_scheme.rbs +20 -0
  330. data/sig/raiha/tls/tcp.rbs +134 -0
  331. data/sig/raiha/tls/test.rbs +143 -0
  332. data/sig/raiha/tls/ticket.rbs +68 -0
  333. data/sig/raiha/tls/ticket_keys.rbs +67 -0
  334. data/sig/raiha/tls/trace.rbs +99 -0
  335. data/sig/raiha/tls/transcript.rbs +22 -0
  336. data/sig/raiha/tls/verifier.rbs +54 -0
  337. data/sig/raiha/tls.rbs +5 -0
  338. data/sig/raiha.rbs +3 -0
  339. metadata +355 -57
  340. data/.gitignore +0 -10
  341. data/CODE_OF_CONDUCT.md +0 -74
  342. data/Gemfile +0 -4
  343. data/Gemfile.lock +0 -22
  344. data/Rakefile +0 -10
  345. data/bin/console +0 -14
  346. data/bin/setup +0 -8
  347. data/raiha.gemspec +0 -40
@@ -0,0 +1,416 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "http3"
4
+
5
+ module Raiha::Runtime
6
+ module HTTP3
7
+ # The body of a response as the block of Request#respond writes it,
8
+ # each write waiting while the stream holds back, and its trailer
9
+ # fields. It ends when the block is over, or before at `close`.
10
+ class ResponseBody
11
+ attr_reader :trailers
12
+
13
+ def initialize(server, stream_id, exchange)
14
+ @server = server
15
+ @stream_id = stream_id
16
+ @exchange = exchange
17
+ @trailers = [] #: Array[[String, String]]
18
+ @closed = false
19
+ end
20
+
21
+ def write(data)
22
+ closed!
23
+ @server.write_body(@stream_id, @exchange, data.b)
24
+ data.bytesize
25
+ end
26
+
27
+ def trailers=(fields)
28
+ closed!
29
+ @trailers = fields
30
+ end
31
+
32
+ # Ends the body, with the trailer fields given so far: the response is
33
+ # whole though the block goes on.
34
+ def close
35
+ return if @closed
36
+ @closed = true
37
+ @server.finish_body(@stream_id, @exchange, @trailers)
38
+ end
39
+
40
+ def closed? = @closed
41
+
42
+ private def closed!
43
+ raise Error::Usage, "the body of the response on stream #{@stream_id} has ended" if @closed
44
+ end
45
+
46
+ def <<(data)
47
+ write(data)
48
+ self
49
+ end
50
+ end
51
+
52
+ # A request to the server of HTTP/3, given to the block of Server#run in
53
+ # a fiber of its own: its pseudo-header fields, its header fields
54
+ # without them, its body as it comes, and its trailer fields once the
55
+ # body has been read. The block answers it with `respond`.
56
+ class Request
57
+ attr_reader :stream_id, :method, :path, :authority, :scheme, :protocol, :headers, :body
58
+
59
+ def initialize(server, stream_id, exchange, headers)
60
+ @server = server
61
+ @stream_id = stream_id
62
+ @exchange = exchange
63
+ pseudo, @headers = Fields.split(headers)
64
+ @method = pseudo.fetch(":method", "")
65
+ @path = pseudo.fetch(":path", "")
66
+ @authority = pseudo[":authority"]
67
+ @scheme = pseudo[":scheme"]
68
+ @protocol = pseudo[":protocol"]
69
+ @body = Body.new(server, stream_id)
70
+ @responded = false
71
+ @marked_early = @headers.any? { |name, _| name == "early-data" }
72
+ end
73
+
74
+ # The trailer fields, once the body has been read to its end.
75
+ def trailers = @exchange.trailers
76
+
77
+ # Whether the request may be a replay of one on another connection: it
78
+ # came in 0-RTT, or with an Early-Data header field, which a proxy
79
+ # that forwarded it from early data adds, and which counts whatever
80
+ # its value and however many there are (RFC 8470 "The Early-Data
81
+ # Header Field"). Waiting for the handshake to complete makes 0-RTT on
82
+ # this connection no replay, but not a request a proxy marked.
83
+ # Answering 425 (Too Early) is the application's. A part of the
84
+ # request that comes in a 0-RTT packet late, after the header section
85
+ # came in 1-RTT, makes it true from then on.
86
+ def early_data? = @marked_early || @server.stream_early_data?(@stream_id)
87
+
88
+ def responded? = @responded
89
+
90
+ # Answers with `status`, `headers` and the whole of `body`, written as
91
+ # the stream takes it; with a block, the block writes the body to the
92
+ # ResponseBody it is given, and its trailer fields.
93
+ def respond(status, headers = [], body = nil, &)
94
+ # An interim response (1xx) has no way out yet.
95
+ raise ArgumentError, "a response's status is 200 to 599, not #{status.inspect}" unless status.is_a?(Integer) && status.between?(200, 599)
96
+ raise Error::Usage, "the request on stream #{@stream_id} has its response already" if @responded
97
+ @responded = true
98
+ return @server.respond_with(@stream_id, @exchange, status, headers, &) if block_given?
99
+ @server.respond(@stream_id, @exchange, status, headers, body)
100
+ end
101
+
102
+ # The priority of the response, `urgency:` and `incremental:`: those
103
+ # given override what the request asked (RFC 9218).
104
+ def priority=(parameters)
105
+ @server.prioritize(@stream_id, urgency: parameters[:urgency], incremental: parameters[:incremental])
106
+ end
107
+ end
108
+
109
+ # The server of HTTP/3 of the runtime: a Runtime::Server whose
110
+ # sessions each have HTTP/3 attached, and whose block is given each
111
+ # request in a fiber of its own. A block that leaves a request with no
112
+ # response has it answered with 500, or with 501 for an Extended
113
+ # CONNECT, whose protocol it then does not take (RFC 9220). A block that
114
+ # raises has its request answered with 500 before the response, and
115
+ # reset with H3_INTERNAL_ERROR in the middle of it, and the exception
116
+ # goes on to the scheduler; the other requests and the connection go
117
+ # on.
118
+ #
119
+ # `stop` stops it as RFC 9114 "Connection Shutdown" has it: each
120
+ # connection is sent a GOAWAY of the largest ID, then, twice the RTT
121
+ # later, one of the first request it has not seen, from which HTTP/3
122
+ # rejects requests with H3_REQUEST_REJECTED; the requests taken are let
123
+ # finish, what was sent is let be acknowledged, and the connection
124
+ # closes with H3_NO_ERROR. A connection out of time has the requests it
125
+ # had not yet given to the block rejected, and those it had cancelled
126
+ # with H3_REQUEST_CANCELLED, their fibers interrupted where they wait,
127
+ # before it closes.
128
+ #
129
+ # Extended CONNECT (RFC 9220) is not offered unless `settings` offers
130
+ # it: the runtime has no way yet to read or write the capsules of such a
131
+ # stream, and a body of capsules would never read as data.
132
+ #
133
+ # 0-RTT is taken only from a ticket issued with the same `settings`
134
+ # (RFC 9114 "Initialization"), and recorded in `anti_replay` as
135
+ # Runtime::Server has it. Given a `worker`, it is one of the workers of
136
+ # Workers as Runtime::Server is, and stops with H3_NO_ERROR when they
137
+ # are told to.
138
+ class Server
139
+ def initialize(config:, tls_config:, keyring:, host: nil, port: nil, max_connections: Raiha::Runtime::Server::MAX_CONNECTIONS,
140
+ max_unvalidated: max_connections / 2, qlog_dir: nil, settings: Raiha::HTTP3::Settings.new, anti_replay: nil, worker: nil)
141
+ @settings = settings
142
+ @server = Raiha::Runtime::Server.new(config:, tls_config:, keyring:, host:, port:, max_connections:, max_unvalidated:, qlog_dir:, anti_replay:,
143
+ app_early_data_context: settings.early_data_context, worker:, stop_error_code: Raiha::HTTP3::ErrorCode::NO_ERROR)
144
+ end
145
+
146
+ def run(&handler)
147
+ @server.run { |session| Serving.new(session, @settings).serve(&handler) }
148
+ end
149
+
150
+ def local_address = @server.local_address
151
+
152
+ def stop(timeout: Raiha::Runtime::Server::STOP_TIMEOUT)
153
+ @server.stop(timeout:, error_code: Raiha::HTTP3::ErrorCode::NO_ERROR)
154
+ end
155
+
156
+ def reconfigure(...) = @server.reconfigure(...)
157
+ end
158
+
159
+ # The HTTP/3 of one connection of a Server: the requests as they come,
160
+ # each to a fiber of its own.
161
+ class Serving
162
+ # The largest ID a server's GOAWAY gives, which only tells the client
163
+ # to make no more requests.
164
+ LAST_REQUEST = (1 << 62) - 4
165
+
166
+ def initialize(session, settings)
167
+ @session = session
168
+ tracer = (quic = session.tracer) && Raiha::HTTP3::Qlog::Tracer.new(quic)
169
+ @http3 = Raiha::HTTP3::Connection.server(settings:, tracer:)
170
+ @binding = Raiha::HTTP3Binding.new(session.connection, @http3)
171
+ @exchanges = {} #: Hash[Integer, Exchange]
172
+ # The requests whose header sections have come, not yet given to a
173
+ # fiber, with their header fields.
174
+ @arrived = [] #: Array[[Integer, Array[[String, String]]]]
175
+ # The fibers of the requests given to the block and not over; nil
176
+ # while one starts.
177
+ @fibers = {} #: Hash[Integer, Fiber?]
178
+ session.attach(self, close_code: Raiha::HTTP3::ErrorCode::NO_ERROR)
179
+ end
180
+
181
+ # Gives each request to the block in a fiber of its own until the
182
+ # connection closes, or until the server stops and the requests taken
183
+ # are over.
184
+ def serve(&handler)
185
+ loop do
186
+ arrived = @session.wait_for { @arrived.shift || (:stopping if @session.stopping?) }
187
+ return go_away(handler) if arrived == :stopping
188
+ start(handler, arrived)
189
+ end
190
+ rescue Raiha::QUIC::ClosedError
191
+ nil
192
+ end
193
+
194
+ # The Session's protocol: the server is out of time to stop.
195
+ def expire
196
+ @arrived.each { |stream_id, _| @http3.cancel(stream_id, error_code: Raiha::HTTP3::ErrorCode::REQUEST_REJECTED) }
197
+ @arrived.clear
198
+ scheduler = Fiber.scheduler
199
+ @fibers.each do |stream_id, fiber|
200
+ @http3.cancel(stream_id, error_code: Raiha::HTTP3::ErrorCode::REQUEST_CANCELLED)
201
+ @exchanges[stream_id]&.reset = Raiha::HTTP3::ErrorCode::REQUEST_CANCELLED
202
+ next unless fiber&.alive? && scheduler.respond_to?(:fiber_interrupt)
203
+ scheduler.fiber_interrupt(fiber, StreamReset.new(stream_id, Raiha::HTTP3::ErrorCode::REQUEST_CANCELLED))
204
+ end
205
+ @binding.flush(now: Driver.clock)
206
+ end
207
+
208
+ # The next chunk of the request on `stream_id` once there is one, nil
209
+ # at its end; for Body.
210
+ def read_chunk(stream_id)
211
+ exchange = @exchanges[stream_id] or return nil
212
+ chunk = @session.wait_for(drain: true) do
213
+ reset = exchange.reset and raise StreamReset.new(stream_id, reset)
214
+ exchange.shift || (:end if exchange.ended?)
215
+ end
216
+ resume(stream_id, exchange) if exchange.paused && !exchange.full?
217
+ chunk if chunk.is_a?(String)
218
+ end
219
+
220
+ # Sends the response on `stream_id`; for Request.
221
+ def respond(stream_id, exchange, status, headers, body)
222
+ send_header_section(stream_id, exchange, [[":status", status.to_s], *headers], fin: body.nil? || body.empty?)
223
+ write(stream_id, exchange, body, fin: true) if body && !body.empty?
224
+ exchange.answered = true
225
+ end
226
+
227
+ # Sends the response on `stream_id`, its body written by the block;
228
+ # for Request.
229
+ def respond_with(stream_id, exchange, status, headers)
230
+ send_header_section(stream_id, exchange, [[":status", status.to_s], *headers], fin: false)
231
+ body = ResponseBody.new(self, stream_id, exchange)
232
+ yield body
233
+ body.close
234
+ end
235
+
236
+ # Ends the body of the response on `stream_id`, with `trailers` when
237
+ # there are any; for ResponseBody.
238
+ def finish_body(stream_id, exchange, trailers)
239
+ if trailers.empty?
240
+ writable(stream_id, exchange) { @http3.send_data(stream_id, "".b, fin: true) && true }
241
+ flush
242
+ else
243
+ send_header_section(stream_id, exchange, trailers, fin: true)
244
+ end
245
+ exchange.answered = true
246
+ end
247
+
248
+ # Writes `data` to the body of the response on `stream_id`; for
249
+ # ResponseBody.
250
+ def write_body(stream_id, exchange, data)
251
+ write(stream_id, exchange, data, fin: false)
252
+ end
253
+
254
+ # Whether a part of `stream_id` came in a 0-RTT packet; for Request.
255
+ def stream_early_data?(stream_id) = @session.connection.stream_early_data?(stream_id)
256
+
257
+ # For Request#priority=.
258
+ def prioritize(stream_id, urgency:, incremental:)
259
+ @http3.update_priority(stream_id, urgency:, incremental:)
260
+ flush
261
+ end
262
+
263
+ # The Session's protocol: the events of QUIC that are HTTP/3's.
264
+ def take(event, now) = @binding.receive(event, now:)
265
+
266
+ # The Session's protocol. The events of a turn are all looked at before
267
+ # what they ask is done, so a stream reset or stopped in it is written
268
+ # to no more.
269
+ def turn(now)
270
+ @binding.flush(now:)
271
+ events = [] #: Array[Raiha::HTTP3::Event::t]
272
+ @http3.each_event { |event| events << event }
273
+ events.each { |event| take_http3(event) }
274
+ @binding.flush(now:)
275
+ end
276
+
277
+ private def start(handler, arrived)
278
+ stream_id, headers = arrived
279
+ exchange = @exchanges.fetch(stream_id)
280
+ # The fiber runs at once, and a request with nothing to wait for is
281
+ # over before Fiber.schedule returns: it is known as there before it
282
+ # starts, and not kept once over.
283
+ @fibers[stream_id] = nil
284
+ fiber = Fiber.schedule { handle(handler, Request.new(self, stream_id, exchange, headers)) }
285
+ @fibers[stream_id] = fiber if @fibers.key?(stream_id)
286
+ end
287
+
288
+ # The server is stopping: GOAWAY twice, with the requests that come in
289
+ # between taken, then the requests taken let finish and what was sent
290
+ # let be acknowledged, until the server is out of time. A connection
291
+ # whose control stream could not be opened has nothing to be told.
292
+ private def go_away(handler)
293
+ begin
294
+ @http3.goaway(LAST_REQUEST)
295
+ rescue Raiha::HTTP3::Error::Usage
296
+ return
297
+ end
298
+ flush
299
+ between = Driver.clock + 2 * @session.connection.stats.rtt
300
+ while (arrived = @session.wait_for(deadline: between) { @arrived.shift })
301
+ start(handler, arrived)
302
+ end
303
+ @http3.goaway
304
+ flush
305
+ while (arrived = @session.wait_for { @arrived.shift || (:over if @fibers.empty?) }) != :over
306
+ start(handler, arrived)
307
+ end
308
+ deadline = @session.stop_deadline or return
309
+ @session.wait_for(deadline:) { @session.connection.stats.send_buffer_bytes.zero? || nil }
310
+ end
311
+
312
+ private def handle(handler, request)
313
+ handler.call(request)
314
+ request.respond(request.protocol ? 501 : 500) unless request.responded?
315
+ rescue Raiha::QUIC::ClosedError, StreamReset
316
+ nil
317
+ rescue StandardError
318
+ failed(request)
319
+ raise
320
+ ensure
321
+ forget(request)
322
+ end
323
+
324
+ # The block raised: what can be told of it, told. A response gone whole
325
+ # is not cut short for it.
326
+ private def failed(request)
327
+ return request.respond(500) unless request.responded?
328
+ return if @exchanges[request.stream_id]&.answered
329
+ @http3.cancel(request.stream_id, error_code: Raiha::HTTP3::ErrorCode::INTERNAL_ERROR)
330
+ flush
331
+ rescue Raiha::QUIC::ClosedError, StreamReset
332
+ nil
333
+ end
334
+
335
+ private def send_header_section(stream_id, exchange, fields, fin:)
336
+ writable(stream_id, exchange) do
337
+ @http3.send_headers(stream_id, fields, fin:)
338
+ true
339
+ end
340
+ flush
341
+ end
342
+
343
+ # Writes all of `data`, as the stream takes it.
344
+ private def write(stream_id, exchange, data, fin:)
345
+ written = 0
346
+ until written == data.bytesize
347
+ written += writable(stream_id, exchange) do
348
+ taken = @http3.send_data(stream_id, data.byteslice(written..) || "".b, fin:)
349
+ # Nothing taken, nothing to send: waking the driver would wake
350
+ # this fiber again at once.
351
+ next nil unless taken.positive?
352
+ flush
353
+ taken
354
+ end
355
+ end
356
+ end
357
+
358
+ # Calls the block until it gives something, waiting for room, unless
359
+ # the client has reset the request or stopped the response. steep
360
+ # picks no overload of `wait` for a block that gives T?.
361
+ private def writable(stream_id, exchange) # steep:ignore
362
+ @session.wait_for do
363
+ stopped = exchange.stopped and raise StreamReset.new(stream_id, stopped)
364
+ reset = exchange.reset and raise StreamReset.new(stream_id, reset)
365
+ yield
366
+ end
367
+ end
368
+
369
+ # The request is over: what of its body has not come is not wanted.
370
+ private def forget(request)
371
+ stream_id = request.stream_id
372
+ @exchanges.delete(stream_id)
373
+ @fibers.delete(stream_id)
374
+ @http3.stop_reading(stream_id)
375
+ @binding.resume(stream_id)
376
+ # A stop waits for the requests to be over.
377
+ @session.changed
378
+ end
379
+
380
+ private def resume(stream_id, exchange)
381
+ exchange.paused = false
382
+ @binding.resume(stream_id)
383
+ flush
384
+ end
385
+
386
+ private def flush
387
+ @binding.flush(now: Driver.clock)
388
+ @session.changed
389
+ end
390
+
391
+ private def take_http3(event)
392
+ case event
393
+ when Raiha::HTTP3::Event::Headers
394
+ return if @exchanges.key?(event.stream_id)
395
+ exchange = @exchanges[event.stream_id] = Exchange.new
396
+ exchange.end! if event.fin
397
+ @arrived << [event.stream_id, event.headers]
398
+ when Raiha::HTTP3::Event::Data
399
+ exchange = @exchanges[event.stream_id] or return
400
+ exchange << event.data unless event.data.empty?
401
+ exchange.end! if event.fin
402
+ if exchange.full? && !exchange.paused
403
+ exchange.paused = true
404
+ @binding.pause(event.stream_id)
405
+ end
406
+ when Raiha::HTTP3::Event::Trailers
407
+ exchange = @exchanges[event.stream_id] or return
408
+ exchange.trailers = event.headers
409
+ exchange.end!
410
+ when Raiha::HTTP3::Event::Reset then @exchanges[event.stream_id]&.reset = event.error_code
411
+ when Raiha::HTTP3::Event::Stopped then @exchanges[event.stream_id]&.stopped = event.error_code
412
+ end
413
+ end
414
+ end
415
+ end
416
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::Runtime
4
+ # What a client keeps of what the server gave for later connections: the
5
+ # tickets of SessionTicket events and the tokens of NewToken ones, the
6
+ # newest MAX_HELD of each, newest last, so that a server sending them
7
+ # without end cannot make the client keep them all. A ticket dropped so is
8
+ # told to the connection's `tracer`. A Session of fibers and one of
9
+ # threads keep them alike.
10
+ class Kept
11
+ MAX_HELD = 8
12
+
13
+ def initialize(tracer)
14
+ @tracer = tracer
15
+ @tickets = [] #: Array[Raiha::QUIC::Session]
16
+ @tokens = [] #: Array[Raiha::QUIC::Token]
17
+ end
18
+
19
+ def tickets = @tickets.dup
20
+
21
+ def tokens = @tokens.dup
22
+
23
+ def ticket(session)
24
+ if @tickets.size >= MAX_HELD
25
+ @tickets.shift
26
+ @tracer&.tls_trace(Raiha::TLS::Trace::TicketIgnored.new(reason: :too_many_tickets))
27
+ end
28
+ @tickets << session
29
+ end
30
+
31
+ def token(token)
32
+ @tokens.shift if @tokens.size >= MAX_HELD
33
+ @tokens << token
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,204 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ module Raiha::Runtime
6
+ # The qlog of the connections of a client or a server of the runtime, a
7
+ # file of its own for each in one directory. A trace writes to memory, as
8
+ # a call into the core never waits, and what it wrote goes to its file
9
+ # when the driver's owner flushes after its turn. The traces of a worker,
10
+ # a Ractor, all draw on one QUIC::Qlog::Budget, 1 GiB, however many
11
+ # connections it has had: what it has spent does not come back.
12
+ #
13
+ # The files of the directory are kept to `max_bytes` (1 GiB) and
14
+ # `max_files` (4,096) together, those of earlier runs included: before a
15
+ # new trace begins, the oldest files not being written are taken away.
16
+ # The directory is looked at once; after that, what the Qlog itself has
17
+ # closed is all it counts. A trace holds its file under an exclusive
18
+ # flock while it writes, and a file whose lock cannot be had, which
19
+ # another Qlog or process sharing the directory is writing, is neither
20
+ # taken away nor counted.
21
+ #
22
+ # A server's file is named by a connection ID the client chose, so
23
+ # whoever can write to the directory could put a FIFO or a symbolic link
24
+ # there under that name first. Files are opened without following links
25
+ # and without waiting, and used only when they are regular files; a trace
26
+ # whose file cannot be had so is dropped.
27
+ class Qlog
28
+ BUDGET = 1024 * 1024 * 1024
29
+ MAX_FILES = 4096
30
+
31
+ # What one trace has written and not yet put in its file: the IO its
32
+ # tracer writes to.
33
+ class Pending
34
+ attr_reader :path
35
+
36
+ # The bytes written so far.
37
+ attr_reader :size
38
+
39
+ def initialize(path)
40
+ @path = path
41
+ @data = "".b
42
+ @file = nil #: File?
43
+ @over = false
44
+ @size = 0
45
+ @dropped = false
46
+ end
47
+
48
+ def write(*strings)
49
+ strings.sum do |string|
50
+ @data << string
51
+ @size += string.bytesize
52
+ string.bytesize
53
+ end
54
+ end
55
+
56
+ # The connection is over: the file closes once flushed.
57
+ def over!
58
+ @over = true
59
+ end
60
+
61
+ # Puts what is pending in the file, opened the first time; whether the
62
+ # file is done with.
63
+ def flush
64
+ data, over = take
65
+ put(data, over)
66
+ end
67
+
68
+ # What the tracer has written since, and whether the connection was
69
+ # over by then: for `put`, which may come later, so that the file is
70
+ # written outside the lock the tracer writes under.
71
+ def take
72
+ data = @data
73
+ @data = "".b
74
+ [data, @over]
75
+ end
76
+
77
+ # Puts `data` of `take` in the file, opened the first time; whether
78
+ # the file is done with, closed once the connection was `over`. The
79
+ # write may yield, so what the tracer writes meanwhile is kept for the
80
+ # next take.
81
+ def put(data, over)
82
+ unless data.empty?
83
+ file = (@file ||= open_locked)
84
+ file&.write(data)
85
+ end
86
+ return false unless over
87
+ @file&.close
88
+ true
89
+ end
90
+
91
+ # The file, a regular one held under an exclusive lock while the trace
92
+ # writes it; nil, and the trace dropped, when it cannot be had so.
93
+ private def open_locked
94
+ return nil if @dropped
95
+ flags = File::WRONLY | File::APPEND | File::CREAT | File::NOFOLLOW | File::NONBLOCK
96
+ # A trace has what the peers said, which is no one else's to read.
97
+ file = File.open(@path, flags, 0o600)
98
+ return file if file.stat.file? && file.flock(File::LOCK_EX | File::LOCK_NB)
99
+ file.close
100
+ @dropped = true
101
+ nil
102
+ rescue SystemCallError
103
+ @dropped = true
104
+ nil
105
+ end
106
+ end
107
+
108
+ # The budget of the traces of the current Ractor.
109
+ def self.budget
110
+ Ractor.current[:raiha_next_runtime_qlog_budget] ||= Raiha::QUIC::Qlog::Budget.new(max_bytes: BUDGET) #: Raiha::QUIC::Qlog::Budget
111
+ end
112
+
113
+ attr_reader :budget
114
+
115
+ def initialize(dir, budget: Qlog.budget, max_bytes: BUDGET, max_files: MAX_FILES)
116
+ FileUtils.mkdir_p(dir)
117
+ @dir = dir
118
+ @budget = budget
119
+ @max_bytes = max_bytes
120
+ @max_files = max_files
121
+ @pending = [] #: Array[Pending]
122
+ # The files no trace writes to, oldest first, with their sizes.
123
+ @closed = files_by_age
124
+ end
125
+
126
+ # A tracer of a connection seen from `vantage_point` (:client or
127
+ # :server), in the file "<name>_<vantage point>.sqlog", and its Pending,
128
+ # to be told when the connection is over. A connection whose ALPN may
129
+ # be HTTP/3 names the schema of HTTP/3's events too, which an
130
+ # HTTP3::Qlog::Tracer over this one records.
131
+ def trace(vantage_point, name, tls_config:)
132
+ rotate
133
+ pending = Pending.new(File.join(@dir, "#{name}_#{vantage_point}.sqlog"))
134
+ @pending << pending
135
+ event_schemas = tls_config.alpn.include?("h3") ? [Raiha::HTTP3::Qlog::EVENT_SCHEMA] : [] #: Array[String]
136
+ [Raiha::QUIC::Qlog::Tracer.new(pending, vantage_point:, budget: @budget, event_schemas:), pending]
137
+ end
138
+
139
+ # Puts what the traces have written in their files, and lets go those
140
+ # whose connections are over.
141
+ def flush
142
+ retire(put(take))
143
+ end
144
+
145
+ # What each trace has written since, as Pending#take has it: for `put`,
146
+ # under the lock the tracers write under, as `trace` and `retire` are.
147
+ def take
148
+ @pending.map do |pending|
149
+ data, over = pending.take
150
+ [pending, data, over] #: [Pending, String, bool]
151
+ end
152
+ end
153
+
154
+ # Puts what `take` took in the files, outside that lock; the traces done
155
+ # with, for `retire`. It touches no list of the Qlog's.
156
+ def put(taken)
157
+ taken.filter_map { |pending, data, over| pending if pending.put(data, over) }
158
+ end
159
+
160
+ # Lets go the traces `put` was done with, counting their files among
161
+ # those to take away.
162
+ def retire(done)
163
+ @pending -= done
164
+ done.each { |pending| @closed << [pending.path, pending.size] }
165
+ nil
166
+ end
167
+
168
+ # The qlog files already in the directory, oldest first, with their
169
+ # sizes.
170
+ private def files_by_age
171
+ files = Dir.glob(File.join(@dir, "*.sqlog")).filter_map do |path|
172
+ stat = File.stat(path)
173
+ next nil unless stat.file?
174
+ [path, stat] #: [String, File::Stat]
175
+ rescue SystemCallError
176
+ nil
177
+ end
178
+ files.sort_by { |_, stat| stat.mtime }.map do |path, stat|
179
+ [path, stat.size] #: [String, Integer]
180
+ end
181
+ end
182
+
183
+ # Takes the oldest files no trace writes to away until one more file
184
+ # fits within `max_files`, and what is there within `max_bytes`.
185
+ private def rotate
186
+ bytes = @closed.sum { |_, size| size } + @pending.sum(&:size)
187
+ while (oldest = @closed.first) && (@closed.size + @pending.size + 1 > @max_files || bytes > @max_bytes)
188
+ @closed.shift
189
+ bytes -= oldest[1]
190
+ delete(oldest[0])
191
+ end
192
+ end
193
+
194
+ # Takes a file away when it is a regular file no other writer holds the
195
+ # lock of; one that has gone already is gone.
196
+ private def delete(path)
197
+ File.open(path, File::RDONLY | File::NOFOLLOW | File::NONBLOCK) do |file|
198
+ File.delete(path) if file.stat.file? && file.flock(File::LOCK_EX | File::LOCK_NB)
199
+ end
200
+ rescue SystemCallError
201
+ nil
202
+ end
203
+ end
204
+ end