MCPcopy
hub / github.com/cloudflare/cloudflared / PipeBidirectional

Function PipeBidirectional

stream/stream.go:97–108  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
97func 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
110func unidirectionalStream(dst WriterCloser, src Reader, dir string, status *bidirectionalStreamStatus, log *zerolog.Logger) {
111 defer func() {

Callers 2

PipeFunction · 0.85

Calls 3

newBiStreamStatusFunction · 0.85
unidirectionalStreamFunction · 0.85
waitMethod · 0.80

Tested by 1