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) }