@@ -696,26 +696,12 @@ extension PostgresConnection {
696696
697697// MARK: Copy from
698698
699- fileprivate extension EventLoop {
700- /// If we are on the given event loop, execute `task` immediately. Otherwise schedule it for execution.
701- func executeImmediatelyOrSchedule( _ task: @Sendable @escaping ( ) -> Void ) {
702- if inEventLoop {
703- return task ( )
704- }
705- return execute ( task)
706- }
707- }
708-
709699/// A handle to send
710700public struct PostgresCopyFromWriter : Sendable {
711701 private let channelHandler : NIOLoopBound < PostgresChannelHandler >
712702 private let context : NIOLoopBound < ChannelHandlerContext >
713703 private let eventLoop : any EventLoop
714704
715- struct NotWritableError : Error , CustomStringConvertible {
716- var description = " No data must be written to `PostgresCopyFromWriter` after it has sent a CopyDone or CopyFail message, ie. after the closure producing the copy data has finished "
717- }
718-
719705 init ( handler: PostgresChannelHandler , context: ChannelHandlerContext , eventLoop: any EventLoop ) {
720706 self . channelHandler = NIOLoopBound ( handler, eventLoop: eventLoop)
721707 self . context = NIOLoopBound ( context, eventLoop: eventLoop)
@@ -725,8 +711,12 @@ public struct PostgresCopyFromWriter: Sendable {
725711 /// Send data for a `COPY ... FROM STDIN` operation to the backend.
726712 public func write( _ byteBuffer: ByteBuffer ) async throws {
727713 await withCheckedContinuation { ( continuation: CheckedContinuation < Void , Never > ) in
728- eventLoop. executeImmediatelyOrSchedule {
714+ if eventLoop. inEventLoop {
729715 self . channelHandler. value. copyData ( byteBuffer, context: self . context. value, readyForMoreWriteContinuation: continuation)
716+ } else {
717+ eventLoop. execute {
718+ self . channelHandler. value. copyData ( byteBuffer, context: self . context. value, readyForMoreWriteContinuation: continuation)
719+ }
730720 }
731721 }
732722 }
@@ -735,8 +725,12 @@ public struct PostgresCopyFromWriter: Sendable {
735725 /// the backend.
736726 func done( ) async throws {
737727 try await withCheckedThrowingContinuation { ( continuation: CheckedContinuation < Void , any Error > ) in
738- eventLoop. executeImmediatelyOrSchedule {
728+ if eventLoop. inEventLoop {
739729 self . channelHandler. value. sendCopyDone ( continuation: continuation, context: self . context. value)
730+ } else {
731+ eventLoop. execute {
732+ self . channelHandler. value. sendCopyDone ( continuation: continuation, context: self . context. value)
733+ }
740734 }
741735 }
742736 }
@@ -745,8 +739,12 @@ public struct PostgresCopyFromWriter: Sendable {
745739 /// the backend.
746740 func failed( error: any Error ) async throws {
747741 try await withCheckedThrowingContinuation { ( continuation: CheckedContinuation < Void , any Error > ) in
748- eventLoop. executeImmediatelyOrSchedule {
742+ if eventLoop. inEventLoop {
749743 self . channelHandler. value. sendCopyFailed ( message: " \( error) " , continuation: continuation, context: self . context. value)
744+ } else {
745+ eventLoop. execute {
746+ self . channelHandler. value. sendCopyFailed ( message: " \( error) " , continuation: continuation, context: self . context. value)
747+ }
750748 }
751749 }
752750 }
0 commit comments