MCPcopy
hub / github.com/passteque/gluetun / streamToChannel

Function streamToChannel

internal/command/start.go:71–100  ·  view source on GitHub ↗
(ready chan<- struct{},
	stop <-chan struct{}, done chan<- struct{},
	stream io.Reader, lines chan<- string,
)

Source from the content-addressed store, hash-verified

69}
70
71func streamToChannel(ready chan<- struct{},
72 stop <-chan struct{}, done chan<- struct{},
73 stream io.Reader, lines chan<- string,
74) {
75 defer close(done)
76 close(ready)
77 scanner := bufio.NewScanner(stream)
78 lineBuffer := make([]byte, bufio.MaxScanTokenSize) // 64KB
79 const maxCapacity = 20 * 1024 * 1024 // 20MB
80 scanner.Buffer(lineBuffer, maxCapacity)
81
82 for scanner.Scan() {
83 // scanner is closed if the context is canceled
84 // or if the command failed starting because the
85 // stream is closed (io.EOF error).
86 lines <- scanner.Text()
87 }
88 err := scanner.Err()
89 if err == nil || errors.Is(err, os.ErrClosed) {
90 return
91 }
92
93 // ignore the error if it is stopped.
94 select {
95 case <-stop:
96 return
97 default:
98 lines <- "stream error: " + err.Error()
99 }
100}

Callers 1

startFunction · 0.85

Calls 1

ErrorMethod · 0.65

Tested by

no test coverage detected