PipeBidirectional copies data to two unidirectional streams. It is a special case of Pipe where it receives a concept that allows for Read and Write side to be closed independently. The main difference is that when piping data from a reader to a writer, if EOF is read, then this implementation propa
(downstream, upstream Stream, maxWaitForSecondStream time.Duration, log *zerolog.Logger)
| 95 | // Finally, depending on once EOF is ready from one of the provided streams, the other direction of streaming data will have a configured time period to also finish, otherwise, |
| 96 | // the method will return immediately with a timeout error. It is however, the responsibility of the caller to close the associated streams in both ends in order to free all the resources/go-routines. |
| 97 | func PipeBidirectional(downstream, upstream Stream, maxWaitForSecondStream time.Duration, log *zerolog.Logger) error { |
| 98 | status := newBiStreamStatus() |
| 99 | |
| 100 | go unidirectionalStream(downstream, upstream, "upstream->downstream", status, log) |
| 101 | go unidirectionalStream(upstream, downstream, "downstream->upstream", status, log) |
| 102 | |
| 103 | if err := status.wait(maxWaitForSecondStream); err != nil { |
| 104 | return errors.Wrap(err, "unable to wait for both streams while proxying") |
| 105 | } |
| 106 | |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | func unidirectionalStream(dst WriterCloser, src Reader, dir string, status *bidirectionalStreamStatus, log *zerolog.Logger) { |
| 111 | defer func() { |