Repository navigation
Expand file tree
/
Copy pathclient_response.go
More file actions
902 lines (847 loc) · 27.3 KB
/
Copy pathclient_response.go
File metadata and controls
902 lines (847 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || windows
package http
import (
"errors"
"io"
stdhttp "net/http"
"runtime/debug"
"sync"
"github.com/lesismal/fib/bufferpool"
"github.com/lesismal/fib/log"
"github.com/lesismal/fib/sidepool"
)
// DefaultStreamResponseBodyBuffer is ClientConfig.StreamResponseBodyBuffer's
// default.
const DefaultStreamResponseBodyBuffer = 256 << 10
var (
// ErrResponseAbandoned is what a streamed response body reports when it
// was given up: by ClientResponse.Close, or because the callback returned
// without taking the body, and the rest of it was more than the client
// would read to keep the connection.
ErrResponseAbandoned = errors.New("http: response body no longer being read")
// ErrBodyTaken is what reading a streamed response's Body reports once
// OnBody has taken the body over.
ErrBodyTaken = errors.New("http: read of a response body that OnBody has taken")
)
// ClientResponse is what a Client hands its callback: the response, with
// ClientResponse.Response being the net/http value, embedded so that its fields
// read directly, and a way to take its body as it arrives.
//
// By default the callback runs once the response is complete, Body holds all
// of it and stays readable after the callback returns, and OnBody delivers it
// in one call. With ClientConfig.StreamResponseBody the callback of a large
// response runs before the body has arrived, and the body is taken with OnBody
// or read from Body inside the callback; see that option.
type ClientResponse struct {
*stdhttp.Response
mu sync.Mutex
// whole, fn, inCallback, delivered and taken serve a response that
// arrived whole: OnBody delivers its body once the callback has returned,
// and taken is how much of it that was.
whole *bufferedBody
fn BodyFunc
inCallback bool
delivered bool
paused bool
taken int64
// s is the stream of a response whose body was still arriving when the
// callback ran, and nil for one that arrived whole.
s *ResponseStream
// abort gives the response up; see Close. req is the Client's request
// it answers, which gives it up in abort's place, saving the function
// value a request's abort would take an allocation for.
abort func(error)
req *clientRequest
// tunnel is what a 101 to StartUpgrade's request switched to.
tunnel *Tunnel
// block is the header's lines, when they came in place of Header.
block string
}
// HeaderBlock is the response's header as the lines it arrived in, each ended
// by CRLF, without the status line before them or the blank line after, for
// a request sent with Framing.ResponseHeaderBlock whose response came so:
// Header is nil then. It holds every field as it arrived, Content-Length,
// Transfer-Encoding and Connection among them, which the client has framed
// the response by; every name in it is a token, as the server wrote it, and
// every value plain. It is "" for any other response, whose fields are in
// Header.
func (c *ClientResponse) HeaderBlock() string { return c.block }
// Tunnel returns the tunnel the connection became when the server answered
// a request sent with Client.StartUpgrade with 101 Switching Protocols, and
// nil for any other response.
func (c *ClientResponse) Tunnel() *Tunnel { return c.tunnel }
// NewBufferedBody returns the Body for a response that arrived whole, which
// DeliverResponse can hand to OnBody without copying it. The body does not
// copy data either.
//
// It is also the Body for a request whose body is bytes already: Client
// takes data as it is rather than reading it into a copy, so data must stay
// as it is until the request's callback has run.
func NewBufferedBody(data []byte) io.ReadCloser {
if len(data) == 0 {
return stdhttp.NoBody
}
return &bufferedBody{data: data}
}
// DeliverResponse runs callback with resp, a response whose body has arrived
// whole, as a ClientResponse that OnBody serves once callback returns. abort
// is what ClientResponse.Close does: it fails the request and abandons the
// exchange. It is for the clients of other transports, such as package http3,
// to deliver their responses the way this package's Client does.
func DeliverResponse(resp *stdhttp.Response, abort func(error), callback func(*ClientResponse, error)) {
deliverArrived(&ClientResponse{Response: resp, abort: abort}, callback)
}
// deliverArrived runs callback with cr, a response whose body has arrived
// whole, and then OnBody's function if the callback registered one.
func deliverArrived(cr *ClientResponse, callback func(*ClientResponse, error)) {
cr.whole, _ = cr.Body.(*bufferedBody)
cr.mu.Lock()
cr.inCallback = true
cr.mu.Unlock()
callback(cr, nil)
cr.mu.Lock()
cr.inCallback = false
taken := cr.fn != nil
cr.mu.Unlock()
if taken {
cr.deliverWhole()
}
}
// OnBody delivers the response's body to fn, piece by piece as it arrives.
// fin marks the last call, and err a body that will not be finished — the
// connection went, the request timed out or was cancelled, the body outgrew
// ClientConfig.MaxStreamedBodyBytes, its framing was broken, or Close was
// called — which is also a last call. data is only valid for the duration of
// the call, and the trailer, if any, is in Trailer once fin arrives.
//
// OnBody never calls fn itself and never waits: it registers fn and returns.
// What of the body has already arrived is handed to fn once the callback
// returns, on the goroutine that ran it, and the rest follows as the
// connection is read, on the worker that reads it. The calls are one at a time
// and in order, so a body of any size can be taken without a goroutine and
// without it being buffered: what holds the connection back is fn itself.
// On HTTP/2 that worker reads every stream of the connection, so fn that
// waits holds them all up, not only its own; a reader of Body that stalls
// holds up only its stream.
// A response that arrived whole is delivered in one call with fin set.
//
// Call OnBody from inside the callback. Called after the callback has
// returned, it still gets a body that is being kept, but not one the client
// gave up on because nothing took it, which fn then hears of as
// ErrResponseAbandoned. Calling it twice replaces fn, and Body is not
// readable once it has been called.
func (c *ClientResponse) OnBody(fn BodyFunc) {
if fn == nil {
return
}
if c.s != nil {
c.s.setFn(fn)
return
}
c.mu.Lock()
if c.delivered {
c.mu.Unlock()
return
}
c.fn = fn
late := !c.inCallback
c.mu.Unlock()
if late {
sidepool.Go(c.deliverWhole)
}
}
// HoldBody pauses, with hold set, the delivery of the body to OnBody's
// function, and resumes it without. While it is held, what arrives waits in
// the client's buffer, as it does for a function that is slow to return:
// HTTP/1 stops reading the connection once StreamResponseBodyBuffer waits,
// and HTTP/2 and HTTP/3 give back no window for this stream, so the other
// streams on the connection go on. The end of the body waits too, but an
// error, which ends the body without the rest of it, is delivered at once.
// Resuming delivers what waits on the client's side pool, never on the
// caller's goroutine, so HoldBody may be called from inside the function
// and from anywhere else. It is how a caller that passes the body on to a
// writer that must not wait, such as Context.WriteNoWait, pauses it until
// Context.OnWritable says there is room.
//
// A response that arrived whole is held the same way, before its one call.
func (c *ClientResponse) HoldBody(hold bool) {
if c.s != nil {
c.s.hold(hold)
return
}
c.mu.Lock()
resume := c.paused && !hold && c.fn != nil && !c.delivered && !c.inCallback
c.paused = hold
c.mu.Unlock()
if resume {
sidepool.Go(c.deliverWhole)
}
}
// deliverWhole hands a body that arrived whole to the function waiting for it.
func (c *ClientResponse) deliverWhole() {
c.mu.Lock()
fn := c.fn
if fn == nil || c.delivered || c.paused {
c.mu.Unlock()
return
}
c.delivered, c.fn = true, nil
c.mu.Unlock()
var data []byte
if c.whole != nil {
data = c.whole.data[c.whole.off:]
c.whole.off = len(c.whole.data)
} else if body := c.Response.Body; body != nil && body != stdhttp.NoBody {
data, _ = io.ReadAll(body)
}
c.mu.Lock()
c.taken = int64(len(data))
c.mu.Unlock()
fn(data, true, nil)
// data was fn's for the call only: a pooled body nothing else was handed
// goes back now.
c.mu.Lock()
w := c.whole
put := w != nil && w.pooled
if put {
w.pooled = false
}
c.mu.Unlock()
if put {
bufferpool.Put(w.data)
w.data, w.off = nil, 0
}
}
// BodyBytes returns the body of a response that arrived whole, what of it
// Body has yet to read, and true; it reports false for a body that streams.
// The bytes are the client's own copy, which it never reuses, so they stay
// valid for as long as they are referenced and may be handed on without a
// copy, as Context.WriteResponse's Body, say, even past the callback. They
// must not be changed. Taking them reads nothing: Body and OnBody still
// deliver them.
func (c *ClientResponse) BodyBytes() ([]byte, bool) {
if c.s != nil {
return nil, false
}
switch body := c.Response.Body; {
case c.whole != nil:
// The bytes are the caller's for as long as it keeps them: Close
// leaves them to the collector rather than give them back.
c.mu.Lock()
c.whole.pooled = false
c.mu.Unlock()
return c.whole.data[c.whole.off:], true
case body == nil || body == stdhttp.NoBody:
return nil, true
}
return nil, false
}
// TakeBody hands over the body of a response that arrived whole: what of it
// Body has yet to read, and true; it reports false for a body that streams.
// From then on the bytes are the caller's, and Body, BodyBytes and OnBody
// find the body empty.
//
// pooled says the bytes are a buffer from package bufferpool, which the
// caller may give back, with bufferpool.Put or by sending it with
// Context.WriteResponseOwned, once it has done with them; it need not, since
// the collector frees a buffer that is not given back. It is how a proxy
// passes a response on without a copy and without the allocation each
// response's body otherwise costs: TakeBody, then WriteResponseOwned.
func (c *ClientResponse) TakeBody() (body []byte, pooled, ok bool) {
if c.s != nil {
return nil, false, false
}
c.mu.Lock()
defer c.mu.Unlock()
w := c.whole
if w == nil {
if b := c.Response.Body; b == nil || b == stdhttp.NoBody {
return nil, false, true
}
return nil, false, false
}
body, pooled = w.data[w.off:], w.pooled && w.off == 0
w.data, w.off, w.pooled = nil, 0, false
return body, pooled, true
}
// BodyComplete reports whether all of the body has arrived, which is always so
// unless the response streams. It is the way to tell, inside the callback,
// whether Body holds the whole of it.
func (c *ClientResponse) BodyComplete() bool {
if c.s == nil {
return true
}
return c.s.complete()
}
// Trailer returns the trailer fields that followed the body, which are known
// once it has ended, and nil before that and for a body without any.
func (c *ClientResponse) Trailer() stdhttp.Header {
if c.s == nil {
return c.Response.Trailer
}
return c.s.trailerFields()
}
// Consumed returns how many bytes of the body have gone to OnBody's function
// or been read from Body so far.
func (c *ClientResponse) Consumed() int64 {
if c.s == nil {
c.mu.Lock()
defer c.mu.Unlock()
return c.taken
}
return c.s.consumedBytes()
}
// Close gives up on the rest of a body that is still arriving: the connection
// is closed and OnBody's function, if it has one, hears ErrResponseAbandoned.
// It does nothing once the body has all arrived.
func (c *ClientResponse) Close() {
switch {
case c.req != nil:
c.req.abort(ErrResponseAbandoned)
case c.abort != nil:
c.abort(ErrResponseAbandoned)
}
}
// bufferedBody is the Body of a response that arrived whole, or of a request
// whose body is bytes already.
type bufferedBody struct {
data []byte
off int
// pooled says data is a buffer from package bufferpool that nothing
// outside has been handed: TakeBody hands it over for the caller to give
// back, Close gives it back, and BodyBytes, which hands it out for as
// long as the caller keeps it, clears it, leaving it to the collector.
pooled bool
}
func (b *bufferedBody) Read(p []byte) (int, error) {
if b.off >= len(b.data) {
return 0, io.EOF
}
n := copy(p, b.data[b.off:])
b.off += n
return n, nil
}
// Close gives a pooled body back to the pool, which Read has only ever
// copied from; Read finds the body ended after it, as it would have.
func (b *bufferedBody) Close() error {
if b.pooled {
bufferpool.Put(b.data)
b.data, b.off, b.pooled = nil, 0, false
}
return nil
}
// StreamHooks ties a ResponseStream to the request it answers, which is the
// transport's own.
type StreamHooks struct {
// Begin is called just before the callback runs, with the stream, which
// the request is to tell of a later failure by Fail. It reports false if
// the request has already ended, and then nothing is called.
Begin func(*ResponseStream) bool
// Callback runs the callback with the response.
Callback func(*ClientResponse)
// Whole is called with the response if its body ended before Begin, so
// that it is delivered as one that arrived whole: with DeliverResponse.
Whole func(*stdhttp.Response)
// Done settles the request once a response that streamed has ended. It
// reports false if the request had ended already.
Done func() bool
// Abort fails the request with err and abandons the exchange.
Abort func(err error)
// Hold stops or resumes the transport's reading, for a transport that
// can stop reading just this response, by holding the connection's.
// Transports that pace the sender by flow control instead leave it nil
// and set Credit.
Hold func(hold bool)
// Credit is told how many bytes of body have left the stream's buffer,
// and gives the sender that much window. Body that arrives before the
// callback runs is credited at once, so that a threshold larger than the
// window cannot stall the sender. It may be called from any goroutine,
// but never while the stream holds a lock the transport's Data and Fail
// calls hold.
Credit func(n int64)
}
// StreamOptions are the limits on a ResponseStream, from the client's
// configuration.
type StreamOptions struct {
// Threshold is how much body to collect before running the callback.
Threshold int64
// Buffer is how many bytes may wait for a reader before Hold stops
// reads; zero means DefaultStreamResponseBodyBuffer.
Buffer int
// Detached keeps OnBody's function off the goroutine that delivers the
// body: it runs on one of its own, and the transport's goroutine only
// queues the data. It is for transports that must go on reading while
// the callback takes its time, as QUIC must, to acknowledge what
// arrives: they pace the sender with Credit, which bounds what waits.
Detached bool
}
// ResponseStream carries the body of a response that streams, from the worker that
// reads the connection to whoever takes it: OnBody's function, or Read.
//
// It is for clients: package http's own, and those of other transports, which
// build one for a response whose body is still arriving, call Start, then Data
// for each piece of the body, in order, and End when it is complete or Fail
// when it will not be. Everything it needs of the request it answers comes
// through StreamHooks.
type ResponseStream struct {
hooks StreamHooks
req *stdhttp.Request
cr *ClientResponse
// threshold is how much body to collect before running the callback, and
// highWater how much may wait unread, after that, before reads stop.
threshold int64
highWater int
detached bool
mu sync.Mutex
// chunks hold body that has arrived and not been taken, oldest first, in
// buffers of streamChunk bytes from the pool; off is how much of the
// first one Read has taken, and buffered how many bytes they hold in all.
// Each chunk is handed over whole and goes back to the pool as soon as
// it has been, so the body waiting costs what is waiting, rather than a
// buffer grown to it a copy at a time and left to the collector.
chunks [][]byte
off int
buffered int
fn BodyFunc
// fired says the callback has been started, inCallback that it is running,
// delivering that a goroutine is inside fn, touched that something took or
// read the body, and discarding that the callback returned without any
// of that, so the rest is thrown away.
fired, inCallback, delivering, touched, discarding bool
dropped int
// ended says all of the body has arrived, err that it never will, and
// finSent that fn has been told so.
ended, finSent bool
err error
// held says the connection's reads are stopped for want of a reader, and
// paused that HoldBody holds the body back from fn.
held bool
paused bool
consumed int64
trailer stdhttp.Header
// preCredited counts what of chunks has been credited already, when
// hooks.Credit paces the sender.
preCredited int64
}
// pushLocked adds b to the body waiting, filling the last chunk first.
func (s *ResponseStream) pushLocked(b []byte) {
s.buffered += len(b)
for len(b) > 0 {
last := len(s.chunks) - 1
if last < 0 || len(s.chunks[last]) == cap(s.chunks[last]) {
s.chunks = append(s.chunks, bufferpool.Get(streamChunk)[:0])
last++
}
tail := s.chunks[last]
n := min(cap(tail)-len(tail), len(b))
s.chunks[last] = append(tail, b[:n]...)
b = b[n:]
}
}
// popLocked takes the oldest chunk off the body waiting, and returns what of
// it is unread, data, and the chunk, which goes back to the pool once data
// has been handed on.
func (s *ResponseStream) popLocked() (data, chunk []byte) {
chunk = s.chunks[0]
data = chunk[s.off:]
n := copy(s.chunks, s.chunks[1:])
s.chunks[n] = nil
s.chunks = s.chunks[:n]
s.off = 0
s.buffered -= len(data)
return data, chunk
}
// dropLocked throws the body waiting away.
func (s *ResponseStream) dropLocked() {
for i, chunk := range s.chunks {
bufferpool.Put(chunk)
s.chunks[i] = nil
}
s.chunks, s.off, s.buffered = s.chunks[:0], 0, 0
}
// takeLocked accounts n bytes leaving chunks, read, delivered or thrown away, and
// returns how many of them the sender is still owed window for.
func (s *ResponseStream) takeLocked(n int) int64 {
if s.hooks.Credit == nil {
return 0
}
c := int64(n)
d := min(c, s.preCredited)
s.preCredited -= d
return c - d
}
// give passes credit on to the sender. It must be called without s.mu held.
func (s *ResponseStream) give(n int64) {
if n > 0 && s.hooks.Credit != nil {
s.hooks.Credit(n)
}
}
// NewResponseStream returns the stream for a response, head, whose body is
// still arriving, to the request req. head.Body is replaced.
func NewResponseStream(head *stdhttp.Response, req *stdhttp.Request, options StreamOptions, hooks StreamHooks) *ResponseStream {
s := &ResponseStream{hooks: hooks, req: req, threshold: options.Threshold, highWater: options.Buffer, detached: options.Detached}
if s.highWater <= 0 {
s.highWater = DefaultStreamResponseBodyBuffer
}
s.cr = &ClientResponse{Response: head, s: s, abort: hooks.Abort}
head.Body = &streamBody{s: s}
return s
}
// Start runs the callback at once if no body need be collected first.
func (s *ResponseStream) Start() {
if s.threshold <= 0 {
s.fire()
}
}
// fire runs the callback, on the goroutine that delivers the body.
func (s *ResponseStream) fire() {
if !s.hooks.Begin(s) {
return
}
s.mu.Lock()
s.fired, s.inCallback = true, true
s.mu.Unlock()
s.hooks.Callback(s.cr)
s.afterCallback()
}
// afterCallback throws the body away if the callback left it with no one, and
// otherwise hands over what it has.
func (s *ResponseStream) afterCallback() {
s.mu.Lock()
s.inCallback = false
if s.fn == nil && !s.touched {
s.discarding = true
c := s.takeLocked(s.buffered)
s.dropLocked()
s.unholdLocked()
s.mu.Unlock()
s.give(c)
return
}
s.mu.Unlock()
if s.detached {
s.kick()
return
}
s.drain()
s.mu.Lock()
s.holdLocked()
s.mu.Unlock()
}
// data takes a piece of the body from the worker that read it.
func (s *ResponseStream) Data(b []byte) {
s.mu.Lock()
if s.err != nil || s.ended {
s.mu.Unlock()
return
}
if s.discarding {
s.dropped += len(b)
over := s.dropped > discardAfterHandler
s.mu.Unlock()
s.give(int64(len(b)))
if over {
s.hooks.Abort(ErrResponseAbandoned)
}
return
}
if s.fn != nil && s.fired && !s.inCallback && !s.delivering && s.buffered == 0 && !s.detached && !s.paused {
// Nobody is behind: hand it over from where it was read.
fn := s.fn
s.delivering = true
s.consumed += int64(len(b))
s.mu.Unlock()
fn(b, false, nil)
s.give(int64(len(b)))
s.mu.Lock()
s.delivering = false
more := s.buffered > 0 || s.err != nil
s.mu.Unlock()
if more {
s.drain()
}
return
}
s.pushLocked(b)
var early int64
if !s.fired && s.hooks.Credit != nil {
early = int64(len(b))
s.preCredited += early
}
fire := !s.fired && int64(s.buffered) >= s.threshold
if !fire {
s.holdLocked()
}
s.mu.Unlock()
s.give(early)
if fire {
s.fire()
} else if s.detached {
s.kick()
}
}
// end is told that all of the body has arrived. A response that never ran its
// callback does so now, as one that arrived whole.
func (s *ResponseStream) End() {
s.mu.Lock()
s.ended = true
s.trailer = s.cr.Response.Trailer
s.unholdLocked()
if !s.fired {
var body []byte
if s.buffered > 0 {
// The body keeps no more than its size, and the chunks go back.
body = make([]byte, 0, s.buffered)
for i, chunk := range s.chunks {
if i == 0 {
chunk = chunk[s.off:]
}
body = append(body, chunk...)
}
}
s.dropLocked()
s.mu.Unlock()
resp := s.cr.Response
if len(body) == 0 {
resp.Body = stdhttp.NoBody
} else {
resp.Body = &bufferedBody{data: body}
}
if s.hooks.Credit != nil && resp.ContentLength < 0 && s.req.Method != stdhttp.MethodHead {
// What an HTTP/2 response that arrived whole reports.
resp.ContentLength = int64(len(body))
}
s.hooks.Whole(resp)
return
}
s.mu.Unlock()
if s.hooks.Done() {
s.deliver()
}
}
// fail ends the body with err, which fn hears of and Read reports.
func (s *ResponseStream) Fail(err error) {
s.mu.Lock()
if s.err == nil && !s.finSent {
s.err = err
}
s.dropLocked()
s.unholdLocked()
s.mu.Unlock()
s.deliver()
}
func (s *ResponseStream) setFn(fn BodyFunc) {
s.mu.Lock()
if s.discarding {
s.mu.Unlock()
sidepool.Go(func() { fn(nil, true, ErrResponseAbandoned) })
return
}
s.fn, s.touched = fn, true
late := !s.inCallback
s.mu.Unlock()
switch {
case late && s.detached:
s.kick()
case late:
sidepool.Go(s.drain)
}
}
// hold is ClientResponse.HoldBody for a body that streams.
func (s *ResponseStream) hold(hold bool) {
s.mu.Lock()
resume := s.paused && !hold
s.paused = hold
s.mu.Unlock()
switch {
case !resume:
case s.detached:
s.kick()
default:
sidepool.Go(s.drain)
}
}
// deliver hands fn what has arrived: on this goroutine, or, for a stream
// that is detached, on one of its own.
func (s *ResponseStream) deliver() {
if s.detached {
s.kick()
} else {
s.drain()
}
}
// kick starts a goroutine to drain the stream, unless one is in fn already,
// or the callback is still running, which carries on with it when it
// returns.
func (s *ResponseStream) kick() {
s.mu.Lock()
if s.delivering || s.inCallback || s.fn == nil || s.paused && s.err == nil {
s.mu.Unlock()
return
}
s.delivering = true
s.mu.Unlock()
// Not a pool's goroutine: fn may take as long as it likes. Nor one that
// recovers what it panics with, so this one does, as a handler's are
// recovered from: a callback that panics ends its exchange, not the
// process.
go func() {
defer func() {
if recovered := recover(); recovered != nil {
log.Error("http: response body callback panicked",
"panic", recovered, "stack", log.Lines(debug.Stack()))
s.callbackPanicked()
}
}()
s.mu.Lock()
s.drainLocked()
}()
}
// callbackPanicked gives up a stream whose detached callback panicked, which
// it did with s.mu released: fn hears nothing more, the body still waiting
// is thrown away, reads go on, and the request fails, abandoning the
// exchange.
func (s *ResponseStream) callbackPanicked() {
s.mu.Lock()
s.delivering, s.discarding, s.finSent = false, true, true
if s.err == nil {
s.err = ErrResponseAbandoned
}
c := s.takeLocked(s.buffered)
s.dropLocked()
s.unholdLocked()
s.mu.Unlock()
s.give(c)
s.hooks.Abort(ErrResponseAbandoned)
}
// drain hands fn what has arrived, then the end, one call at a time. Whoever
// finds nobody inside fn does it, and the one inside fn carries on with
// whatever arrives meanwhile.
func (s *ResponseStream) drain() {
s.mu.Lock()
if s.delivering || s.inCallback || s.fn == nil {
s.mu.Unlock()
return
}
s.delivering = true
s.drainLocked()
}
// drainLocked is drain's loop, entered with s.mu held and delivering set,
// and left with s.mu released.
func (s *ResponseStream) drainLocked() {
for {
fn := s.fn
switch {
case s.buffered > 0 && s.err == nil && !s.paused:
data, chunk := s.popLocked()
s.consumed += int64(len(data))
c := s.takeLocked(len(data))
s.mu.Unlock()
fn(data, false, nil)
bufferpool.Put(chunk)
s.give(c)
s.mu.Lock()
continue
case s.err != nil && !s.finSent:
s.finSent = true
err := s.err
s.mu.Unlock()
fn(nil, true, err)
s.mu.Lock()
continue
case s.ended && !s.finSent && s.err == nil && !s.paused:
s.finSent = true
s.mu.Unlock()
fn(nil, true, nil)
s.mu.Lock()
continue
}
break
}
s.delivering = false
s.holdLocked()
s.mu.Unlock()
}
// holdLocked stops the transport's reads while more waits for a reader than
// the buffer allows, and starts them again once it has gone down by half.
func (s *ResponseStream) holdLocked() {
if s.hooks.Hold == nil {
return
}
switch {
case !s.held && s.fired && !s.ended && s.err == nil && s.buffered > s.highWater:
s.held = true
s.hooks.Hold(true)
case s.held && s.buffered <= s.highWater/2:
s.held = false
s.hooks.Hold(false)
}
}
func (s *ResponseStream) unholdLocked() {
if s.held {
s.held = false
s.hooks.Hold(false)
}
}
func (s *ResponseStream) complete() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.ended
}
func (s *ResponseStream) trailerFields() stdhttp.Header {
s.mu.Lock()
defer s.mu.Unlock()
return s.trailer
}
func (s *ResponseStream) consumedBytes() int64 {
s.mu.Lock()
defer s.mu.Unlock()
return s.consumed
}
// streamBody is the Body of a response that streams. It reads what has
// arrived and never waits for more: with nothing to read and the body not
// over, it returns ErrWouldBlock, and the data is better taken with OnBody.
// Closing it does nothing; ClientResponse.Close gives the response up.
type streamBody struct{ s *ResponseStream }
func (b *streamBody) Read(p []byte) (int, error) {
s := b.s
s.mu.Lock()
switch {
case s.fn != nil:
s.mu.Unlock()
return 0, ErrBodyTaken
case s.discarding:
s.mu.Unlock()
return 0, ErrResponseAbandoned
}
s.touched = true
if s.buffered > 0 {
n := 0
for n < len(p) && s.buffered > 0 {
k := copy(p[n:], s.chunks[0][s.off:])
n += k
s.off += k
s.buffered -= k
if s.off == len(s.chunks[0]) {
// Read all of it: the chunk goes back.
_, chunk := s.popLocked()
bufferpool.Put(chunk)
}
}
s.consumed += int64(n)
c := s.takeLocked(n)
s.holdLocked()
s.mu.Unlock()
s.give(c)
return n, nil
}
defer s.mu.Unlock()
switch {
case s.err != nil:
return 0, s.err
case s.ended:
return 0, io.EOF
}
return 0, ErrWouldBlock
}
func (b *streamBody) Close() error { return nil }