Skip to content

Commit f051108

Browse files
committed
One QUIC connection per endpoint-pair
1 parent 37019d1 commit f051108

10 files changed

Lines changed: 800 additions & 368 deletions

File tree

‎packages/network-transport-quic/CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Unreleased Laurent P. René de Cotret <laurent.decotret@outlook.com> 0.2.0
2+
3+
* All the connections to an endpoint are now carried by a single QUIC connection (one stream
4+
each), rather than by one QUIC connection for each.
5+
* The number of logical connections to one endpoint is no longer limited by the QUIC
6+
concurrent stream limit.
7+
* Breaking change: Add the `socketOptions` field to `QUICTransportConfig`, allowing the user to control the UDP socket
8+
underlying a connection.
19

210
2026-04-21 Laurent P. René de Cotret <laurent.decotret@outlook.com> 0.1.2
311

‎packages/network-transport-quic/bench/Bench.hs‎

Lines changed: 111 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,51 @@
66

77
module Main where
88

9-
import Control.Concurrent (forkIO)
10-
import Control.Concurrent.Async (forConcurrently_)
9+
import Control.Concurrent.Async (forConcurrently_, link, wait, withAsync)
1110
import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
1211
import Control.Exception (finally, throwIO)
13-
import Control.Monad (forM_, replicateM, void, when)
12+
import Control.Monad (forM_, forever, replicateM, void, when)
1413
import qualified Data.ByteString as BS
15-
import Data.IORef (
16-
atomicModifyIORef',
17-
newIORef,
18-
)
14+
import Data.IORef
15+
( atomicModifyIORef',
16+
newIORef,
17+
)
1918
import Data.List.NonEmpty (NonEmpty (..))
20-
import Network.Transport (
21-
Connection (send),
22-
EndPoint (address, connect, receive),
23-
Event (ConnectionOpened, Received),
24-
Reliability (ReliableOrdered),
25-
Transport (closeTransport, newEndPoint),
26-
defaultConnectHints,
27-
)
19+
import qualified Network.Socket as N
20+
import Network.Transport
21+
( Connection (send),
22+
EndPoint (address, connect, receive),
23+
Event (ConnectionOpened, ErrorEvent, Received),
24+
Reliability (ReliableOrdered),
25+
Transport (closeTransport, newEndPoint),
26+
defaultConnectHints,
27+
)
2828
import qualified Network.Transport.QUIC as QUIC
2929
import qualified Network.Transport.TCP as TCP
3030
import System.FilePath ((</>))
31-
import Test.Tasty (TestTree)
32-
import Test.Tasty.Bench (bench, bgroup, defaultMain, nfIO)
31+
import System.Timeout (timeout)
32+
import Test.Tasty (localOption)
33+
import Test.Tasty.Bench (Benchmark, TimeMode (WallTime), bench, bgroup, defaultMain, nfIO)
3334

3435
data TransportConfig = TransportConfig
35-
{ transportName :: String
36-
, mkTransport :: IO Transport
36+
{ transportName :: String,
37+
mkTransport :: IO Transport
3738
}
3839

3940
tcpConfig :: TransportConfig
4041
tcpConfig =
4142
TransportConfig
42-
{ transportName = "TCP"
43-
, mkTransport = do
43+
{ transportName = "TCP",
44+
mkTransport = do
4445
Right t <- TCP.createTransport (TCP.defaultTCPAddr "127.0.0.1" "0") TCP.defaultTCPParameters
4546
pure t
4647
}
4748

4849
quicConfig :: TransportConfig
4950
quicConfig =
5051
TransportConfig
51-
{ transportName = "QUIC"
52-
, mkTransport =
52+
{ transportName = "QUIC",
53+
mkTransport =
5354
QUIC.credentialLoadX509
5455
-- Generate a self-signed x509v3 certificate using this nifty tool:
5556
-- https://certificatetools.com/
@@ -59,103 +60,125 @@ quicConfig =
5960
Left errmsg -> throwIO $ userError errmsg
6061
Right credentials ->
6162
QUIC.createTransport
62-
( QUIC.QUICTransportConfig
63-
{ hostName = "127.0.0.1"
64-
, serviceName = "0"
65-
, credentials = credentials :| []
66-
, -- credentials are self-signed
67-
validateCredentials = False
63+
( (QUIC.defaultQUICTransportConfig "127.0.0.1" (credentials :| []))
64+
{ QUIC.serviceName = "0",
65+
QUIC.validateCredentials = False,
66+
-- For benchmarks with lots of streams and tiny messages, we can easily
67+
-- overflow the receive buffer
68+
QUIC.socketOptions = [(N.RecvBuffer, 4 * 1024 * 1024)]
6869
}
6970
)
7071
}
7172

7273
data BenchParams = BenchParams
73-
{ messageSize :: !Int
74-
, messageCount :: !Int
75-
, connectionCount :: !Int
74+
{ messageSize :: !Int,
75+
messageCount :: !Int,
76+
connectionCount :: !Int
7677
}
7778

7879
smallMessages, mediumMessages, largeMessages :: BenchParams
79-
smallMessages = BenchParams{messageSize = 64, messageCount = 10_000, connectionCount = 1}
80-
mediumMessages = BenchParams{messageSize = 1024, messageCount = 1_000, connectionCount = 1}
81-
largeMessages = BenchParams{messageSize = 4096, messageCount = 100, connectionCount = 1}
80+
smallMessages = BenchParams {messageSize = 64, messageCount = 10_000, connectionCount = 1}
81+
mediumMessages = BenchParams {messageSize = 1024, messageCount = 1_000, connectionCount = 1}
82+
largeMessages = BenchParams {messageSize = 4096, messageCount = 100, connectionCount = 1}
8283

8384
multiConn :: Int -> BenchParams -> BenchParams
84-
multiConn n p = p{connectionCount = n}
85+
multiConn n p = p {connectionCount = n}
8586

8687
throughputBench :: TransportConfig -> BenchParams -> IO ()
87-
throughputBench TransportConfig{mkTransport} BenchParams{messageSize, messageCount, connectionCount} = do
88+
throughputBench cfg params =
89+
timeout 30_000_000 (throughputBench' cfg params)
90+
>>= maybe (throwIO $ userError "benchmark stalled: timed out waiting for messages") pure
91+
92+
throughputBench' :: TransportConfig -> BenchParams -> IO ()
93+
throughputBench' TransportConfig {mkTransport} BenchParams {messageSize, messageCount, connectionCount} = do
8894
transport <- mkTransport
89-
flip finally (closeTransport transport) $ do
95+
-- Closing is bounded as well: it can block if a connection was lost, and that
96+
-- would hide the failure we are trying to report.
97+
flip finally (void $ timeout 5_000_000 (closeTransport transport)) $ do
9098
Right senderEP <- newEndPoint transport
9199
Right receiverEP <- newEndPoint transport
92100

93101
let payload = BS.replicate messageSize 0x42
94102
totalMessages = messageCount * connectionCount
95103

96104
receiverReady <- newEmptyMVar
97-
receiverDone <- newEmptyMVar
98-
99-
void $ forkIO $ do
100-
connsEstablished <- newIORef (0 :: Int)
101-
let waitForConnections = do
102-
event <- receive receiverEP
103-
case event of
104-
ConnectionOpened{} -> do
105-
n <- atomicModifyIORef' connsEstablished (\x -> (x + 1, x + 1))
106-
when (n < connectionCount) waitForConnections
107-
_ -> waitForConnections
108-
waitForConnections
109-
putMVar receiverReady ()
110-
111-
msgsReceived <- newIORef (0 :: Int)
112-
let recvLoop = do
113-
event <- receive receiverEP
114-
case event of
115-
Received _ _ -> do
116-
n <- atomicModifyIORef' msgsReceived (\x -> (x + 1, x + 1))
117-
when (n < totalMessages) recvLoop
118-
_ -> recvLoop
119-
recvLoop
120-
putMVar receiverDone ()
121-
122-
let receiverAddr = address receiverEP
123-
connections <-
124-
replicateM
125-
connectionCount
126-
(connect senderEP receiverAddr ReliableOrdered defaultConnectHints >>= either throwIO pure)
127-
128-
takeMVar receiverReady
129-
130-
forConcurrently_ connections $ \conn ->
131-
forM_ [0 .. messageCount] $ \_ -> send conn [payload]
132-
133-
takeMVar receiverDone
134-
135-
benchTransport :: TransportConfig -> TestTree
136-
benchTransport cfg@TransportConfig{transportName} =
105+
106+
let receiver = do
107+
connsEstablished <- newIORef (0 :: Int)
108+
let waitForConnections = do
109+
event <- receive receiverEP
110+
case event of
111+
ConnectionOpened {} -> do
112+
n <- atomicModifyIORef' connsEstablished (\x -> (x + 1, x + 1))
113+
when (n < connectionCount) waitForConnections
114+
ErrorEvent err -> throwIO err
115+
_ -> waitForConnections
116+
waitForConnections
117+
putMVar receiverReady ()
118+
119+
msgsReceived <- newIORef (0 :: Int)
120+
let recvLoop = do
121+
event <- receive receiverEP
122+
case event of
123+
Received _ _ -> do
124+
n <- atomicModifyIORef' msgsReceived (\x -> (x + 1, x + 1))
125+
when (n < totalMessages) recvLoop
126+
ErrorEvent err -> throwIO err
127+
_ -> recvLoop
128+
recvLoop
129+
130+
let watchSender = forever $ do
131+
event <- receive senderEP
132+
case event of
133+
ErrorEvent err -> throwIO err
134+
_ -> pure ()
135+
136+
withAsync receiver $ \receiverAsync -> withAsync watchSender $ \senderAsync -> do
137+
link receiverAsync
138+
link senderAsync
139+
140+
let receiverAddr = address receiverEP
141+
connections <-
142+
replicateM
143+
connectionCount
144+
(connect senderEP receiverAddr ReliableOrdered defaultConnectHints >>= either throwIO pure)
145+
146+
takeMVar receiverReady
147+
148+
forConcurrently_ connections $ \conn ->
149+
forM_ [0 .. messageCount] $ \_ -> send conn [payload] >>= either throwIO pure
150+
151+
wait receiverAsync
152+
153+
benchTransport :: TransportConfig -> Benchmark
154+
benchTransport cfg@TransportConfig {transportName} =
137155
bgroup
138156
transportName
139157
[ bgroup
140158
"throughput"
141159
[ bgroup
142160
"single-connection"
143-
[ bench "small-msg" $ nfIO $ throughputBench cfg smallMessages
144-
, bench "default-msg" $ nfIO $ throughputBench cfg mediumMessages
145-
, bench "large-msg" $ nfIO $ throughputBench cfg largeMessages
146-
]
147-
, bgroup
161+
[ bench "small-msg" $ nfIO $ throughputBench cfg smallMessages,
162+
bench "default-msg" $ nfIO $ throughputBench cfg mediumMessages,
163+
bench "large-msg" $ nfIO $ throughputBench cfg largeMessages
164+
],
165+
bgroup
148166
"multi-connection"
149-
[ bench "2-conn" $ nfIO $ throughputBench cfg smallMessages{connectionCount = 2, messageCount = 10_000}
150-
, bench "5-conn" $ nfIO $ throughputBench cfg smallMessages{connectionCount = 5, messageCount = 10_000}
151-
, bench "10-conn" $ nfIO $ throughputBench cfg smallMessages{connectionCount = 10, messageCount = 5_000}
167+
[ bench "2-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 2, messageCount = 10_000},
168+
bench "5-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 5, messageCount = 10_000},
169+
bench "10-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 10, messageCount = 5_000},
170+
bench "50-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 50, messageCount = 100},
171+
bench "100-conn" $ nfIO $ throughputBench cfg smallMessages {connectionCount = 100, messageCount = 50}
152172
]
153173
]
154174
]
155175

156176
main :: IO ()
157177
main =
158178
defaultMain
159-
[ benchTransport tcpConfig
160-
, benchTransport quicConfig
179+
-- QUIC is a userspace networking protocol,
180+
-- so CPU time isn't the appropriate comparison
181+
-- to make with TCP
182+
[ localOption WallTime (benchTransport tcpConfig),
183+
localOption WallTime (benchTransport quicConfig)
161184
]

‎packages/network-transport-quic/network-transport-quic.cabal‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.0
22
Name: network-transport-quic
3-
Version: 0.1.2
3+
Version: 0.2.0
44
build-Type: Simple
55
License: BSD-3-Clause
66
License-file: LICENSE
@@ -59,10 +59,9 @@ library
5959
, microlens-platform ^>=0.4
6060
, network >= 3.1 && < 3.3
6161
, network-transport >= 0.5 && < 0.6
62-
-- Prior to version 0.2.20, `quic` had issues with handling
63-
-- pending data in the stream buffer. This meant that vectored
64-
-- message sends did not work correctly at the transport layer
65-
, quic >=0.2.20 && <0.4
62+
-- Version 0.3.15 added graceful server shutdown which
63+
-- changes the way network-transport-quic works
64+
, quic >=0.3.15 && <0.4
6665
, stm >=2.4 && <2.6
6766
, tls >= 2.1 && < 2.5
6867
, tls-session-manager >= 0.0.5 && <0.2
@@ -97,6 +96,7 @@ test-suite network-transport-quic-tests
9796
, network-transport
9897
, network-transport-quic
9998
, network-transport-tests
99+
, quic
100100
, tasty ^>=1.5
101101
, tasty-flaky ^>= 0.1.3
102102
, tasty-hedgehog
@@ -108,13 +108,15 @@ benchmark network-transport-quic-bench
108108
hs-source-dirs: bench
109109
main-is: Bench.hs
110110
default-language: Haskell2010
111-
ghc-options: -rtsopts -with-rtsopts=-N
111+
-- -T makes the allocations of each benchmark appear in its results
112+
ghc-options: -rtsopts "-with-rtsopts=-N -T"
112113
build-depends: async
113114
, base >=4.14 && <5
114115
, bytestring
115116
, filepath
117+
, network
116118
, network-transport
117119
, network-transport-tcp
118120
, network-transport-quic
119-
, tasty ^>=1.5
121+
, tasty ^>=1.5
120122
, tasty-bench >=0.4

0 commit comments

Comments
 (0)