As part of our continued requirements gathering, alongside our HTTPClient discussion, we'd like to open discussion regarding the various layers a native Swift network stack may need. This includes the points at which it would touch operating systems, interact with various protocols, and offer abstractions to developers.
From a systems perspective, it's fairly straightforward. Each layer introduces its own level of data and set of events into the stream.
Kernel and OS abstractions: sockets, queues, epoll, io_uring, libuv, Winsock2. These are the lowest level abstractions for platforms Swift supports that can actually provide network data in some form. SocketDescriptor is already under discussion to be part of swift-system, and others have existing wrappers, but we'll need virtual all of these abstractions to be properly wrapped into Swift at some point. That work is useful independent of the networking effort.
Connection and pathing: functionally combines the lower level with basic service APIs, like DNS / mDNS resolution, pathing and interface changes, and MPTCP.
Secure channels: integration with system or library TLS services, this is likely the lowest level something like QUIC could live.
Protocols and framing: parses the incoming data into the application appropriate frames, HTTP, MQTT, WebSocket, etc. Obviously Swift can't ship support for all framing, so this would be an important API layer.
Applications: HTTPClient and the like. Consumers of the framed data building protocol specific functionality on top.
Perhaps more interestingly are the design opportunities Swift has as it rises up this stack.
Each layer is an opportunity for Swift to provide testing hooks and API for custom functionality. For instance, we want developers to be able to provide their own socket-level abstraction while still hooking into the rest of the stack, while maintaining performance. We also want the higher levels to be able to test themselves with both real and fake underlying implementations.
Each layer is also an opportunity for monitoring and logging, so investigation is needed in how to do that while maintaining maximum performance. Does Swift have the tools to observe streams of ~Copyable elements without impacting the performance of those streams?
So, what layering is important to you? What do other languages and platforms do that Swift can learn from? Is there anything we definitely shouldn't do?
There is GitHub - IndySockets/Indy: Indy - Internet Direct · GitHub library for Windows written in Pascal. I don’t know if its architecture is optimal, but the library itself is simply invaluable. I’m missing something like this written in Swift.
Thanks for opening the discussion. First, I think we need to separate the HTTP client APIs (the protocols) from the discussion of how a concrete modern HTTP client is built. Both of those can be separated. In fact it helps separating the two since it allows us to focus on API design at a high level without having to solve everything from the bottom up. That separation was our goal with the HTTP API proposal repository and I think we proved that the separation holds. On a technical level this repo only needs two things to achieve stability:
Currency types for the most common request configuration options (endpoints, TLS configuration, etc.)
Stable asynchronous interfaces (AsyncSequence successor and a writing counter part)
To your specific layering question we tried to outline these layers in the vision with this diagram:
Comparing this to your list of layers I think you are missing a fundamental layer here which is the Swift Concurrency executors layer. My personal belief is that since Swift introduced Concurrency to handle scheduling of asynchronous tasks the next logical step is to add support for asynchronous I/O to executors. In my opinion, this will result in great ergonomics and the best possible performance since we can execute the I/O operation on the same thread as the asynchronous task is running. In fact, we have recently started to work on prototypes for async I/O support in swift-platform-executors (TCP socket connection example). In the layering you described this would take the 1. layer. swift-system is just an implementation detail of this to implement the async I/O.
Building up from there I mostly agree with your layers though I think we need two fundamental building blocks:
A general purpose binary serialization and parsing library to implement (4)
Asynchronous streaming APIs (same as the HTTP client APIs)
With the low-level asynchronous interfaces plus those two building blocks, I believe you can build up an entire stack. However, this implies something that I want to make explicit. If our low-level async I/O interfaces are asynchronous and our higher-level APIs (HTTP/gRPC client & server) are also asynchronous then what does it mean for network protocol composition in the middle. I personally believe that only a fully asynchronous networking stack similar to Rust's tokio makes sense. You could imagine a synchronous stack similar to how NIO or swift-network-evolution works but this will require you to invert control between async and sync both at the low-level and high-level interfaces. This inversion of control will result in a performance overhead. A synchronous API also makes the dynamism of arbitrary composition of individual protocols with potentially different message types very hard.
I have personally done some prototyping to see how this could look like and while very early I really like the ergonomics of it:
// Pseudocode
let socket = try await TCPSocketConnection.connect(to: endpoint)
defer {
try! await socket.close()
}
let (tlsConnection, negotiatedProtocol) = try await TLSConnection.connect(alpn: [.http1, .http2], on: socket) // TLSConnection is generic over a reader/writer of UInt8
switch negotiatedProtocol {
case .http1:
let http1Connection = HTTP1Connection(on: tlsConnection)
try await handle(http1Connection)
case .http2:
let http2Connection = HTTP2Connection(on: tlsConnection)
try await handle(http2Connection)
default: fatalError()
}
Agreed and the above would allow this since every single protocol layer is just generic over what it needs from the previous. Most often just a byte reader and writer.
Similarly here. You can insert monitoring and logging yourself by just wrapping the previous reader and writer. You can also make this conditional at compile or runtime.
Most layers are not super clearcut: Between path and QUIC, prior to connection establishment, we might start multiple QUIC connections, one for each path. However, after we have a usable QUIC connection that supports connection migration, path becomes QUIC's responsibility. For 3~5, we still need to support legacy protocols that use StartTLS where the application can drive the stack reconfiguration.
Exposing the entire protocol stack as public APIs has risks, advanced usages can be fragile due to unforeseen use cases, as these might not be well-thought-out customization points. We should definitely build the stack to be layered and composable, but we should also consider the nightmare of maintaining binary compatibility of some apps installing a TCP byte rewriter underneath HTTP.