Skip to content

Commit

Permalink
Ensure flush happens even with errors
Browse files Browse the repository at this point in the history
Fixes #160
  • Loading branch information
lukebakken committed Jan 30, 2023
1 parent e1b77e8 commit b66f0c9
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ func (ch *Channel) sendOpen(msg message) (err error) {
return ch.sendClosed(msg)
}

// Flush the buffer only after all the Frames that comprise the Message
// have been written to maximise benefits of using a buffered writer.
defer func() {
if flushErr := ch.connection.flush(); flushErr != nil {
if err == nil {
err = flushErr
}
}
}()

// We use sendUnflushed() in this method as sending the message requires
// sending multiple Frames (methodFrame, headerFrame, N x bodyFrame).
// Flushing after each Frame is inefficient, as it negates much of the
Expand Down Expand Up @@ -275,10 +285,6 @@ func (ch *Channel) sendOpen(msg message) (err error) {
return
}
}

// Flush the buffer only after all the Frames that comprise the Message
// have been written to maximise benefits of using a buffered writer.
err = ch.connection.flush()
} else {
// If the channel is closed, use Channel.sendClosed()
if ch.IsClosed() {
Expand Down

0 comments on commit b66f0c9

Please sign in to comment.