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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/LICENSE +202 -0
- data/README.md +94 -22
- data/docs/configuration.md +227 -0
- data/docs/development.md +99 -0
- data/docs/fiber-scheduler.md +591 -0
- data/docs/getting-started.md +190 -0
- data/docs/http3-client.md +303 -0
- data/docs/http3-server.md +301 -0
- data/docs/platforms.md +98 -0
- data/docs/qlog.md +86 -0
- data/docs/quic.md +268 -0
- data/docs/threads.md +127 -0
- data/examples/_certificate.rb +55 -0
- data/examples/high_level_loopback.rb +59 -0
- data/examples/http3_get_public.rb +68 -0
- data/examples/in_memory_handshake.rb +38 -0
- data/examples/qlog_output.rb +58 -0
- data/examples/qlog_roundtrips.rb +66 -0
- data/examples/quic_gem_loopback.rb +62 -0
- data/examples/stream_echo.rb +68 -0
- data/examples/udp_handshake.rb +76 -0
- data/lib/raiha/http3/action.rb +21 -0
- data/lib/raiha/http3/capsule.rb +86 -0
- data/lib/raiha/http3/connection.rb +983 -0
- data/lib/raiha/http3/error.rb +24 -0
- data/lib/raiha/http3/error_code.rb +27 -0
- data/lib/raiha/http3/event.rb +63 -0
- data/lib/raiha/http3/frame.rb +148 -0
- data/lib/raiha/http3/frame_reader.rb +114 -0
- data/lib/raiha/http3/message.rb +137 -0
- data/lib/raiha/http3/output.rb +61 -0
- data/lib/raiha/http3/priority.rb +52 -0
- data/lib/raiha/http3/qlog.rb +145 -0
- data/lib/raiha/http3/qpack/decoder.rb +302 -0
- data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
- data/lib/raiha/http3/qpack/encoder.rb +94 -0
- data/lib/raiha/http3/qpack/error.rb +39 -0
- data/lib/raiha/http3/qpack/error_code.rb +13 -0
- data/lib/raiha/http3/qpack/field.rb +15 -0
- data/lib/raiha/http3/qpack/huffman.rb +397 -0
- data/lib/raiha/http3/qpack/reader.rb +82 -0
- data/lib/raiha/http3/qpack/result.rb +26 -0
- data/lib/raiha/http3/qpack/static_table.rb +139 -0
- data/lib/raiha/http3/qpack/writer.rb +44 -0
- data/lib/raiha/http3/qpack.rb +26 -0
- data/lib/raiha/http3/receiving.rb +46 -0
- data/lib/raiha/http3/sending.rb +23 -0
- data/lib/raiha/http3/session.rb +31 -0
- data/lib/raiha/http3/settings.rb +81 -0
- data/lib/raiha/http3/structured_field.rb +120 -0
- data/lib/raiha/http3/tracer.rb +26 -0
- data/lib/raiha/http3/varint.rb +45 -0
- data/lib/raiha/http3.rb +19 -0
- data/lib/raiha/http3_binding.rb +139 -0
- data/lib/raiha/namespace.rb +11 -0
- data/lib/raiha/quic/config.rb +229 -0
- data/lib/raiha/quic/congestion/algorithms.rb +17 -0
- data/lib/raiha/quic/congestion/cubic.rb +258 -0
- data/lib/raiha/quic/congestion/new_reno.rb +161 -0
- data/lib/raiha/quic/congestion/pacing.rb +21 -0
- data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
- data/lib/raiha/quic/congestion/startup.rb +13 -0
- data/lib/raiha/quic/congestion.rb +15 -0
- data/lib/raiha/quic/connection.rb +2022 -0
- data/lib/raiha/quic/connection_ids.rb +62 -0
- data/lib/raiha/quic/crypto/aead.rb +68 -0
- data/lib/raiha/quic/crypto/header_protection.rb +42 -0
- data/lib/raiha/quic/crypto/initial.rb +23 -0
- data/lib/raiha/quic/crypto/keys.rb +32 -0
- data/lib/raiha/quic/crypto/mac.rb +15 -0
- data/lib/raiha/quic/crypto/random.rb +13 -0
- data/lib/raiha/quic/crypto.rb +18 -0
- data/lib/raiha/quic/crypto_stream.rb +161 -0
- data/lib/raiha/quic/datagrams.rb +91 -0
- data/lib/raiha/quic/endpoint.rb +785 -0
- data/lib/raiha/quic/error.rb +61 -0
- data/lib/raiha/quic/error_code.rb +34 -0
- data/lib/raiha/quic/event.rb +59 -0
- data/lib/raiha/quic/issued_connection_ids.rb +137 -0
- data/lib/raiha/quic/kept_packets.rb +41 -0
- data/lib/raiha/quic/mtu_search.rb +75 -0
- data/lib/raiha/quic/negotiation.rb +143 -0
- data/lib/raiha/quic/new_token.rb +55 -0
- data/lib/raiha/quic/pacer.rb +52 -0
- data/lib/raiha/quic/packet_protection.rb +310 -0
- data/lib/raiha/quic/path.rb +473 -0
- data/lib/raiha/quic/peer_connection_ids.rb +160 -0
- data/lib/raiha/quic/qlog.rb +260 -0
- data/lib/raiha/quic/ranges.rb +32 -0
- data/lib/raiha/quic/recovery/duty.rb +102 -0
- data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
- data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
- data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
- data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
- data/lib/raiha/quic/recovery.rb +641 -0
- data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
- data/lib/raiha/quic/retry_token.rb +54 -0
- data/lib/raiha/quic/route.rb +37 -0
- data/lib/raiha/quic/scheduler.rb +112 -0
- data/lib/raiha/quic/session.rb +47 -0
- data/lib/raiha/quic/stateless_reset.rb +38 -0
- data/lib/raiha/quic/stats.rb +24 -0
- data/lib/raiha/quic/streams/receive_window.rb +119 -0
- data/lib/raiha/quic/streams/receiver.rb +217 -0
- data/lib/raiha/quic/streams/sender.rb +200 -0
- data/lib/raiha/quic/streams.rb +687 -0
- data/lib/raiha/quic/test/bottleneck.rb +55 -0
- data/lib/raiha/quic/test/congestion_contract.rb +106 -0
- data/lib/raiha/quic/test/network.rb +244 -0
- data/lib/raiha/quic/test/random_loss.rb +34 -0
- data/lib/raiha/quic/test/reorder.rb +51 -0
- data/lib/raiha/quic/test/server.rb +54 -0
- data/lib/raiha/quic/test/vectors.rb +337 -0
- data/lib/raiha/quic/test/virtual_clock.rb +21 -0
- data/lib/raiha/quic/test.rb +17 -0
- data/lib/raiha/quic/tls_binding.rb +179 -0
- data/lib/raiha/quic/token.rb +28 -0
- data/lib/raiha/quic/tracer.rb +127 -0
- data/lib/raiha/quic/transmit.rb +14 -0
- data/lib/raiha/quic/transport_parameters.rb +244 -0
- data/lib/raiha/quic/version.rb +63 -0
- data/lib/raiha/quic/wire/codec.rb +136 -0
- data/lib/raiha/quic/wire/frame.rb +521 -0
- data/lib/raiha/quic/wire/header.rb +90 -0
- data/lib/raiha/quic/wire/packet.rb +130 -0
- data/lib/raiha/quic/wire/packet_number.rb +47 -0
- data/lib/raiha/quic/wire/retry.rb +62 -0
- data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
- data/lib/raiha/quic/wire.rb +9 -0
- data/lib/raiha/quic.rb +46 -0
- data/lib/raiha/runtime/addresses.rb +35 -0
- data/lib/raiha/runtime/admission.rb +37 -0
- data/lib/raiha/runtime/client.rb +190 -0
- data/lib/raiha/runtime/driver.rb +165 -0
- data/lib/raiha/runtime/error.rb +16 -0
- data/lib/raiha/runtime/http3.rb +363 -0
- data/lib/raiha/runtime/http3_fields.rb +38 -0
- data/lib/raiha/runtime/http3_server.rb +416 -0
- data/lib/raiha/runtime/kept.rb +36 -0
- data/lib/raiha/runtime/qlog.rb +204 -0
- data/lib/raiha/runtime/rack.rb +180 -0
- data/lib/raiha/runtime/server.rb +334 -0
- data/lib/raiha/runtime/session.rb +447 -0
- data/lib/raiha/runtime/threaded/client.rb +161 -0
- data/lib/raiha/runtime/threaded/driver.rb +230 -0
- data/lib/raiha/runtime/threaded/http3.rb +287 -0
- data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
- data/lib/raiha/runtime/threaded/server.rb +290 -0
- data/lib/raiha/runtime/threaded/session.rb +604 -0
- data/lib/raiha/runtime/threaded.rb +45 -0
- data/lib/raiha/runtime/udp.rb +410 -0
- data/lib/raiha/runtime/worker.rb +138 -0
- data/lib/raiha/runtime/workers.rb +248 -0
- data/lib/raiha/runtime.rb +37 -0
- data/lib/raiha/tls/anti_replay.rb +139 -0
- data/lib/raiha/tls/cipher_suite.rb +62 -0
- data/lib/raiha/tls/codec/error.rb +43 -0
- data/lib/raiha/tls/codec/reader.rb +95 -0
- data/lib/raiha/tls/codec/writer.rb +81 -0
- data/lib/raiha/tls/codec.rb +5 -0
- data/lib/raiha/tls/config.rb +318 -0
- data/lib/raiha/tls/crypto/aead.rb +64 -0
- data/lib/raiha/tls/crypto/certificate.rb +28 -0
- data/lib/raiha/tls/crypto/digest.rb +30 -0
- data/lib/raiha/tls/crypto/ecdh.rb +74 -0
- data/lib/raiha/tls/crypto/hash.rb +22 -0
- data/lib/raiha/tls/crypto/hkdf.rb +28 -0
- data/lib/raiha/tls/crypto/signature.rb +87 -0
- data/lib/raiha/tls/crypto.rb +19 -0
- data/lib/raiha/tls/error.rb +36 -0
- data/lib/raiha/tls/event.rb +45 -0
- data/lib/raiha/tls/extension/alpn.rb +35 -0
- data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
- data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
- data/lib/raiha/tls/extension/cookie.rb +27 -0
- data/lib/raiha/tls/extension/early_data.rb +54 -0
- data/lib/raiha/tls/extension/key_share.rb +90 -0
- data/lib/raiha/tls/extension/list.rb +49 -0
- data/lib/raiha/tls/extension/opaque.rb +21 -0
- data/lib/raiha/tls/extension/placement.rb +55 -0
- data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
- data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
- data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
- data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
- data/lib/raiha/tls/extension/registry.rb +50 -0
- data/lib/raiha/tls/extension/server_name.rb +63 -0
- data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
- data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
- data/lib/raiha/tls/extension/supported_groups.rb +32 -0
- data/lib/raiha/tls/extension/supported_versions.rb +53 -0
- data/lib/raiha/tls/extension.rb +32 -0
- data/lib/raiha/tls/handshake/acceptor.rb +164 -0
- data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
- data/lib/raiha/tls/handshake/certificate.rb +56 -0
- data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
- data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
- data/lib/raiha/tls/handshake/client_hello.rb +208 -0
- data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
- data/lib/raiha/tls/handshake/client_side.rb +578 -0
- data/lib/raiha/tls/handshake/conductor.rb +390 -0
- data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
- data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
- data/lib/raiha/tls/handshake/finished.rb +36 -0
- data/lib/raiha/tls/handshake/key_update.rb +38 -0
- data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
- data/lib/raiha/tls/handshake/machine.rb +238 -0
- data/lib/raiha/tls/handshake/message.rb +25 -0
- data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
- data/lib/raiha/tls/handshake/reassembler.rb +88 -0
- data/lib/raiha/tls/handshake/server_hello.rb +98 -0
- data/lib/raiha/tls/handshake/server_side.rb +672 -0
- data/lib/raiha/tls/handshake/violation.rb +12 -0
- data/lib/raiha/tls/handshake.rb +62 -0
- data/lib/raiha/tls/iana.rb +113 -0
- data/lib/raiha/tls/key_exchange.rb +58 -0
- data/lib/raiha/tls/key_schedule.rb +269 -0
- data/lib/raiha/tls/keylog.rb +40 -0
- data/lib/raiha/tls/keyring.rb +180 -0
- data/lib/raiha/tls/masked_inspect.rb +33 -0
- data/lib/raiha/tls/output.rb +32 -0
- data/lib/raiha/tls/profile.rb +82 -0
- data/lib/raiha/tls/record/layer.rb +228 -0
- data/lib/raiha/tls/record/protection.rb +67 -0
- data/lib/raiha/tls/record.rb +11 -0
- data/lib/raiha/tls/server_name.rb +42 -0
- data/lib/raiha/tls/session.rb +106 -0
- data/lib/raiha/tls/signature_scheme.rb +35 -0
- data/lib/raiha/tls/tcp/acceptor.rb +121 -0
- data/lib/raiha/tls/tcp/connection.rb +371 -0
- data/lib/raiha/tls/tcp/event.rb +15 -0
- data/lib/raiha/tls/tcp/socket.rb +161 -0
- data/lib/raiha/tls/tcp.rb +14 -0
- data/lib/raiha/tls/test/fixed_kem.rb +30 -0
- data/lib/raiha/tls/test/fixed_random.rb +18 -0
- data/lib/raiha/tls/test/rfc8448.rb +923 -0
- data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
- data/lib/raiha/tls/test.rb +13 -0
- data/lib/raiha/tls/ticket.rb +184 -0
- data/lib/raiha/tls/ticket_keys.rb +206 -0
- data/lib/raiha/tls/trace.rb +58 -0
- data/lib/raiha/tls/transcript.rb +94 -0
- data/lib/raiha/tls/verifier/default.rb +72 -0
- data/lib/raiha/tls/verifier/host_name.rb +75 -0
- data/lib/raiha/tls/verifier/none.rb +14 -0
- data/lib/raiha/tls/verifier.rb +24 -0
- data/lib/raiha/tls.rb +34 -0
- data/lib/raiha/version.rb +3 -1
- data/lib/raiha.rb +8 -5
- data/sig/patches/array.rbs +9 -0
- data/sig/patches/io_buffer.rbs +13 -0
- data/sig/patches/openssl.rbs +25 -0
- data/sig/patches/socket.rbs +29 -0
- data/sig/patches/string.rbs +26 -0
- data/sig/patches/strscan.rbs +12 -0
- data/sig/raiha/http3/capsule.rbs +32 -0
- data/sig/raiha/http3/connection.rbs +324 -0
- data/sig/raiha/http3/frame.rbs +138 -0
- data/sig/raiha/http3/frame_reader.rbs +39 -0
- data/sig/raiha/http3/message.rbs +23 -0
- data/sig/raiha/http3/priority.rbs +40 -0
- data/sig/raiha/http3/qlog.rbs +54 -0
- data/sig/raiha/http3/qpack.rbs +246 -0
- data/sig/raiha/http3/session.rbs +11 -0
- data/sig/raiha/http3/settings.rbs +26 -0
- data/sig/raiha/http3.rbs +2 -0
- data/sig/raiha/http3_binding.rbs +26 -0
- data/sig/raiha/namespace.rbs +2 -0
- data/sig/raiha/quic/config.rbs +89 -0
- data/sig/raiha/quic/congestion.rbs +213 -0
- data/sig/raiha/quic/connection.rbs +249 -0
- data/sig/raiha/quic/connection_ids.rbs +34 -0
- data/sig/raiha/quic/crypto.rbs +78 -0
- data/sig/raiha/quic/crypto_stream.rbs +34 -0
- data/sig/raiha/quic/datagrams.rbs +32 -0
- data/sig/raiha/quic/endpoint.rbs +179 -0
- data/sig/raiha/quic/frame.rbs +387 -0
- data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
- data/sig/raiha/quic/kept_packets.rbs +25 -0
- data/sig/raiha/quic/mtu_search.rbs +21 -0
- data/sig/raiha/quic/negotiation.rbs +33 -0
- data/sig/raiha/quic/new_token.rbs +14 -0
- data/sig/raiha/quic/pacer.rbs +16 -0
- data/sig/raiha/quic/packet_protection.rbs +63 -0
- data/sig/raiha/quic/path.rbs +106 -0
- data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
- data/sig/raiha/quic/qlog.rbs +74 -0
- data/sig/raiha/quic/ranges.rbs +7 -0
- data/sig/raiha/quic/recovery.rbs +376 -0
- data/sig/raiha/quic/retry_token.rbs +21 -0
- data/sig/raiha/quic/route.rbs +51 -0
- data/sig/raiha/quic/scheduler.rbs +36 -0
- data/sig/raiha/quic/session.rbs +17 -0
- data/sig/raiha/quic/stateless_reset.rbs +10 -0
- data/sig/raiha/quic/streams.rbs +244 -0
- data/sig/raiha/quic/test.rbs +354 -0
- data/sig/raiha/quic/tls_binding.rbs +58 -0
- data/sig/raiha/quic/token.rbs +11 -0
- data/sig/raiha/quic/tracer.rbs +71 -0
- data/sig/raiha/quic/transport_parameters.rbs +113 -0
- data/sig/raiha/quic/values.rbs +200 -0
- data/sig/raiha/quic/version.rbs +30 -0
- data/sig/raiha/quic/wire.rbs +199 -0
- data/sig/raiha/quic.rbs +2 -0
- data/sig/raiha/runtime/threaded.rbs +616 -0
- data/sig/raiha/runtime.rbs +982 -0
- data/sig/raiha/tls/anti_replay.rbs +41 -0
- data/sig/raiha/tls/cipher_suite.rbs +41 -0
- data/sig/raiha/tls/codec.rbs +66 -0
- data/sig/raiha/tls/config.rbs +122 -0
- data/sig/raiha/tls/crypto.rbs +86 -0
- data/sig/raiha/tls/error.rbs +21 -0
- data/sig/raiha/tls/event.rbs +61 -0
- data/sig/raiha/tls/extension.rbs +331 -0
- data/sig/raiha/tls/handshake.rbs +311 -0
- data/sig/raiha/tls/iana.rbs +13 -0
- data/sig/raiha/tls/key_exchange.rbs +33 -0
- data/sig/raiha/tls/key_schedule.rbs +58 -0
- data/sig/raiha/tls/keylog.rbs +20 -0
- data/sig/raiha/tls/keyring.rbs +64 -0
- data/sig/raiha/tls/machine.rbs +429 -0
- data/sig/raiha/tls/masked_inspect.rbs +12 -0
- data/sig/raiha/tls/output.rbs +37 -0
- data/sig/raiha/tls/profile.rbs +26 -0
- data/sig/raiha/tls/record.rbs +79 -0
- data/sig/raiha/tls/server_name.rbs +10 -0
- data/sig/raiha/tls/session.rbs +48 -0
- data/sig/raiha/tls/signature_scheme.rbs +20 -0
- data/sig/raiha/tls/tcp.rbs +134 -0
- data/sig/raiha/tls/test.rbs +143 -0
- data/sig/raiha/tls/ticket.rbs +68 -0
- data/sig/raiha/tls/ticket_keys.rbs +67 -0
- data/sig/raiha/tls/trace.rbs +99 -0
- data/sig/raiha/tls/transcript.rbs +22 -0
- data/sig/raiha/tls/verifier.rbs +54 -0
- data/sig/raiha/tls.rbs +5 -0
- data/sig/raiha.rbs +3 -0
- metadata +355 -57
- data/.gitignore +0 -10
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -4
- data/Gemfile.lock +0 -22
- data/Rakefile +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/raiha.gemspec +0 -40
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::Runtime
|
|
4
|
+
module Threaded
|
|
5
|
+
# The threads a client or a server of the runtime starts to run the
|
|
6
|
+
# application, each named, and how many of them may run at once: a
|
|
7
|
+
# thread takes a place before it starts, waiting while `max` are
|
|
8
|
+
# taken, and gives it back once it ends. A server has one of these for
|
|
9
|
+
# the threads of streams and requests, and one for those of its
|
|
10
|
+
# connections' handlers, whose places it only counts. The threads
|
|
11
|
+
# started are kept until they end, however long their connections
|
|
12
|
+
# last, so that they can be joined.
|
|
13
|
+
#
|
|
14
|
+
# Its lock is its own, apart from a driver's core lock, and nothing
|
|
15
|
+
# waits for a place holding the core lock; what wakes those waiting
|
|
16
|
+
# (`wake`) may hold it.
|
|
17
|
+
class Threads
|
|
18
|
+
attr_reader :max
|
|
19
|
+
|
|
20
|
+
def initialize(max)
|
|
21
|
+
raise ArgumentError, "max_threads is an Integer of 1 or more, not #{max.inspect}" unless max.is_a?(Integer) && max.positive?
|
|
22
|
+
@max = max
|
|
23
|
+
@lock = Thread::Mutex.new
|
|
24
|
+
@freed = Thread::ConditionVariable.new
|
|
25
|
+
@taken = 0
|
|
26
|
+
@running = {} #: Hash[Thread, true]
|
|
27
|
+
@running.compare_by_identity
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# How many places are taken.
|
|
31
|
+
def taken = @lock.synchronize { @taken }
|
|
32
|
+
|
|
33
|
+
# Takes a place, waiting while there is none, until the block is true:
|
|
34
|
+
# whether one was taken.
|
|
35
|
+
def take
|
|
36
|
+
@lock.synchronize do
|
|
37
|
+
until @taken < @max
|
|
38
|
+
return false if yield
|
|
39
|
+
@freed.wait(@lock)
|
|
40
|
+
end
|
|
41
|
+
return false if yield
|
|
42
|
+
@taken += 1
|
|
43
|
+
true
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Takes a place without waiting, counting a thread that runs whether
|
|
48
|
+
# or not there is room: for a connection's handler, which the server
|
|
49
|
+
# counts before it takes the connection.
|
|
50
|
+
def count
|
|
51
|
+
@lock.synchronize { @taken += 1 }
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def give_back
|
|
56
|
+
@lock.synchronize do
|
|
57
|
+
@taken -= 1
|
|
58
|
+
@freed.signal
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Wakes what waits for a place, to look again at what it waits until.
|
|
63
|
+
def wake
|
|
64
|
+
@lock.synchronize { @freed.broadcast }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Starts a thread named `name` for the block, on a place taken; the
|
|
68
|
+
# place is given back once it ends, or at once when the thread cannot
|
|
69
|
+
# be started, which raises.
|
|
70
|
+
def start(name)
|
|
71
|
+
@lock.synchronize do
|
|
72
|
+
thread = Thread.new do
|
|
73
|
+
# Until it is kept among those running.
|
|
74
|
+
@lock.synchronize { nil }
|
|
75
|
+
yield
|
|
76
|
+
ensure
|
|
77
|
+
@lock.synchronize do
|
|
78
|
+
@running.delete(Thread.current)
|
|
79
|
+
@taken -= 1
|
|
80
|
+
@freed.signal
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
thread.name = name
|
|
84
|
+
@running[thread] = true
|
|
85
|
+
thread
|
|
86
|
+
end
|
|
87
|
+
rescue ThreadError
|
|
88
|
+
give_back
|
|
89
|
+
raise
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The threads started and not yet ended.
|
|
93
|
+
def running = @lock.synchronize { @running.keys }
|
|
94
|
+
|
|
95
|
+
# Waits for the threads started to end, until `deadline` at most;
|
|
96
|
+
# those running on are left to.
|
|
97
|
+
def join(deadline)
|
|
98
|
+
running.each do |thread|
|
|
99
|
+
left = deadline - Driver.clock
|
|
100
|
+
break unless left.positive?
|
|
101
|
+
thread.join(left)
|
|
102
|
+
end
|
|
103
|
+
nil
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# A QUIC connection the threads of the application use as if it
|
|
108
|
+
# blocked, as the Session of fibers is: a read waits for data, a write
|
|
109
|
+
# for room, an open for a stream the peer allows, each on a condition
|
|
110
|
+
# of its Driver's core lock, which the runner's turns broadcast. Each
|
|
111
|
+
# method holds the core lock from when it is called until it returns,
|
|
112
|
+
# but while it waits; so does what it does once a wait is over, so that
|
|
113
|
+
# no event taken by another thread in between is missed.
|
|
114
|
+
#
|
|
115
|
+
# The events of the connection are taken before each wait is looked at
|
|
116
|
+
# again. What the Session keeps of a stream goes once the application
|
|
117
|
+
# is done with both its parts. A protocol over the connection, HTTP/3,
|
|
118
|
+
# is attached to the Session and called under the lock; the blocks of
|
|
119
|
+
# `wait_for` are too, and are not to wait.
|
|
120
|
+
#
|
|
121
|
+
# `connection` is the QUIC connection behind a lock: each of its methods
|
|
122
|
+
# is called holding the core lock. What one of them returns is read
|
|
123
|
+
# without it, and may change.
|
|
124
|
+
#
|
|
125
|
+
# A client's Session keeps the tickets and the NEW_TOKEN tokens the
|
|
126
|
+
# server gives (Kept). When the server takes no 0-RTT, each Stream the
|
|
127
|
+
# client opened before raises QUIC::Error::ZeroRttRejected.
|
|
128
|
+
#
|
|
129
|
+
# `each_stream` starts a thread ("raiha-stream") for each stream the
|
|
130
|
+
# peer opens, once it has a place of `threads`, waiting for one while
|
|
131
|
+
# the connection is open and the driver runs.
|
|
132
|
+
class Session
|
|
133
|
+
# The QUIC connection behind the core lock of its driver.
|
|
134
|
+
class Locked < BasicObject
|
|
135
|
+
def initialize(connection, driver)
|
|
136
|
+
@connection = connection
|
|
137
|
+
@driver = driver
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def method_missing(name, ...)
|
|
141
|
+
@driver.synchronize { @connection.__send__(name, ...) }
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def respond_to_missing?(name, include_private = false) = @connection.respond_to?(name, include_private)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
Track = Raiha::Runtime::Session::Track
|
|
148
|
+
MAX_ACK_DELAY = Raiha::Runtime::Session::MAX_ACK_DELAY
|
|
149
|
+
MAX_HELD = Kept::MAX_HELD
|
|
150
|
+
CLOSE_TIMEOUT = Raiha::Runtime::Session::CLOSE_TIMEOUT
|
|
151
|
+
|
|
152
|
+
# The qlog tracer of the connection, if it has one, which a protocol
|
|
153
|
+
# records its own events with.
|
|
154
|
+
attr_reader :tracer
|
|
155
|
+
|
|
156
|
+
# At a client, the name of the server the connection is to, and what
|
|
157
|
+
# Client.connect was given to resume. Both are nil at a server.
|
|
158
|
+
attr_reader :server_name, :resumed
|
|
159
|
+
|
|
160
|
+
def initialize(connection, driver, role:, threads:, tracer: nil, server_name: nil, resumed: nil)
|
|
161
|
+
@connection = connection
|
|
162
|
+
@locked = Locked.new(connection, driver)
|
|
163
|
+
@tracer = tracer
|
|
164
|
+
@server_name = server_name
|
|
165
|
+
@resumed = resumed
|
|
166
|
+
@driver = driver
|
|
167
|
+
@threads = threads
|
|
168
|
+
@role = role
|
|
169
|
+
@changed = driver.new_cond
|
|
170
|
+
@waiting = 0
|
|
171
|
+
@tracks = {} #: Hash[Integer, Track]
|
|
172
|
+
@incoming = [] #: Array[Integer]
|
|
173
|
+
@peer_next = [0, 0, 0, 0]
|
|
174
|
+
@aborted = nil #: StandardError?
|
|
175
|
+
@stopping = false
|
|
176
|
+
@stop_deadline = nil #: Float?
|
|
177
|
+
@connected = nil #: Raiha::QUIC::Event::Connected?
|
|
178
|
+
@protocol = nil #: _Protocol?
|
|
179
|
+
@close_code = 0
|
|
180
|
+
@kept = Kept.new(tracer)
|
|
181
|
+
@generation = 0
|
|
182
|
+
# Whether the connection has closed or the driver stopped, for what
|
|
183
|
+
# waits for a place of `threads` to look at without the core lock.
|
|
184
|
+
@ended = false
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# The threads the application's code runs in, which a protocol's
|
|
188
|
+
# take their places of too.
|
|
189
|
+
attr_reader :threads
|
|
190
|
+
|
|
191
|
+
# The QUIC connection, each of its methods called under the core
|
|
192
|
+
# lock.
|
|
193
|
+
def connection = @locked # steep:ignore
|
|
194
|
+
|
|
195
|
+
# The QUIC connection itself, for a protocol that calls it holding the
|
|
196
|
+
# core lock, as it does in its turn.
|
|
197
|
+
def core = @connection
|
|
198
|
+
|
|
199
|
+
# Holds the core lock around the block, for a protocol whose method
|
|
200
|
+
# touches what the lock guards from its start to its end.
|
|
201
|
+
def synchronize(&) = @driver.synchronize(&)
|
|
202
|
+
|
|
203
|
+
# Whether the connection has closed or the driver stopped, read
|
|
204
|
+
# without the lock by what waits for a place of `threads`.
|
|
205
|
+
def ended? = @ended
|
|
206
|
+
|
|
207
|
+
# Raises Error::Usage where this thread holds the core lock, before
|
|
208
|
+
# the application's code runs on it: a thread that did would keep the
|
|
209
|
+
# driver from running while it waits.
|
|
210
|
+
def unlocked!
|
|
211
|
+
raise Error::Usage, "the core lock of the driver is held where the application runs" if @driver.mon_owned?
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def handshake = @driver.synchronize { @connected&.handshake }
|
|
215
|
+
|
|
216
|
+
def tickets = @driver.synchronize { @kept.tickets }
|
|
217
|
+
|
|
218
|
+
def tokens = @driver.synchronize { @kept.tokens }
|
|
219
|
+
|
|
220
|
+
# Puts `protocol` over the connection, which then closes with its
|
|
221
|
+
# `close_code` when no other is given. A handshake completed before is
|
|
222
|
+
# told it with the Connected event it missed. A connection that has
|
|
223
|
+
# closed takes none, and raises QUIC::ClosedError, as a handler's
|
|
224
|
+
# thread may come to it after the peer has closed.
|
|
225
|
+
def attach(protocol, close_code: @close_code)
|
|
226
|
+
@driver.synchronize do
|
|
227
|
+
aborted = @aborted and raise aborted
|
|
228
|
+
reason = @connection.close_reason and raise Raiha::QUIC::ClosedError.new(reason)
|
|
229
|
+
@protocol = protocol
|
|
230
|
+
@close_code = close_code
|
|
231
|
+
connected = @connected and protocol.take(connected, Driver.clock)
|
|
232
|
+
changed
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# Has what the protocol or the application changed go out.
|
|
237
|
+
def changed = @driver.changed
|
|
238
|
+
|
|
239
|
+
# A new stream of `direction` for a protocol, which keeps it itself:
|
|
240
|
+
# its ID, once the peer allows it.
|
|
241
|
+
def open_stream_id(direction: :bidi)
|
|
242
|
+
@driver.synchronize do
|
|
243
|
+
stream_id = wait { @connection.open_stream(direction:) }
|
|
244
|
+
changed
|
|
245
|
+
stream_id
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# Calls the block until it gives something other than nil or "",
|
|
250
|
+
# under the core lock, waiting for a change in between: for a
|
|
251
|
+
# protocol's waits. With `deadline`, nil once the clock is past it;
|
|
252
|
+
# with `drain`, a read gives what came before the connection closed.
|
|
253
|
+
# steep picks no overload of `wait` for a block that gives T?.
|
|
254
|
+
def wait_for(deadline: nil, drain: false) # steep:ignore
|
|
255
|
+
@driver.synchronize { deadline ? wait(deadline:, drain:) { yield } : wait(drain:) { yield } }
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# A new stream of `direction` (:bidi or :uni), once the peer allows it.
|
|
259
|
+
def open_stream(direction: :bidi)
|
|
260
|
+
@driver.synchronize do
|
|
261
|
+
stream_id = wait { @connection.open_stream(direction:) }
|
|
262
|
+
@tracks[stream_id] = Track.new(nil, nil, direction == :bidi, true)
|
|
263
|
+
changed
|
|
264
|
+
Stream.new(self, stream_id, @generation)
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# Waits, for `seconds` at most, until all the streams have sent has
|
|
269
|
+
# been acknowledged; whether it has.
|
|
270
|
+
def wait_acknowledged(seconds)
|
|
271
|
+
@driver.synchronize { wait(deadline: Driver.clock + seconds) { @connection.stats.send_buffer_bytes.zero? || nil } || false }
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
# Closes the connection with the application's `error_code` once what
|
|
275
|
+
# the streams sent is acknowledged, or three PTOs have gone with no
|
|
276
|
+
# more of it acknowledged, or `timeout` seconds in all, unless it has
|
|
277
|
+
# closed already, as the Session of fibers does.
|
|
278
|
+
def close_when_acknowledged(error_code: @close_code, timeout: CLOSE_TIMEOUT)
|
|
279
|
+
@driver.synchronize do
|
|
280
|
+
next if closed?
|
|
281
|
+
limit = Driver.clock + timeout
|
|
282
|
+
while (left = @connection.stats.send_buffer_bytes).positive?
|
|
283
|
+
stats = @connection.stats
|
|
284
|
+
three_ptos = 3 * (stats.rtt + [4 * stats.rtt_var, 0.001].max + MAX_ACK_DELAY)
|
|
285
|
+
deadline = [Driver.clock + three_ptos, limit].min
|
|
286
|
+
break unless wait(deadline:) { @connection.stats.send_buffer_bytes < left || nil }
|
|
287
|
+
end
|
|
288
|
+
close(error_code:)
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def wait_handshake
|
|
293
|
+
@driver.synchronize { wait { @connection.handshake_complete? || nil } }
|
|
294
|
+
nil
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# Waits until the application may send: until the handshake has
|
|
298
|
+
# completed, or not at all while 0-RTT goes.
|
|
299
|
+
def wait_ready
|
|
300
|
+
@driver.synchronize { wait { @connection.handshake_complete? || @connection.zero_rtt_state == :pending || nil } }
|
|
301
|
+
nil
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# The next stream the peer opens; nil once the connection has closed,
|
|
305
|
+
# or the server is stopping and none has come.
|
|
306
|
+
def accept_stream
|
|
307
|
+
@driver.synchronize do
|
|
308
|
+
stream_id = wait(drain: true) { @incoming.shift || (:stopping if @stopping) }
|
|
309
|
+
Stream.new(self, stream_id, @generation) unless stream_id == :stopping
|
|
310
|
+
end
|
|
311
|
+
rescue Raiha::QUIC::ClosedError
|
|
312
|
+
nil
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# Each stream the peer opens, in a thread of its own once a place is
|
|
316
|
+
# free, until the connection closes or the server stops. A stream
|
|
317
|
+
# whose thread cannot be started is reset, and stopped, with the
|
|
318
|
+
# close code, and the ThreadError raised.
|
|
319
|
+
def each_stream(&block)
|
|
320
|
+
unlocked!
|
|
321
|
+
while @threads.take { @ended }
|
|
322
|
+
stream = accept_stream
|
|
323
|
+
unless stream
|
|
324
|
+
@threads.give_back
|
|
325
|
+
break
|
|
326
|
+
end
|
|
327
|
+
start_stream(stream, &block)
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# Closes the connection with the application's `error_code`.
|
|
332
|
+
def close(error_code: @close_code, reason: "")
|
|
333
|
+
@driver.synchronize do
|
|
334
|
+
@connection.close(error_code:, reason:, application: true, now: Driver.clock)
|
|
335
|
+
changed
|
|
336
|
+
notify
|
|
337
|
+
ended
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def closed? = @driver.synchronize { !(@aborted || @connection.close_reason).nil? }
|
|
342
|
+
|
|
343
|
+
def stopping? = @driver.synchronize { @stopping }
|
|
344
|
+
|
|
345
|
+
def stop_deadline = @driver.synchronize { @stop_deadline }
|
|
346
|
+
|
|
347
|
+
# The server is stopping, and closes what is left at `deadline`: what
|
|
348
|
+
# waits for a stream to come wakes.
|
|
349
|
+
def stopping!(deadline = nil)
|
|
350
|
+
@driver.synchronize do
|
|
351
|
+
@stopping = true
|
|
352
|
+
@stop_deadline = deadline
|
|
353
|
+
notify
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# The server is out of time to stop: the protocol is told, what it
|
|
358
|
+
# sent goes, and the connection closes with `error_code`.
|
|
359
|
+
def expire(error_code:)
|
|
360
|
+
@driver.synchronize do
|
|
361
|
+
next if closed?
|
|
362
|
+
@protocol&.expire
|
|
363
|
+
@driver.flush
|
|
364
|
+
close(error_code:)
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
# The driver has stopped, after `failure` when something failed in
|
|
369
|
+
# it: what waits wakes to raise it, or QUIC::ClosedError.
|
|
370
|
+
def abort(failure = nil)
|
|
371
|
+
@driver.synchronize do
|
|
372
|
+
@aborted ||= failure || Raiha::QUIC::ClosedError.new(Raiha::QUIC::ConnectionError.new(
|
|
373
|
+
code: Raiha::QUIC::ErrorCode::NO_ERROR, frame_type: nil, reason: "the runtime stopped", application: false))
|
|
374
|
+
notify
|
|
375
|
+
ended
|
|
376
|
+
end
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
# The runner's turn, under the core lock: the events are taken, and
|
|
380
|
+
# what waits looks again.
|
|
381
|
+
def turn
|
|
382
|
+
take_events
|
|
383
|
+
notify if @waiting.positive?
|
|
384
|
+
ended if closed?
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
# Up to `max_bytes` of `stream_id` once there are any, nil at its
|
|
388
|
+
# end, and after it.
|
|
389
|
+
def read(stream_id, max_bytes, generation:)
|
|
390
|
+
@driver.synchronize do
|
|
391
|
+
current!(stream_id, generation)
|
|
392
|
+
track = @tracks[stream_id]
|
|
393
|
+
next nil unless track&.reading
|
|
394
|
+
data = wait(drain: true) do
|
|
395
|
+
current!(stream_id, generation)
|
|
396
|
+
if (reset = track.reset)
|
|
397
|
+
over(stream_id, :reading)
|
|
398
|
+
raise StreamReset.new(stream_id, reset)
|
|
399
|
+
end
|
|
400
|
+
@connection.stream_read(stream_id, max_bytes) || :end
|
|
401
|
+
end
|
|
402
|
+
if data.is_a?(String)
|
|
403
|
+
# What was read frees the peer's flow control, which the runner
|
|
404
|
+
# tells it of; asleep since before the read, it would wait for
|
|
405
|
+
# no more and the peer stay held back.
|
|
406
|
+
changed
|
|
407
|
+
next data
|
|
408
|
+
end
|
|
409
|
+
over(stream_id, :reading)
|
|
410
|
+
nil
|
|
411
|
+
end
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
# Writes all of `data` to `stream_id`, waiting for room as it goes,
|
|
415
|
+
# and ends it with `fin`.
|
|
416
|
+
def write(stream_id, data, fin:, generation:)
|
|
417
|
+
@driver.synchronize do
|
|
418
|
+
current!(stream_id, generation)
|
|
419
|
+
track = @tracks[stream_id]
|
|
420
|
+
raise Raiha::QUIC::Error::StreamError, "stream #{stream_id} has ended" unless track&.writing
|
|
421
|
+
written = 0
|
|
422
|
+
wait do
|
|
423
|
+
current!(stream_id, generation)
|
|
424
|
+
if (stopped = track.stop)
|
|
425
|
+
over(stream_id, :writing)
|
|
426
|
+
raise StreamReset.new(stream_id, stopped)
|
|
427
|
+
end
|
|
428
|
+
taken = @connection.stream_write(stream_id, data.byteslice(written..) || "".b, fin:)
|
|
429
|
+
written += taken
|
|
430
|
+
# A write that took nothing has nothing to send; the runner
|
|
431
|
+
# woken for it would wake this thread again, and round they
|
|
432
|
+
# would go.
|
|
433
|
+
changed if taken.positive? || data.empty?
|
|
434
|
+
written if written == data.bytesize
|
|
435
|
+
end
|
|
436
|
+
over(stream_id, :writing) if fin
|
|
437
|
+
written
|
|
438
|
+
end
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
# The thread of `stream`, which it is given apart from the loop of
|
|
443
|
+
# each_stream that goes on to the next.
|
|
444
|
+
private def start_stream(stream)
|
|
445
|
+
@threads.start("raiha-stream") do
|
|
446
|
+
unlocked!
|
|
447
|
+
yield stream
|
|
448
|
+
end
|
|
449
|
+
rescue ThreadError
|
|
450
|
+
give_up(stream.id)
|
|
451
|
+
raise
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
# A stream no thread could be started for: it is let go both ways.
|
|
455
|
+
private def give_up(stream_id)
|
|
456
|
+
@driver.synchronize do
|
|
457
|
+
@connection.stream_reset(stream_id, error_code: @close_code)
|
|
458
|
+
@connection.stream_stop_sending(stream_id, error_code: @close_code)
|
|
459
|
+
@tracks.delete(stream_id)
|
|
460
|
+
changed
|
|
461
|
+
end
|
|
462
|
+
rescue Raiha::QUIC::Error::Base
|
|
463
|
+
nil
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
# The connection has closed or the driver stopped: what waits for a
|
|
467
|
+
# place of `threads` is to stop waiting.
|
|
468
|
+
private def ended
|
|
469
|
+
return if @ended
|
|
470
|
+
@ended = true
|
|
471
|
+
@threads.wake
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
private def take_events
|
|
475
|
+
protocol = @protocol
|
|
476
|
+
now = Driver.clock
|
|
477
|
+
@connection.each_event do |event|
|
|
478
|
+
zero_rtt_rejected if event.is_a?(Raiha::QUIC::Event::ZeroRttRejected)
|
|
479
|
+
take(event) unless protocol&.take(event, now)
|
|
480
|
+
end
|
|
481
|
+
protocol&.turn(now)
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
private def zero_rtt_rejected
|
|
485
|
+
@generation += 1
|
|
486
|
+
@tracks.delete_if { |stream_id, _| local?(stream_id) }
|
|
487
|
+
@connection.acknowledge_zero_rtt_rejection
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
private def current!(stream_id, generation)
|
|
491
|
+
return if generation == @generation || !local?(stream_id)
|
|
492
|
+
raise Raiha::QUIC::Error::ZeroRttRejected, "stream #{stream_id} went in 0-RTT, which the server rejected"
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
private def take(event)
|
|
496
|
+
case event
|
|
497
|
+
when Raiha::QUIC::Event::Connected then @connected = event
|
|
498
|
+
when Raiha::QUIC::Event::StreamReadable then arrived(event.stream_id)
|
|
499
|
+
when Raiha::QUIC::Event::StreamReset
|
|
500
|
+
arrived(event.stream_id)
|
|
501
|
+
@tracks[event.stream_id]&.reset = event.error_code
|
|
502
|
+
forget_reset(event.stream_id)
|
|
503
|
+
when Raiha::QUIC::Event::StopSending then @tracks[event.stream_id]&.stop = event.error_code
|
|
504
|
+
when Raiha::QUIC::Event::SessionTicket then @kept.ticket(event.session)
|
|
505
|
+
when Raiha::QUIC::Event::NewToken then @kept.token(event.token)
|
|
506
|
+
end
|
|
507
|
+
end
|
|
508
|
+
|
|
509
|
+
private def arrived(stream_id)
|
|
510
|
+
return if local?(stream_id)
|
|
511
|
+
type = stream_id & 0x3
|
|
512
|
+
while @peer_next.fetch(type) <= stream_id >> 2
|
|
513
|
+
opened = @peer_next.fetch(type) << 2 | type
|
|
514
|
+
@tracks[opened] = Track.new(nil, nil, true, opened.nobits?(0x2))
|
|
515
|
+
@incoming << opened
|
|
516
|
+
@peer_next[type] = @peer_next.fetch(type) + 1
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
private def forget_reset(stream_id)
|
|
521
|
+
return unless stream_id.anybits?(0x2) && @incoming.delete(stream_id)
|
|
522
|
+
@tracks.delete(stream_id)
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
private def over(stream_id, part)
|
|
526
|
+
track = @tracks[stream_id] or return
|
|
527
|
+
track[part] = false
|
|
528
|
+
@tracks.delete(stream_id) unless track.reading || track.writing
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
private def local?(stream_id) = stream_id.anybits?(0x1) == (@role == :server)
|
|
532
|
+
|
|
533
|
+
private def notify = @changed.broadcast
|
|
534
|
+
|
|
535
|
+
# Calls the block until it gives something other than nil or "",
|
|
536
|
+
# waiting on the Session's condition in between, which lets the core
|
|
537
|
+
# lock go; what it gave, or nil once the clock is past `deadline`.
|
|
538
|
+
# Once the connection has closed, raises QUIC::ClosedError, and once
|
|
539
|
+
# the driver has stopped, what `abort` was given; with `drain`, the
|
|
540
|
+
# block is called first all the same. The caller holds the core lock.
|
|
541
|
+
private def wait(deadline: nil, drain: false)
|
|
542
|
+
@waiting += 1
|
|
543
|
+
loop do
|
|
544
|
+
take_events
|
|
545
|
+
result = yield if drain
|
|
546
|
+
return result unless result.nil? || result == "" # steep:ignore
|
|
547
|
+
aborted = @aborted and raise aborted
|
|
548
|
+
reason = @connection.close_reason and raise Raiha::QUIC::ClosedError.new(reason)
|
|
549
|
+
result = yield unless drain
|
|
550
|
+
return result unless result.nil? || result == "" # steep:ignore
|
|
551
|
+
left = deadline && deadline - Driver.clock
|
|
552
|
+
return nil if left && left <= 0
|
|
553
|
+
@changed.wait(left)
|
|
554
|
+
end
|
|
555
|
+
ensure
|
|
556
|
+
@waiting -= 1
|
|
557
|
+
end
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
# A stream of a Session, read and written as an IO would be, as the
|
|
561
|
+
# Stream of fibers is. Two threads are not to read it, or write it, at
|
|
562
|
+
# once.
|
|
563
|
+
class Stream
|
|
564
|
+
PIECE = Raiha::Runtime::Stream::PIECE
|
|
565
|
+
|
|
566
|
+
attr_reader :id
|
|
567
|
+
|
|
568
|
+
def initialize(session, id, generation)
|
|
569
|
+
@session = session
|
|
570
|
+
@id = id
|
|
571
|
+
@generation = generation
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
# With `length`, up to that many bytes once there are any, nil at the
|
|
575
|
+
# end; without, all until the end, "" when it has ended.
|
|
576
|
+
def read(length = nil)
|
|
577
|
+
return @session.read(@id, length, generation: @generation) if length
|
|
578
|
+
data = "".b
|
|
579
|
+
while (chunk = @session.read(@id, PIECE, generation: @generation))
|
|
580
|
+
data << chunk
|
|
581
|
+
end
|
|
582
|
+
data
|
|
583
|
+
end
|
|
584
|
+
|
|
585
|
+
# Writes all of `data`, waiting while the peer's flow control holds it
|
|
586
|
+
# back; its length.
|
|
587
|
+
def write(data)
|
|
588
|
+
@session.write(@id, data.b, fin: false, generation: @generation)
|
|
589
|
+
data.bytesize
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
def <<(data)
|
|
593
|
+
write(data)
|
|
594
|
+
self
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
# Ends the sending part.
|
|
598
|
+
def close_write
|
|
599
|
+
@session.write(@id, "".b, fin: true, generation: @generation)
|
|
600
|
+
nil
|
|
601
|
+
end
|
|
602
|
+
end
|
|
603
|
+
end
|
|
604
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "monitor"
|
|
4
|
+
|
|
5
|
+
module Raiha::Runtime
|
|
6
|
+
# The runtime on threads, with no Fiber scheduler: the classes of
|
|
7
|
+
# Raiha::Runtime by the same names, to be called from any thread. Where
|
|
8
|
+
# the runtime of fibers starts a fiber, this one starts a thread: the
|
|
9
|
+
# reader of each socket and the runner of a driver, the handler of each
|
|
10
|
+
# connection, each stream of `each_stream` and each request of HTTP/3.
|
|
11
|
+
#
|
|
12
|
+
# A fiber runs until it yields, so the runtime of fibers calls into the
|
|
13
|
+
# core with no lock. A thread can be preempted anywhere, so each Driver
|
|
14
|
+
# here has a lock, a Monitor, that whatever touches its Endpoint, its
|
|
15
|
+
# connections, their HTTP/3 or what a Session keeps takes first; no I/O
|
|
16
|
+
# and no application code runs under it, and what waits waits on a
|
|
17
|
+
# condition of it. The runtime of fibers and this one are not to be
|
|
18
|
+
# mixed in one connection.
|
|
19
|
+
module Threaded
|
|
20
|
+
# The budget of the traces of the current Ractor's clients and
|
|
21
|
+
# servers on threads, as Qlog.budget is of those on fibers: drivers
|
|
22
|
+
# that each hold a lock of their own spend it, so it holds one too.
|
|
23
|
+
class Budget < Raiha::QUIC::Qlog::Budget
|
|
24
|
+
def self.current
|
|
25
|
+
Ractor.current[:raiha_threaded_qlog_budget] ||= new(max_bytes: Qlog::BUDGET) #: Budget
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize(max_bytes:)
|
|
29
|
+
super
|
|
30
|
+
@lock = Thread::Mutex.new
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def left = @lock.synchronize { super }
|
|
34
|
+
|
|
35
|
+
def spend(bytes) = @lock.synchronize { super }
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
require_relative "threaded/driver"
|
|
41
|
+
require_relative "threaded/session"
|
|
42
|
+
require_relative "threaded/server"
|
|
43
|
+
require_relative "threaded/client"
|
|
44
|
+
require_relative "threaded/http3"
|
|
45
|
+
require_relative "threaded/http3_server"
|