MCPcopy Create free account
hub / github.com/containerd/containerd / ReceiveStream

Function ReceiveStream

core/transfer/streaming/stream.go:143–205  ·  view source on GitHub ↗
(ctx context.Context, stream streaming.Stream)

Source from the content-addressed store, hash-verified

141}
142
143func ReceiveStream(ctx context.Context, stream streaming.Stream) io.Reader {
144 r, w := io.Pipe()
145 go func() {
146 defer stream.Close()
147 var window int32
148 for {
149 var werr error
150 if window < windowSize {
151 update := &transferapi.WindowUpdate{
152 Update: windowSize,
153 }
154 anyType, err := typeurl.MarshalAny(update)
155 if err != nil {
156 w.CloseWithError(fmt.Errorf("failed to marshal window update: %w", err))
157 return
158 }
159 // check window update error after recv, stream may be complete
160 if werr = stream.Send(anyType); werr == nil {
161 window += windowSize
162 } else if errors.Is(werr, io.EOF) {
163 // TODO: Why does send return EOF here
164 werr = nil
165 }
166 }
167 anyType, err := stream.Recv()
168 if err != nil {
169 if errors.Is(err, io.EOF) || errors.Is(err, context.Canceled) {
170 err = nil
171 } else {
172 err = fmt.Errorf("received failed: %w", err)
173 }
174 w.CloseWithError(err)
175 return
176 } else if werr != nil {
177 // Try receive before erroring out
178 w.CloseWithError(fmt.Errorf("failed to send window update: %w", werr))
179 return
180 }
181 i, err := typeurl.UnmarshalAny(anyType)
182 if err != nil {
183 w.CloseWithError(fmt.Errorf("failed to unmarshal received object: %w", err))
184 return
185 }
186 switch v := i.(type) {
187 case *transferapi.Data:
188 n, err := w.Write(v.Data)
189 if err != nil {
190 w.CloseWithError(fmt.Errorf("failed to unmarshal received object: %w", err))
191 // Close will error out sender
192 return
193 }
194 window = window - int32(n)
195 // TODO: Handle error case
196 default:
197 log.G(ctx).Warnf("Ignoring unknown stream object of type %T", i)
198 continue
199 }
200 }

Callers 4

runSendAndReceiveFuzzFunction · 0.85
runWriterFuzzFunction · 0.85
chainStreamsFunction · 0.85

Calls 8

PipeMethod · 0.80
CloseMethod · 0.65
MarshalAnyMethod · 0.65
SendMethod · 0.65
RecvMethod · 0.65
UnmarshalAnyMethod · 0.65
WriteMethod · 0.65
CloseWithErrorMethod · 0.45

Tested by 4

runSendAndReceiveFuzzFunction · 0.68
runWriterFuzzFunction · 0.68
chainStreamsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…