Sendability warnings

This commit is contained in:
Rick Newton-Rogers 2024-10-28 11:07:04 +00:00
parent 3a47b8e1e7
commit c198c58dc7
5 changed files with 32 additions and 36 deletions

View File

@ -31,8 +31,9 @@ private final class HTTPHandler: ChannelInboundHandler {
self.wrapOutboundOut(.head(HTTPResponseHead(version: head.version, status: .badRequest))), self.wrapOutboundOut(.head(HTTPResponseHead(version: head.version, status: .badRequest))),
promise: nil promise: nil
) )
let loopBoundContext = NIOLoopBound.init(context, eventLoop: context.eventLoop)
context.writeAndFlush(self.wrapOutboundOut(.end(nil))).whenComplete { (_: Result<(), Error>) in context.writeAndFlush(self.wrapOutboundOut(.end(nil))).whenComplete { (_: Result<(), Error>) in
context.close(promise: nil) loopBoundContext.value.close(promise: nil)
} }
return return
} }
@ -59,10 +60,10 @@ private final class HTTPHandler: ChannelInboundHandler {
context.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) context.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil)
buffer.clear() buffer.clear()
buffer.writeStaticString("done with the request now\n") buffer.writeStaticString("done with the request now\n")
let loopBoundContext = NIOLoopBound.init(context, eventLoop: context.eventLoop)
_ = context.eventLoop.scheduleTask(in: .seconds(30)) { [buffer] in _ = context.eventLoop.scheduleTask(in: .seconds(30)) { [buffer] in
context.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) loopBoundContext.value.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) loopBoundContext.value.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
} }
} }
} }

View File

@ -162,13 +162,14 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
return return
} }
let loopBoundContext = NIOLoopBound.init(context, eventLoop: context.eventLoop)
let timeout = context.eventLoop.scheduleTask(deadline: self.deadline) { let timeout = context.eventLoop.scheduleTask(deadline: self.deadline) {
switch self.state { switch self.state {
case .initialized: case .initialized:
preconditionFailure("How can we have a scheduled timeout, if the connection is not even up?") preconditionFailure("How can we have a scheduled timeout, if the connection is not even up?")
case .connectSent, .headReceived: case .connectSent, .headReceived:
self.failWithError(Error.httpProxyHandshakeTimeout(), context: context) self.failWithError(Error.httpProxyHandshakeTimeout(), context: loopBoundContext.value)
case .failed, .completed: case .failed, .completed:
break break

View File

@ -130,21 +130,22 @@ let allDonePromise = group.next().makePromise(of: Void.self)
let maximumFragments = 4 let maximumFragments = 4
let connection = try ClientBootstrap(group: group.next()) let connection = try ClientBootstrap(group: group.next())
.channelInitializer { channel in .channelInitializer { channel in
let pcapRingBuffer = NIOPCAPRingBuffer( channel.eventLoop.makeCompletedFuture {
maximumFragments: maximumFragments, let pcapRingBuffer = NIOPCAPRingBuffer(
maximumBytes: 1_000_000 maximumFragments: maximumFragments,
) maximumBytes: 1_000_000
return channel.pipeline.addHandler(
NIOWritePCAPHandler(
mode: .client,
fileSink: pcapRingBuffer.addFragment
) )
).flatMap { try channel.pipeline.syncOperations.addHandler(
channel.pipeline.addHTTPClientHandlers() NIOWritePCAPHandler(
}.flatMap { mode: .client,
channel.pipeline.addHandler(TriggerPCAPHandler(pcapRingBuffer: pcapRingBuffer, sink: fileSink.write)) fileSink: pcapRingBuffer.addFragment
}.flatMap { )
channel.pipeline.addHandler(SendSimpleSequenceRequestHandler(allDonePromise: allDonePromise)) )
try channel.pipeline.syncOperations.addHTTPClientHandlers()
try channel.pipeline.syncOperations.addHandlers([
TriggerPCAPHandler(pcapRingBuffer: pcapRingBuffer, sink: fileSink.write),
SendSimpleSequenceRequestHandler(allDonePromise: allDonePromise),
])
} }
} }
.connect(host: "httpbin.org", port: 80) .connect(host: "httpbin.org", port: 80)

View File

@ -204,7 +204,6 @@ class PCAPRingBufferTest: XCTestCase {
) )
} }
let channel = EmbeddedChannel()
let trigger = self.dataForTests()[0..<triggerEndIndex] let trigger = self.dataForTests()[0..<triggerEndIndex]
.compactMap { t in t.readableBytes }.reduce(0, +) .compactMap { t in t.readableBytes }.reduce(0, +)
@ -213,20 +212,14 @@ class PCAPRingBufferTest: XCTestCase {
maximumBytes: 1_000_000 maximumBytes: 1_000_000
) )
let addHandlers = let channel = EmbeddedChannel(handlers: [
channel.pipeline.addHandler( NIOWritePCAPHandler(mode: .client, fileSink: pcapRingBuffer.addFragment),
NIOWritePCAPHandler(mode: .client, fileSink: pcapRingBuffer.addFragment), TriggerOnCumulativeSizeHandler(
name: "capture" triggerBytes: trigger,
) pcapRingBuffer: pcapRingBuffer,
.flatMap { () -> EventLoopFuture<Void> in sink: testRecordedBytes
let triggerHandler = TriggerOnCumulativeSizeHandler( ),
triggerBytes: trigger, ])
pcapRingBuffer: pcapRingBuffer,
sink: testRecordedBytes
)
return channel.pipeline.addHandler(triggerHandler, name: "trigger")
}
XCTAssertNoThrow(try addHandlers.wait())
channel.localAddress = try! SocketAddress(ipAddress: "255.255.255.254", port: Int(UInt16.max) - 1) channel.localAddress = try! SocketAddress(ipAddress: "255.255.255.254", port: Int(UInt16.max) - 1)
XCTAssertNoThrow(try channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5678)).wait()) XCTAssertNoThrow(try channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5678)).wait())

View File

@ -67,7 +67,7 @@ private func withTemporaryFile<T>(
_ body: (NIOCore.NIOFileHandle, String) throws -> T _ body: (NIOCore.NIOFileHandle, String) throws -> T
) throws -> T { ) throws -> T {
let temporaryFilePath = "\(temporaryDirectory)/nio_extras_\(UUID())" let temporaryFilePath = "\(temporaryDirectory)/nio_extras_\(UUID())"
FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8)) XCTAssertTrue(FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8)))
defer { defer {
XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath)) XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath))
} }
@ -86,7 +86,7 @@ private func withTemporaryFile<T>(
_ body: (NIOCore.NIOFileHandle, String) async throws -> T _ body: (NIOCore.NIOFileHandle, String) async throws -> T
) async throws -> T { ) async throws -> T {
let temporaryFilePath = "\(temporaryDirectory)/nio_extras_\(UUID())" let temporaryFilePath = "\(temporaryDirectory)/nio_extras_\(UUID())"
FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8)) XCTAssertTrue(FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8)))
defer { defer {
XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath)) XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath))
} }