Skip to content

Commit

Permalink
Try to eliminate extra message
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang01 committed Nov 21, 2024
1 parent ed6ef5a commit 458403c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pgserver/connection_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,8 +1340,11 @@ func (h *ConnectionHandler) handleCopyToStdout(query ConvertedQuery, copyTo *tre
return err
}

done := make(chan struct{})
var sendErr atomic.Value
go func() {
defer close(done)

// Open the pipe for reading.
ctx.GetLogger().Tracef("Opening FIFO pipe for reading: %s", pipePath)
pipe, err := os.OpenFile(pipePath, os.O_RDONLY, os.ModeNamedPipe)
Expand All @@ -1353,6 +1356,10 @@ func (h *ConnectionHandler) handleCopyToStdout(query ConvertedQuery, copyTo *tre
defer pipe.Close()

ctx.GetLogger().Debug("Copying data from the pipe to the client")
defer func() {
ctx.GetLogger().Debug("Finished copying data from the pipe to the client")
}()

buf := make([]byte, 1<<20) // 1MB buffer
for {
n, err := pipe.Read(buf)
Expand Down Expand Up @@ -1380,9 +1387,12 @@ func (h *ConnectionHandler) handleCopyToStdout(query ConvertedQuery, copyTo *tre

select {
case <-ctx.Done(): // Context is canceled
<-done
err, _ := sendErr.Load().(error)
return errors.Join(ctx.Err(), err)
case result := <-ch:
<-done

if result.Err != nil {
return fmt.Errorf("failed to copy data: %w", result.Err)
}
Expand Down

0 comments on commit 458403c

Please sign in to comment.