Strict concurrency for HTTPServerWithQuiescingDemo

This commit is contained in:
George Barnett 2025-03-24 14:38:37 +00:00
parent c92af9d4ef
commit c07bf58fda
2 changed files with 15 additions and 11 deletions

View File

@ -60,7 +60,8 @@ var targets: [PackageDescription.Target] = [
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
]
],
swiftSettings: strictConcurrencySettings
),
.executableTarget(
name: "NIOWritePCAPDemo",

View File

@ -60,10 +60,9 @@ private final class HTTPHandler: ChannelInboundHandler {
context.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil)
buffer.clear()
buffer.writeStaticString("done with the request now\n")
let loopBoundContext = NIOLoopBound.init(context, eventLoop: context.eventLoop)
_ = context.eventLoop.scheduleTask(in: .seconds(30)) { [buffer] in
loopBoundContext.value.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil)
loopBoundContext.value.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
_ = context.eventLoop.assumeIsolated().scheduleTask(in: .seconds(30)) { [buffer] in
context.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
}
}
}
@ -98,17 +97,21 @@ private func runServer() throws {
.serverChannelOption(ChannelOptions.backlog, value: 256)
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.serverChannelInitializer { channel in
channel.pipeline.addHandler(quiesce.makeServerChannelHandler(channel: channel))
channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandler(quiesce.makeServerChannelHandler(channel: channel))
}
}
.childChannelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
.childChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.childChannelOption(ChannelOptions.maxMessagesPerRead, value: 1)
.childChannelInitializer { channel in
channel.pipeline.configureHTTPServerPipeline(
channel.eventLoop.makeCompletedFuture {
let sync = channel.pipeline.syncOperations
try sync.configureHTTPServerPipeline(
withPipeliningAssistance: true,
withErrorHandling: true
).flatMap {
channel.pipeline.addHandler(HTTPHandler())
)
try sync.addHandler(HTTPHandler())
}
}
.bind(host: "localhost", port: 0)