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,687 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::QUIC
|
|
4
|
+
# The streams of a connection and their flow control (RFC 9000 "Streams"
|
|
5
|
+
# and "Flow Control"): the receiving and sending parts of each stream,
|
|
6
|
+
# the connection's receive window, and the limits on how many streams
|
|
7
|
+
# each end opens.
|
|
8
|
+
#
|
|
9
|
+
# A stream ID tells who opened the stream and which way it goes; a frame
|
|
10
|
+
# for a part the stream does not have, or for a stream of this end not
|
|
11
|
+
# opened yet, is STREAM_STATE_ERROR, and a stream of the peer past the
|
|
12
|
+
# limit given STREAM_LIMIT_ERROR. A stream of the peer opens with the
|
|
13
|
+
# first frame for it, and with it those of the same kind numbered below
|
|
14
|
+
# it (RFC 9000 "Stream Types and Identifiers").
|
|
15
|
+
#
|
|
16
|
+
# A STREAM frame is checked before any of its data is kept: the
|
|
17
|
+
# stream's ID, its final size, its limit, then the connection's limit,
|
|
18
|
+
# in that order, so that the first rule broken names the error (flow
|
|
19
|
+
# control's design, "Detecting Violations"). The connection has received
|
|
20
|
+
# the sum of how far each stream has, counted however the data has
|
|
21
|
+
# gaps (CVE-2026-75804). The holes of all the streams together are at
|
|
22
|
+
# most one for each datagram the connection's largest receive window
|
|
23
|
+
# holds, past which the connection closes with INTERNAL_ERROR rather
|
|
24
|
+
# than dropping data an ACK has already covered: a peer that sends in
|
|
25
|
+
# bursts loses up to every other datagram when a queue overflows, which
|
|
26
|
+
# stays under it, and holes of a byte each stay some thirteen thousand
|
|
27
|
+
# pieces with the default window.
|
|
28
|
+
#
|
|
29
|
+
# A stream that had nothing to read and has now tells the application
|
|
30
|
+
# with Event::StreamReadable, once, as the application reads it until it
|
|
31
|
+
# gives "" or nil. Credit goes back as MAX_DATA and MAX_STREAM_DATA as
|
|
32
|
+
# the data is read; a stream's window that grows takes the connection's
|
|
33
|
+
# to one and a half times it at least, so that one stream cannot use up
|
|
34
|
+
# the connection's window.
|
|
35
|
+
#
|
|
36
|
+
# Sending, a write takes what the stream's credit, the connection's and
|
|
37
|
+
# this end's send buffer, max_send_buffer bytes of data not sent or not
|
|
38
|
+
# acknowledged, all leave room for, and no more; a stream written less
|
|
39
|
+
# than asked tells the application with Event::StreamWritable, once,
|
|
40
|
+
# when it can take some again (flow control's design, "Sending"). A
|
|
41
|
+
# write the peer's credit cuts short, or a stream the peer's limit keeps
|
|
42
|
+
# from opening, sends DATA_BLOCKED, STREAM_DATA_BLOCKED or
|
|
43
|
+
# STREAMS_BLOCKED with the limit, once for each value of it; one this
|
|
44
|
+
# end's send buffer cuts short sends none. The tracer hears when the
|
|
45
|
+
# peer's credit blocks the connection or a stream, and when it no longer
|
|
46
|
+
# does. Frames are taken from the streams with something to send in the
|
|
47
|
+
# order the Scheduler keeps, one a time.
|
|
48
|
+
#
|
|
49
|
+
# The application stops reading a stream with STOP_SENDING, or resets
|
|
50
|
+
# one it writes with RESET_STREAM; a peer's STOP_SENDING resets the
|
|
51
|
+
# stream with its error code (RFC 9000 "Solicited State Transitions").
|
|
52
|
+
# What a stream abandoned on either end held goes back to the
|
|
53
|
+
# connection's window at once, and its receiving part gets no more
|
|
54
|
+
# credit.
|
|
55
|
+
#
|
|
56
|
+
# A stream whose parts are both over, read to the end or reset, and
|
|
57
|
+
# acknowledged to the end or reset and acknowledged, is let go: frames
|
|
58
|
+
# for it are ignored and a read gives its end. One of the peer's makes
|
|
59
|
+
# room for another, so that the peer can keep as many open at once as
|
|
60
|
+
# the limit first given; MAX_STREAMS tells it when the room not told
|
|
61
|
+
# yet is an eighth of that limit, at least one (flow control's design,
|
|
62
|
+
# "Stream Limits"). STREAMS_BLOCKED does not hurry it, as a peer always
|
|
63
|
+
# at the limit would otherwise get MAX_STREAMS for every stream or two
|
|
64
|
+
# that ends, as quinn and quic-go do not.
|
|
65
|
+
class Streams
|
|
66
|
+
# The low bits of a stream ID: who opened it, and which way it goes.
|
|
67
|
+
SERVER_INITIATED = 0x01
|
|
68
|
+
UNIDIRECTIONAL = 0x02
|
|
69
|
+
# How much wider than a stream's window the connection's is kept.
|
|
70
|
+
CONNECTION_WINDOW_FACTOR = 1.5
|
|
71
|
+
# The longest a limit takes as a varint: a credit frame is put together
|
|
72
|
+
# only where it fits with that, as its value is fixed as it goes.
|
|
73
|
+
MAX_VARINT_SIZE = 8
|
|
74
|
+
# The share of the streams the peer may have open at once that, not
|
|
75
|
+
# told yet, makes MAX_STREAMS due.
|
|
76
|
+
STREAM_CREDIT_SHARE = 8
|
|
77
|
+
DIRECTIONS = %i[bidi uni].freeze
|
|
78
|
+
|
|
79
|
+
# The connection's receive window.
|
|
80
|
+
attr_reader :window
|
|
81
|
+
# The holes the data received may have, all the streams together.
|
|
82
|
+
attr_reader :max_gaps
|
|
83
|
+
# The stream data not sent or not acknowledged.
|
|
84
|
+
attr_reader :buffered
|
|
85
|
+
# How many BLOCKED frames the peer's limits made this end send, not
|
|
86
|
+
# counting those sent again, and how many the peer sent.
|
|
87
|
+
attr_reader :blocked_sent
|
|
88
|
+
attr_reader :blocked_received
|
|
89
|
+
|
|
90
|
+
# Events go to `events`.
|
|
91
|
+
def initialize(role:, config:, events:, now:, tracer: nil)
|
|
92
|
+
@role = role
|
|
93
|
+
@config = config
|
|
94
|
+
@events = events
|
|
95
|
+
@tracer = tracer
|
|
96
|
+
@window = ReceiveWindow.new(size: config.initial_max_data, max: config.max_receive_window, now:)
|
|
97
|
+
@receivers = {} #: Hash[Integer, Receiver]
|
|
98
|
+
@senders = {} #: Hash[Integer, Sender]
|
|
99
|
+
# How many streams of each type (the low two bits of an ID) are open.
|
|
100
|
+
@opened = [0, 0, 0, 0]
|
|
101
|
+
@gaps = 0
|
|
102
|
+
@max_gaps = config.max_receive_window / config.initial_max_datagram_size
|
|
103
|
+
@peer_parameters = nil #: TransportParameters?
|
|
104
|
+
# What the peer allows this end to send on the connection, and how
|
|
105
|
+
# much it has written; the bytes not acknowledged, of all the streams.
|
|
106
|
+
@peer_max_data = 0
|
|
107
|
+
@written = 0
|
|
108
|
+
@buffered = 0
|
|
109
|
+
# How many streams of each direction the peer allows this end to open.
|
|
110
|
+
@peer_max_streams = { bidi: 0, uni: 0 }
|
|
111
|
+
# The streams with something to send, in the order they take turns,
|
|
112
|
+
# and those written less than asked.
|
|
113
|
+
@scheduler = Scheduler.new
|
|
114
|
+
@wanting = [] #: Array[Integer]
|
|
115
|
+
# The limits a DATA_BLOCKED and a STREAMS_BLOCKED of each direction
|
|
116
|
+
# are due or went for, and which are due.
|
|
117
|
+
@data_blocked_at = nil #: Integer?
|
|
118
|
+
@data_blocked_due = false
|
|
119
|
+
@streams_blocked_at = { bidi: nil, uni: nil } #: Hash[:bidi | :uni, Integer?]
|
|
120
|
+
@streams_blocked_due = { bidi: false, uni: false }
|
|
121
|
+
# How many streams of each direction the peer may open, as last told;
|
|
122
|
+
# how many of its streams are over; and whether MAX_STREAMS goes
|
|
123
|
+
# again.
|
|
124
|
+
@local_max_streams = { bidi: config.initial_max_streams_bidi, uni: config.initial_max_streams_uni }
|
|
125
|
+
@ended = { bidi: 0, uni: 0 }
|
|
126
|
+
@max_streams_resend = { bidi: false, uni: false }
|
|
127
|
+
@blocked_sent = 0
|
|
128
|
+
@blocked_received = 0
|
|
129
|
+
# Whether the peer's credit blocks the connection, and the streams it
|
|
130
|
+
# blocks.
|
|
131
|
+
@connection_blocked = false
|
|
132
|
+
@flow_blocked = [] #: Array[Integer]
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# The peer's transport parameters, which set the limits of what this
|
|
136
|
+
# end sends.
|
|
137
|
+
# The peer's limits: a client's remembered ones first, for 0-RTT, then
|
|
138
|
+
# the server's, which raise those of the streams opened by then (RFC
|
|
139
|
+
# 9000 "Values of Transport Parameters for 0-RTT").
|
|
140
|
+
def peer_parameters=(parameters)
|
|
141
|
+
@peer_parameters = parameters
|
|
142
|
+
@peer_max_data = [parameters.initial_max_data, @peer_max_data].max
|
|
143
|
+
@peer_max_streams = { bidi: parameters.initial_max_streams_bidi, uni: parameters.initial_max_streams_uni }
|
|
144
|
+
own = @role == :server ? SERVER_INITIATED : 0
|
|
145
|
+
@senders.each do |stream_id, sender|
|
|
146
|
+
next unless stream_id & SERVER_INITIATED == own
|
|
147
|
+
sender.raise_limit(stream_id & UNIDIRECTIONAL == 0 ? parameters.initial_max_stream_data_bidi_remote : parameters.initial_max_stream_data_uni)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# A client's 0-RTT was rejected (RFC 9001 "Accepting and Rejecting
|
|
152
|
+
# 0-RTT"): the streams this end opened are gone with what they wrote,
|
|
153
|
+
# which went in 0-RTT packets, and their IDs are given again from 0.
|
|
154
|
+
# The peer opened none yet. The limits the session remembered no longer
|
|
155
|
+
# hold, and the peer's own come with its transport parameters: a server
|
|
156
|
+
# that rejects 0-RTT may allow less, as may the server of another
|
|
157
|
+
# version a Version Negotiation packet sends the client to.
|
|
158
|
+
def roll_back_own_streams
|
|
159
|
+
own = @role == :server ? SERVER_INITIATED : 0
|
|
160
|
+
gone = (@senders.keys | @receivers.keys).select { |stream_id| stream_id & SERVER_INITIATED == own }
|
|
161
|
+
gone.each do |stream_id|
|
|
162
|
+
@senders.delete(stream_id)
|
|
163
|
+
@receivers.delete(stream_id)
|
|
164
|
+
@scheduler.forget(stream_id)
|
|
165
|
+
@wanting.delete(stream_id)
|
|
166
|
+
@flow_blocked.delete(stream_id)
|
|
167
|
+
end
|
|
168
|
+
@opened[own] = 0
|
|
169
|
+
@opened[own | UNIDIRECTIONAL] = 0
|
|
170
|
+
@peer_parameters = nil
|
|
171
|
+
@peer_max_data = 0
|
|
172
|
+
@peer_max_streams = { bidi: 0, uni: 0 }
|
|
173
|
+
@written = 0
|
|
174
|
+
@buffered = 0
|
|
175
|
+
@connection_blocked = false
|
|
176
|
+
@data_blocked_at = nil
|
|
177
|
+
@data_blocked_due = false
|
|
178
|
+
@streams_blocked_at = { bidi: nil, uni: nil }
|
|
179
|
+
@streams_blocked_due = { bidi: false, uni: false }
|
|
180
|
+
nil
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# Opens a stream of this end going `direction`, :bidi or :uni: its ID,
|
|
184
|
+
# or nil when the peer allows no more.
|
|
185
|
+
def open(direction, now:)
|
|
186
|
+
type = (@role == :server ? SERVER_INITIATED : 0) | (direction == :uni ? UNIDIRECTIONAL : 0)
|
|
187
|
+
index = @opened.fetch(type)
|
|
188
|
+
limit = @peer_max_streams.fetch(direction)
|
|
189
|
+
return streams_blocked(direction, limit) if index >= limit
|
|
190
|
+
stream_id = index << 2 | type
|
|
191
|
+
@opened[type] = index + 1
|
|
192
|
+
peer = @peer_parameters or raise Error::StateError, "the peer's transport parameters have not come"
|
|
193
|
+
if direction == :uni
|
|
194
|
+
@senders[stream_id] = Sender.new(stream_id:, limit: peer.initial_max_stream_data_uni)
|
|
195
|
+
else
|
|
196
|
+
@senders[stream_id] = Sender.new(stream_id:, limit: peer.initial_max_stream_data_bidi_remote)
|
|
197
|
+
window = ReceiveWindow.new(size: @config.initial_max_stream_data_bidi_local, max: @config.max_stream_receive_window, now:)
|
|
198
|
+
@receivers[stream_id] = Receiver.new(window:)
|
|
199
|
+
end
|
|
200
|
+
stream_id
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# Writes as much of `data` to `stream_id` as there is room for, and
|
|
204
|
+
# the end of the stream with `fin` when all of it fits: how many bytes
|
|
205
|
+
# were taken.
|
|
206
|
+
def write(stream_id, data, fin:, now:)
|
|
207
|
+
sender = sender_for(stream_id)
|
|
208
|
+
accepted = [data.bytesize, room_for(sender)].min
|
|
209
|
+
whole = accepted == data.bytesize
|
|
210
|
+
taken = data.byteslice(0, accepted) #: String
|
|
211
|
+
sender.write(taken, fin: fin && whole)
|
|
212
|
+
@written += accepted
|
|
213
|
+
@buffered += accepted
|
|
214
|
+
@scheduler.ready(stream_id)
|
|
215
|
+
unless whole
|
|
216
|
+
@wanting << stream_id unless @wanting.include?(stream_id)
|
|
217
|
+
stream_blocked(stream_id, now) if sender.credit.zero?
|
|
218
|
+
data_blocked(now) if @written == @peer_max_data
|
|
219
|
+
end
|
|
220
|
+
accepted
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# The application resets `stream_id` with `error_code`.
|
|
224
|
+
def reset(stream_id, error_code)
|
|
225
|
+
sender = @senders[stream_id]
|
|
226
|
+
return if sender.nil? && over?(stream_id) && sends?(stream_id)
|
|
227
|
+
sender or raise Error::StreamError, "stream #{stream_id} has nothing to write to"
|
|
228
|
+
abandon_sending(sender, error_code)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# The application sends `stream_id` with `urgency` and as `incremental`
|
|
232
|
+
# (RFC 9218), as the Scheduler orders the streams.
|
|
233
|
+
def prioritize(stream_id, urgency:, incremental:)
|
|
234
|
+
sender = @senders[stream_id]
|
|
235
|
+
return if sender.nil? && over?(stream_id) && sends?(stream_id)
|
|
236
|
+
sender or raise Error::StreamError, "stream #{stream_id} has nothing to write to"
|
|
237
|
+
@scheduler.prioritize(stream_id, urgency:, incremental:)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# The application stops reading `stream_id`, asking the peer with
|
|
241
|
+
# `error_code` to stop sending.
|
|
242
|
+
def stop_sending(stream_id, error_code)
|
|
243
|
+
receiver = @receivers[stream_id]
|
|
244
|
+
return if receiver.nil? && over?(stream_id) && receives?(stream_id)
|
|
245
|
+
receiver or raise Error::StreamError, "stream #{stream_id} has nothing to read"
|
|
246
|
+
gaps = receiver.gaps
|
|
247
|
+
consumed = receiver.stop!(error_code)
|
|
248
|
+
@gaps -= gaps - receiver.gaps
|
|
249
|
+
@window.consume(consumed)
|
|
250
|
+
retire(stream_id)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# The peer reset a stream: checked as the end of data is, and the
|
|
254
|
+
# application hears of it once.
|
|
255
|
+
def receive_reset_stream(frame, now:)
|
|
256
|
+
type = Wire::Frame::ResetStream::TYPES.fetch(0)
|
|
257
|
+
receiver = receiver_for(frame.stream_id, frame_type: type, now:) or return
|
|
258
|
+
grown = receiver.admit(frame.final_size, fin: true, frame_type: type)
|
|
259
|
+
@window.receive(@window.received + grown, frame_type: type)
|
|
260
|
+
return if receiver.reset?
|
|
261
|
+
gaps = receiver.gaps
|
|
262
|
+
@window.consume(receiver.reset!(frame.final_size))
|
|
263
|
+
@gaps -= gaps
|
|
264
|
+
@events << Event::StreamReset.new(stream_id: frame.stream_id, error_code: frame.error_code, final_size: frame.final_size)
|
|
265
|
+
retire(frame.stream_id)
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# The peer cannot open a stream past the limit told; MAX_STREAMS keeps
|
|
269
|
+
# to its threshold all the same.
|
|
270
|
+
def receive_streams_blocked(frame)
|
|
271
|
+
@blocked_received += 1
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
# The peer asks this end to stop sending a stream: it is reset with
|
|
275
|
+
# the same error code, and the application hears of it.
|
|
276
|
+
def receive_stop_sending(frame, now:)
|
|
277
|
+
sender = sender_part(frame.stream_id, frame_type: Wire::Frame::StopSending::TYPES.fetch(0), now:) or return
|
|
278
|
+
@events << Event::StopSending.new(stream_id: frame.stream_id, error_code: frame.error_code)
|
|
279
|
+
abandon_sending(sender, frame.error_code)
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# The next RESET_STREAM or STOP_SENDING due and its duty, in no more
|
|
283
|
+
# than `room` bytes; nil when none is or it does not fit.
|
|
284
|
+
def next_abandon(room)
|
|
285
|
+
@senders.each do |stream_id, sender|
|
|
286
|
+
code = sender.reset_due or next
|
|
287
|
+
frame = Wire::Frame::ResetStream.new(stream_id:, error_code: code, final_size: sender.final_size)
|
|
288
|
+
return nil unless fits?(frame, room)
|
|
289
|
+
sender.reset_sent
|
|
290
|
+
return [frame, Recovery::Duty::ResetStream.new(stream_id:)]
|
|
291
|
+
end
|
|
292
|
+
@receivers.each do |stream_id, receiver|
|
|
293
|
+
code = receiver.stop_due or next
|
|
294
|
+
frame = Wire::Frame::StopSending.new(stream_id:, error_code: code)
|
|
295
|
+
return nil unless fits?(frame, room)
|
|
296
|
+
receiver.stop_sent
|
|
297
|
+
return [frame, Recovery::Duty::StopSending.new(stream_id:)]
|
|
298
|
+
end
|
|
299
|
+
nil
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def reset_stream_acked(duty)
|
|
303
|
+
sender = @senders[duty.stream_id] or return
|
|
304
|
+
sender.reset_acked
|
|
305
|
+
retire(duty.stream_id)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def reset_stream_lost(duty) = @senders[duty.stream_id]&.reset_lost
|
|
309
|
+
|
|
310
|
+
def stop_sending_lost(duty) = @receivers[duty.stream_id]&.stop_lost
|
|
311
|
+
|
|
312
|
+
# The peer's limits grew: what they now let through can be written.
|
|
313
|
+
def receive_max_data(frame, now:)
|
|
314
|
+
return unless frame.maximum > @peer_max_data
|
|
315
|
+
@peer_max_data = frame.maximum
|
|
316
|
+
if @connection_blocked
|
|
317
|
+
@connection_blocked = false
|
|
318
|
+
@tracer&.connection_data_blocked_updated(old: :blocked, new: :unblocked, reason: :connection_flow_control, now:)
|
|
319
|
+
end
|
|
320
|
+
writable_again
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def receive_max_stream_data(frame, now:)
|
|
324
|
+
sender = sender_part(frame.stream_id, frame_type: Wire::Frame::MaxStreamData::TYPES.fetch(0), now:) or return
|
|
325
|
+
return unless sender.raise_limit(frame.maximum)
|
|
326
|
+
if @flow_blocked.delete(frame.stream_id)
|
|
327
|
+
@tracer&.stream_data_blocked_updated(stream_id: frame.stream_id, old: :blocked, new: :unblocked, reason: :stream_flow_control, now:)
|
|
328
|
+
end
|
|
329
|
+
writable_again
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
def receive_max_streams(frame)
|
|
333
|
+
direction = frame.bidirectional ? :bidi : :uni #: :bidi | :uni
|
|
334
|
+
@peer_max_streams[direction] = frame.maximum if frame.maximum > @peer_max_streams.fetch(direction)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# The peer is blocked by the connection's limit or a stream's: credit
|
|
338
|
+
# not told yet goes at once.
|
|
339
|
+
def receive_data_blocked(frame)
|
|
340
|
+
@blocked_received += 1
|
|
341
|
+
@window.blocked!
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def receive_stream_data_blocked(frame, now:)
|
|
345
|
+
@blocked_received += 1
|
|
346
|
+
receiver = receiver_for(frame.stream_id, frame_type: Wire::Frame::StreamDataBlocked::TYPES.fetch(0), now:) or return
|
|
347
|
+
receiver.window.blocked!
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
# The next DATA_BLOCKED, STREAMS_BLOCKED or STREAM_DATA_BLOCKED due and
|
|
351
|
+
# its duty, in no more than `room` bytes; nil when none is or it does
|
|
352
|
+
# not fit.
|
|
353
|
+
def next_blocked(room)
|
|
354
|
+
if @data_blocked_due
|
|
355
|
+
frame = Wire::Frame::DataBlocked.new(maximum: @peer_max_data)
|
|
356
|
+
return nil unless fits?(frame, room)
|
|
357
|
+
@data_blocked_due = false
|
|
358
|
+
return [frame, Recovery::Duty::DataBlocked.new(maximum: frame.maximum)]
|
|
359
|
+
end
|
|
360
|
+
@streams_blocked_due.each do |direction, due|
|
|
361
|
+
next unless due
|
|
362
|
+
bidirectional = direction == :bidi
|
|
363
|
+
frame = Wire::Frame::StreamsBlocked.new(bidirectional:, maximum: @peer_max_streams.fetch(direction))
|
|
364
|
+
return nil unless fits?(frame, room)
|
|
365
|
+
@streams_blocked_due[direction] = false
|
|
366
|
+
return [frame, Recovery::Duty::StreamsBlocked.new(bidirectional:, maximum: frame.maximum)]
|
|
367
|
+
end
|
|
368
|
+
@senders.each do |stream_id, sender|
|
|
369
|
+
next unless sender.blocked_due?
|
|
370
|
+
frame = Wire::Frame::StreamDataBlocked.new(stream_id:, maximum: sender.limit)
|
|
371
|
+
return nil unless fits?(frame, room)
|
|
372
|
+
sender.blocked_sent
|
|
373
|
+
return [frame, Recovery::Duty::StreamDataBlocked.new(stream_id:, maximum: frame.maximum)]
|
|
374
|
+
end
|
|
375
|
+
nil
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
# A BLOCKED frame was lost: it goes again while its limit still holds
|
|
379
|
+
# this end back.
|
|
380
|
+
def data_blocked_lost(duty)
|
|
381
|
+
@data_blocked_due = true if duty.maximum == @peer_max_data && @written == @peer_max_data
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
def stream_data_blocked_lost(duty) = @senders[duty.stream_id]&.blocked_lost(duty.maximum)
|
|
385
|
+
|
|
386
|
+
def streams_blocked_lost(duty)
|
|
387
|
+
direction = duty.bidirectional ? :bidi : :uni #: :bidi | :uni
|
|
388
|
+
limit = @peer_max_streams.fetch(direction)
|
|
389
|
+
type = (@role == :server ? SERVER_INITIATED : 0) | (duty.bidirectional ? 0 : UNIDIRECTIONAL)
|
|
390
|
+
@streams_blocked_due[direction] = true if duty.maximum == limit && @opened.fetch(type) >= limit
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
# How many bytes a write to `stream_id` would take now.
|
|
394
|
+
def writable_bytes(stream_id) = room_for(sender_for(stream_id))
|
|
395
|
+
|
|
396
|
+
# The streams a write would take something on.
|
|
397
|
+
def writable = @senders.filter_map { |stream_id, sender| stream_id if !sender.fin? && !sender.reset? && room_for(sender).positive? }
|
|
398
|
+
|
|
399
|
+
# The next STREAM frame and its duty in no more than `room` bytes, from
|
|
400
|
+
# the next stream in turn with something to send; nil when none has,
|
|
401
|
+
# or the frame does not fit.
|
|
402
|
+
def next_stream_frame(room)
|
|
403
|
+
sender = current_sender or return nil
|
|
404
|
+
pair = sender.next_frame(room) or return nil
|
|
405
|
+
@scheduler.advance
|
|
406
|
+
pair
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
# The urgency of the next stream in turn with something to send; nil
|
|
410
|
+
# when none has.
|
|
411
|
+
def urgency
|
|
412
|
+
@scheduler.urgency if current_sender
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
# Data sent was acknowledged: the bytes let go leave room in the send
|
|
416
|
+
# buffer.
|
|
417
|
+
def stream_data_acked(duty)
|
|
418
|
+
sender = @senders[duty.stream_id] or return
|
|
419
|
+
@buffered -= sender.acked(duty.offset, duty.length, duty.fin)
|
|
420
|
+
writable_again
|
|
421
|
+
retire(duty.stream_id)
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
# Data sent was lost: it goes again.
|
|
425
|
+
def stream_data_lost(duty)
|
|
426
|
+
sender = @senders[duty.stream_id] or return
|
|
427
|
+
sender.lost(duty.offset, duty.length, duty.fin)
|
|
428
|
+
@scheduler.ready(duty.stream_id)
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
# Takes a STREAM frame: its data is kept, once every check passes.
|
|
432
|
+
def receive_stream(frame, now:)
|
|
433
|
+
type = Wire::Frame::Stream::TYPES.fetch(0)
|
|
434
|
+
receiver = receiver_for(frame.stream_id, frame_type: type, now:) or return
|
|
435
|
+
length = frame.data.bytesize
|
|
436
|
+
# Before the end is admitted: a FIN with no data makes the stream
|
|
437
|
+
# readable by the end alone.
|
|
438
|
+
readable = receiver.readable?
|
|
439
|
+
grown = receiver.admit(frame.offset + length, fin: frame.fin, frame_type: type)
|
|
440
|
+
@window.receive(@window.received + grown, frame_type: type)
|
|
441
|
+
if receiver.abandoned?
|
|
442
|
+
@window.consume(receiver.discard)
|
|
443
|
+
return retire(frame.stream_id)
|
|
444
|
+
end
|
|
445
|
+
gaps = @gaps - receiver.gaps + receiver.gaps_with(frame.offset, length)
|
|
446
|
+
if gaps > @max_gaps
|
|
447
|
+
raise Violation.new("data received in more than #{@max_gaps} pieces", code: ErrorCode::INTERNAL_ERROR, frame_type: type)
|
|
448
|
+
end
|
|
449
|
+
@gaps = gaps
|
|
450
|
+
receiver.insert(frame.offset, frame.data)
|
|
451
|
+
@events << Event::StreamReadable.new(stream_id: frame.stream_id) if !readable && receiver.readable?
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
# Up to `max_bytes` of `stream_id`, as Receiver#read gives them,
|
|
455
|
+
# consumed in the connection's window as in the stream's.
|
|
456
|
+
def read(stream_id, max_bytes)
|
|
457
|
+
receiver = @receivers[stream_id]
|
|
458
|
+
return nil if receiver.nil? && over?(stream_id) && receives?(stream_id)
|
|
459
|
+
receiver or raise Error::StreamError, "stream #{stream_id} has nothing to read"
|
|
460
|
+
data = receiver.read(max_bytes)
|
|
461
|
+
@window.consume(data.bytesize) if data
|
|
462
|
+
retire(stream_id)
|
|
463
|
+
data
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
# The streams a read would give something on.
|
|
467
|
+
def readable
|
|
468
|
+
@receivers.filter_map { |stream_id, receiver| stream_id if receiver.readable? }
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
# The next MAX_DATA, MAX_STREAMS or MAX_STREAM_DATA frame due and its
|
|
472
|
+
# duty, in no more than `room` bytes; nil when none is. `rtt` is the smoothed RTT,
|
|
473
|
+
# nil before the first sample. A stream whose final size is known needs
|
|
474
|
+
# no more credit.
|
|
475
|
+
def next_credit(room, now:, rtt:)
|
|
476
|
+
if @window.update_due?
|
|
477
|
+
return nil if room < 1 + MAX_VARINT_SIZE
|
|
478
|
+
maximum = @window.advance(now:, rtt:)
|
|
479
|
+
return [Wire::Frame::MaxData.new(maximum:), Recovery::Duty::MaxData.new(maximum:)]
|
|
480
|
+
end
|
|
481
|
+
DIRECTIONS.each do |direction|
|
|
482
|
+
next unless max_streams_due?(direction)
|
|
483
|
+
return nil if room < 1 + MAX_VARINT_SIZE
|
|
484
|
+
maximum = @local_max_streams[direction] = streams_allowed(direction)
|
|
485
|
+
@max_streams_resend[direction] = false
|
|
486
|
+
bidirectional = direction == :bidi
|
|
487
|
+
return [Wire::Frame::MaxStreams.new(bidirectional:, maximum:), Recovery::Duty::MaxStreams.new(bidirectional:, maximum:)]
|
|
488
|
+
end
|
|
489
|
+
@receivers.each do |stream_id, receiver|
|
|
490
|
+
window = receiver.window
|
|
491
|
+
next unless window.update_due? && receiver.final_size.nil? && !receiver.abandoned?
|
|
492
|
+
return nil if room < 1 + Wire::Varint.size(stream_id) + MAX_VARINT_SIZE
|
|
493
|
+
size = window.size
|
|
494
|
+
maximum = window.advance(now:, rtt:)
|
|
495
|
+
@window.grow_to((window.size * CONNECTION_WINDOW_FACTOR).ceil, now:) if window.size > size
|
|
496
|
+
return [Wire::Frame::MaxStreamData.new(stream_id:, maximum:), Recovery::Duty::MaxStreamData.new(stream_id:, maximum:)]
|
|
497
|
+
end
|
|
498
|
+
nil
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
def max_data_lost(duty) = @window.lost(duty.maximum)
|
|
502
|
+
|
|
503
|
+
def max_stream_data_lost(duty) = @receivers[duty.stream_id]&.window&.lost(duty.maximum)
|
|
504
|
+
|
|
505
|
+
# A lost MAX_STREAMS goes again while it is the latest.
|
|
506
|
+
def max_streams_lost(duty)
|
|
507
|
+
direction = duty.bidirectional ? :bidi : :uni #: :bidi | :uni
|
|
508
|
+
@max_streams_resend[direction] = true if duty.maximum == @local_max_streams.fetch(direction)
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
# How many streams of `direction` the peer may open by now: as many
|
|
512
|
+
# as are over, and as many again as the limit first given, short of
|
|
513
|
+
# 2^60.
|
|
514
|
+
private def streams_allowed(direction)
|
|
515
|
+
initial = direction == :bidi ? @config.initial_max_streams_bidi : @config.initial_max_streams_uni
|
|
516
|
+
[@ended.fetch(direction) + initial, Wire::Frame::MAX_STREAM_COUNT].min
|
|
517
|
+
end
|
|
518
|
+
|
|
519
|
+
private def max_streams_due?(direction)
|
|
520
|
+
return true if @max_streams_resend.fetch(direction)
|
|
521
|
+
room = streams_allowed(direction) - @local_max_streams.fetch(direction)
|
|
522
|
+
return false unless room.positive?
|
|
523
|
+
initial = direction == :bidi ? @config.initial_max_streams_bidi : @config.initial_max_streams_uni
|
|
524
|
+
room >= [initial / STREAM_CREDIT_SHARE, 1].max
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
# `stream_id` is let go once both its parts are over; one of the
|
|
528
|
+
# peer's makes room for another.
|
|
529
|
+
private def retire(stream_id)
|
|
530
|
+
receiver = @receivers[stream_id]
|
|
531
|
+
sender = @senders[stream_id]
|
|
532
|
+
return if receiver && !receiver.finished?
|
|
533
|
+
return if sender && !(sender.all_acked? || sender.reset_acked?)
|
|
534
|
+
@receivers.delete(stream_id)
|
|
535
|
+
@senders.delete(stream_id)
|
|
536
|
+
@scheduler.forget(stream_id)
|
|
537
|
+
@wanting.delete(stream_id)
|
|
538
|
+
@flow_blocked.delete(stream_id)
|
|
539
|
+
@ended[stream_id & UNIDIRECTIONAL == 0 ? :bidi : :uni] += 1 unless local?(stream_id)
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
# Whether `stream_id` has been opened, and so, when it is not held,
|
|
543
|
+
# is over.
|
|
544
|
+
private def over?(stream_id) = stream_id >> 2 < @opened.fetch(stream_id & 0x3)
|
|
545
|
+
|
|
546
|
+
# Whether `stream_id` has a receiving part on this end, or a sending
|
|
547
|
+
# part.
|
|
548
|
+
private def receives?(stream_id) = !(local?(stream_id) && stream_id & UNIDIRECTIONAL != 0)
|
|
549
|
+
|
|
550
|
+
private def sends?(stream_id) = !(!local?(stream_id) && stream_id & UNIDIRECTIONAL != 0)
|
|
551
|
+
|
|
552
|
+
# The connection's credit cut a write short.
|
|
553
|
+
private def data_blocked(now)
|
|
554
|
+
unless @connection_blocked
|
|
555
|
+
@connection_blocked = true
|
|
556
|
+
@tracer&.connection_data_blocked_updated(old: :unblocked, new: :blocked, reason: :connection_flow_control, now:)
|
|
557
|
+
end
|
|
558
|
+
return if @data_blocked_at == @peer_max_data
|
|
559
|
+
@data_blocked_at = @peer_max_data
|
|
560
|
+
@data_blocked_due = true
|
|
561
|
+
@blocked_sent += 1
|
|
562
|
+
end
|
|
563
|
+
|
|
564
|
+
# The stream's credit cut a write to `stream_id` short.
|
|
565
|
+
private def stream_blocked(stream_id, now)
|
|
566
|
+
sender = @senders.fetch(stream_id)
|
|
567
|
+
@blocked_sent += 1 if sender.blocked!
|
|
568
|
+
return if @flow_blocked.include?(stream_id)
|
|
569
|
+
@flow_blocked << stream_id
|
|
570
|
+
@tracer&.stream_data_blocked_updated(stream_id:, old: :unblocked, new: :blocked, reason: :stream_flow_control, now:)
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
# No stream of `direction` opens past the peer's `limit`: nil, and
|
|
574
|
+
# STREAMS_BLOCKED with it, once for each value of the limit.
|
|
575
|
+
private def streams_blocked(direction, limit)
|
|
576
|
+
return nil if @streams_blocked_at.fetch(direction) == limit
|
|
577
|
+
@streams_blocked_at[direction] = limit
|
|
578
|
+
@streams_blocked_due[direction] = true
|
|
579
|
+
@blocked_sent += 1
|
|
580
|
+
nil
|
|
581
|
+
end
|
|
582
|
+
|
|
583
|
+
private def fits?(frame, room) = Wire::Frame.encode([frame], ack_delay_exponent: 0).bytesize <= room
|
|
584
|
+
|
|
585
|
+
# The sending part of `stream_id` for a frame of `frame_type`, opening
|
|
586
|
+
# the peer's streams up to it; nil when the stream is over, and a
|
|
587
|
+
# Violation when it has no sending part or cannot be open.
|
|
588
|
+
private def sender_part(stream_id, frame_type:, now:)
|
|
589
|
+
if !local?(stream_id) && stream_id & UNIDIRECTIONAL != 0
|
|
590
|
+
raise Violation.new("stream #{stream_id} only receives", code: ErrorCode::STREAM_STATE_ERROR, frame_type:)
|
|
591
|
+
end
|
|
592
|
+
open_peer_streams(stream_id, frame_type:, now:) unless local?(stream_id)
|
|
593
|
+
check_opened(stream_id, frame_type:)
|
|
594
|
+
@senders[stream_id]
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
# The sending part of `stream_id` for the application to write to.
|
|
598
|
+
private def sender_for(stream_id)
|
|
599
|
+
sender = @senders[stream_id] or raise Error::StreamError, "stream #{stream_id} has nothing to write to"
|
|
600
|
+
raise Error::StreamError, "stream #{stream_id} has ended" if sender.fin? || sender.reset?
|
|
601
|
+
sender
|
|
602
|
+
end
|
|
603
|
+
|
|
604
|
+
# A stream's sending part is reset: the data it held leaves the send
|
|
605
|
+
# buffer, and what it never sent the connection's count.
|
|
606
|
+
private def abandon_sending(sender, error_code)
|
|
607
|
+
released, unsent = sender.reset!(error_code)
|
|
608
|
+
@buffered -= released
|
|
609
|
+
@written -= unsent
|
|
610
|
+
writable_again
|
|
611
|
+
end
|
|
612
|
+
|
|
613
|
+
# What the stream's credit, the connection's and the send buffer all
|
|
614
|
+
# leave room for.
|
|
615
|
+
private def room_for(sender) = [sender.credit, @peer_max_data - @written, @config.max_send_buffer - @buffered].min
|
|
616
|
+
|
|
617
|
+
# The sending part of the stream whose turn it is, those with nothing
|
|
618
|
+
# to send let go idle on the way; nil when none has something.
|
|
619
|
+
private def current_sender
|
|
620
|
+
while (stream_id = @scheduler.current)
|
|
621
|
+
sender = @senders[stream_id]
|
|
622
|
+
return sender if sender&.pending?
|
|
623
|
+
@scheduler.idle(stream_id)
|
|
624
|
+
end
|
|
625
|
+
nil
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
# The streams written less than asked that can take some now hear so;
|
|
629
|
+
# one that has ended since is let go.
|
|
630
|
+
private def writable_again
|
|
631
|
+
@wanting.reject! do |stream_id|
|
|
632
|
+
sender = @senders[stream_id]
|
|
633
|
+
next true if sender.nil? || sender.fin? || sender.reset?
|
|
634
|
+
next false unless room_for(sender).positive?
|
|
635
|
+
@events << Event::StreamWritable.new(stream_id:)
|
|
636
|
+
true
|
|
637
|
+
end
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
# Whether `stream_id` was opened by this end.
|
|
641
|
+
def local?(stream_id) = (stream_id & SERVER_INITIATED == SERVER_INITIATED) == (@role == :server)
|
|
642
|
+
|
|
643
|
+
# The receiving part of `stream_id` for a frame of `frame_type`, opening
|
|
644
|
+
# the peer's streams up to it; nil when the stream is over, and a
|
|
645
|
+
# Violation when it has no receiving part or cannot be open.
|
|
646
|
+
private def receiver_for(stream_id, frame_type:, now:)
|
|
647
|
+
if local?(stream_id) && stream_id & UNIDIRECTIONAL != 0
|
|
648
|
+
raise Violation.new("stream #{stream_id} only sends", code: ErrorCode::STREAM_STATE_ERROR, frame_type:)
|
|
649
|
+
end
|
|
650
|
+
open_peer_streams(stream_id, frame_type:, now:) unless local?(stream_id)
|
|
651
|
+
check_opened(stream_id, frame_type:)
|
|
652
|
+
@receivers[stream_id]
|
|
653
|
+
end
|
|
654
|
+
|
|
655
|
+
# A stream of this end must have been opened.
|
|
656
|
+
private def check_opened(stream_id, frame_type:)
|
|
657
|
+
return unless local?(stream_id) && stream_id >> 2 >= @opened.fetch(stream_id & 0x3)
|
|
658
|
+
raise Violation.new("stream #{stream_id} is not open", code: ErrorCode::STREAM_STATE_ERROR, frame_type:)
|
|
659
|
+
end
|
|
660
|
+
|
|
661
|
+
# The peer's streams of the type of `stream_id`, up to it, open, within
|
|
662
|
+
# the limit given (RFC 9000 "Controlling Concurrency").
|
|
663
|
+
private def open_peer_streams(stream_id, frame_type:, now:)
|
|
664
|
+
type = stream_id & 0x3
|
|
665
|
+
index = stream_id >> 2
|
|
666
|
+
return if index < @opened.fetch(type)
|
|
667
|
+
limit = @local_max_streams.fetch(type & UNIDIRECTIONAL == 0 ? :bidi : :uni)
|
|
668
|
+
if index >= limit
|
|
669
|
+
raise Violation.new("stream #{stream_id} past the limit of #{limit}", code: ErrorCode::STREAM_LIMIT_ERROR, frame_type:)
|
|
670
|
+
end
|
|
671
|
+
(@opened.fetch(type)..index).each { |each_index| open_peer_stream(each_index << 2 | type, now) }
|
|
672
|
+
@opened[type] = index + 1
|
|
673
|
+
end
|
|
674
|
+
|
|
675
|
+
private def open_peer_stream(stream_id, now)
|
|
676
|
+
unidirectional = stream_id & UNIDIRECTIONAL != 0
|
|
677
|
+
size = unidirectional ? @config.initial_max_stream_data_uni : @config.initial_max_stream_data_bidi_remote
|
|
678
|
+
@receivers[stream_id] = Receiver.new(window: ReceiveWindow.new(size:, max: @config.max_stream_receive_window, now:))
|
|
679
|
+
return if unidirectional
|
|
680
|
+
@senders[stream_id] = Sender.new(stream_id:, limit: @peer_parameters&.initial_max_stream_data_bidi_local || 0)
|
|
681
|
+
end
|
|
682
|
+
end
|
|
683
|
+
end
|
|
684
|
+
|
|
685
|
+
require_relative "streams/receive_window"
|
|
686
|
+
require_relative "streams/receiver"
|
|
687
|
+
require_relative "streams/sender"
|