converge/pkg/support/ioutils/synchronizestreams.go
Erik Brakkee e9fdece4ec Updated packages
Dealing with short writes.
A more clean implementation of SynchronizeStreams to give a higher
guarantee that all data will be written before the connections are
closed.
2024-12-25 19:42:56 +01:00

43 lines
934 B
Go

package ioutils
import (
"io"
"log"
"time"
)
func SynchronizeStreams(description string, stream1, stream2 io.ReadWriter) {
waitChannel := make(chan bool, 2)
go func() {
defer func() {
waitChannel <- true
}()
_, err := io.Copy(&RetryWriter{stream1}, stream2)
if err != nil {
log.Printf("SynchronizeStreams: %s: error <-: %v\n", description, err)
}
}()
go func() {
defer func() {
waitChannel <- true
}()
_, err := io.Copy(&RetryWriter{stream2}, stream1)
if err != nil {
log.Printf("SynchronizeStreams: %s: error ->: %v\n", description, err)
}
}()
<-waitChannel
// wait until the second connection closes with a timeout to make it more likely
// that all data will be written.
select {
case <-waitChannel:
// the other connection also closed
case <-time.After(500 * time.Millisecond):
// the timeout expired
}
log.Printf("SynchronizeStreams: %s: Connection closed", description)
}