This document outlines the architecture of the Quartz H2 server, a high-performance HTTP/2 implementation built with Scala and Cats Effect. The architecture focuses on efficient resource management, functional stream processing, and robust connection handling for both HTTP/1.1 and HTTP/2 protocols.
Quartz H2 is a modern HTTP/2 server implementation with the following key features:
┌───────────────────────────────────────────────────────────────────────────┐
│ QuartzH2Server │
└─────────────────────────────────────┬─────────────────────────────────────┘
│ creates
▼
┌───────────────────────────────────────────────────────────────────────────┐
│ Connection Management Layer │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ TLSChannel │ │ TCPChannel │ │ IOURingChannel │ │
│ │ (ALPN, SNI) │ │ (Plain HTTP) │ │ (Linux only) │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
└───────────────────────────────┬───────────────────────────────────────────┘
│
▼
┌───────────────────────────────────────────────────────────────────────────┐
│ Protocol Handler Layer │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Http2Connection│ │ Http11Connection│ │
│ │ │ │ │ │
│ └────────┬────────┘ └────────┬────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Http2Stream(s) │ │ ChunkedTransfer │ │
│ │ │ │ │ │
│ └─────────────────┘ └─────────────────┘ │
└───────────────────────────────┬───────────────────────────────────────────┘
│
▼
┌───────────────────────────────────────────────────────────────────────────┐
│ Application Layer │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ HttpRoute │ │ WebFilter │ │ HttpRouteIO │ │
│ │ │ │ │ │ │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
└───────────────────────────────────────────────────────────────────────────┘
The architecture is organized into three main layers:
The Http2Connection class is the core component that manages HTTP/2 connections. It extends Http2ConnectionCommon which provides shared functionality for both client and server HTTP/2 connections.
┌─────────────────────────────────────────────────────────────────────────┐
│ Http2Connection │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Outbound Queue │ │ Flow Control │ │ Stream Table │ │
│ │ (outq) │◄────►│ Management │◄────►│ (streamTbl) │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
│ ▲ ▲ ▲ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Outbound Worker │ │ Packet Handler │ │ Http2Stream(s) │ │
│ │ Process │◄────►│ │◄────►│ │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
│ ▲ ▲ │
└────────────────────────────────────┼────────────────────────┼───────────┘
│ │
▼ ▼
┌──────────────────────────────────┐ ┌──────────────────────────┐
│ IOChannel │ │ Http Route │
└──────────────────────────────────┘ └──────────────────────────┘
The QuartzH2Server handles connection establishment through several methods:
Http2Connection is createdQuartz H2 leverages functional streams (fs2.Stream) as a core abstraction for processing HTTP/2 data. Unlike traditional imperative streams, these streams represent a description of data transformations that are executed only when the stream is run. This functional approach provides several benefits:
makePacketStream: Creates a Stream that reads from the IOChannel and transforms raw bytes into HTTP/2 packetspacketStreamPipe: Transforms a stream of bytes into a stream of HTTP/2 framesdataEvalEffectProducer: Produces data from queues as a Stream transformationThe implementation uses several advanced stream processing techniques:
Pull API is used to implement custom stream processing logicoutq)Queue[IO, ByteBuffer]Each HTTP/2 stream has several queues to manage its data flow:
inDataQ)Queue[IO, ByteBuffer]outXFlowSync)Queue[IO, Boolean]syncUpdateWindowQ)Queue[IO, Unit]The implementation uses several mechanisms to ensure thread-safe concurrent operations:
processIncoming method sets up a Stream pipeline starting with leftover datamakePacketStream creates a Stream that reads from the IOChannel and transforms raw bytes into HTTP/2 packetspacketStreamPipe pipelineforeach, which applies packet_handler to each packetpacket_handler methodinDataQheaderFrame and dataFrameoutq 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 updatesQuartz H2 includes a robust HTTP/1.1 implementation that serves as a fallback when HTTP/2 is not supported or negotiated. The HTTP/1.1 implementation has been enhanced with comprehensive documentation, better error handling, and improved code structure.
The HTTP/1.1 connection handler includes the following improvements:
The HTTP/1.1 chunked transfer encoding implementation has been enhanced with:
The HttpRangeRequest class has been improved with:
The main server class that manages HTTP/2 connections, handles SSL context setup, and manages incoming connections. It provides the following key functionality:
The main class that manages an HTTP/2 connection. It extends Http2ConnectionCommon and implements the core HTTP/2 protocol logic, including:
Represents an individual HTTP/2 stream within a connection. Each stream has its own set of queues for managing data flow and handles:
Handles secure connections using SSL/TLS protocols. Key features include:
HTTP/2 implements flow control at two levels:
globalTransmitWindow and globalInboundWindowtransmitWindow and inboundWindowCats Effect Queues play a crucial role in coordinating these flow control mechanisms, ensuring efficient data transmission while preventing buffer bloat and resource exhaustion.
Quartz H2 includes a client implementation for making HTTP/2 requests to servers. The QuartzH2Client provides functionality for establishing connections and sending requests.
The client supports several methods for establishing connections:
connectTLS_alpn_h2The client provides several features for handling requests:
Quartz H2 is a high-performance HTTP/2 server and client implementation built with Scala and Cats Effect. Its architecture emphasizes:
The architecture provides a solid foundation for building high-performance web applications and services with modern HTTP capabilities while maintaining code clarity and maintainability through functional programming principles.