This document outlines the architecture of the HTTP/2 connection implementation in ZIO Quartz H2, with a focus on how ZIO Queues and ZStreams are utilized for efficient data flow management.
┌─────────────────────────────────────────────────────────────────────────┐ │ QuartzH2Server │ └─────────────────────────────────────┬─────────────────────────────────────┘ │ creates ▼ ┌─────────────────────────────────────────────────────────────────────────┐ │ Http2Connection │ │ │ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ │ │ Outbound Queue │ │ Flow Control │ │ Stream Table │ │ │ │ (outq) │◄────►│ Management │◄────►│ (streamTbl) │ │ │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ │ ▲ ▲ ▲ │ │ │ │ │ │ │ ▼ ▼ ▼ │ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ │ │ Outbound Worker │ │ Packet Handler │ │ Http2Stream(s) │ │ │ │ Process │◄────►│ │◄────►│ │ │ │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ │ ▲ ▲ │ └────────────────────────────────────┼────────────────────────┼────────────┘ │ │ ▼ ▼ ┌──────────────────────────────────┐ ┌──────────────────────────┐ │ IOChannel │ │ Http Route │ └──────────────────────────────────┘ └──────────────────────────┘
ZIO Quartz H2 leverages ZIO's functional streams (ZStream) as a core abstraction for processing HTTP/2 data. Unlike traditional imperative streams, ZStream represents a description of data transformations that are executed only when the stream is run. This functional approach provides several benefits:
makePacketStream
: Creates a ZStream that reads from the IOChannel and transforms raw bytes into HTTP/2 packetspacketStreamPipe
: A ZPipeline that transforms a stream of bytes into a stream of HTTP/2 framesdataEvalEffectProducer
: Produces data from queues as a ZStream transformationThe HTTP/2 connection implementation in ZIO Quartz H2 makes extensive use of ZIO Queues for managing data flow between different components. Here are the key queues used in the system:
outq
)Queue[ByteBuffer]
Each HTTP/2 stream has several queues to manage its data flow:
inDataQ
)Queue[ByteBuffer]
outXFlowSync
)Queue[Boolean]
syncUpdateWindowQ
)Queue[Unit]
processIncoming
method sets up a ZStream pipeline starting with leftover datamakePacketStream
creates a ZStream that reads from the IOChannel and transforms raw bytes into HTTP/2 packetspacketStreamPipe
ZPipelineforeach
, which applies packet_handler
to each packetpacket_handler
methodinDataQ
headerFrame
and dataFrame
outq
queueoutBoundWorkerProc
continuously takes frames from the outq
queuetxWindow_Transmit
method handles the logic for splitting frames if necessary based on available window sizeoutXFlowSync
queue is used to signal when more data can be sent after window updatesThe main class that manages an HTTP/2 connection. It extends Http2ConnectionCommon
and implements the core HTTP/2 protocol logic.
A trait that provides common functionality for HTTP/2 connections, including methods for creating and sending frames.
Represents an individual HTTP/2 stream within a connection. Each stream has its own set of queues for managing data flow.
HTTP/2 implements flow control at two levels:
globalTransmitWindow
and globalInboundWindow
transmitWindow
and inboundWindow
ZIO Queues play a crucial role in coordinating these flow control mechanisms, ensuring efficient data transmission while preventing buffer bloat and resource exhaustion.