-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathhttp_response_reader.go
More file actions
147 lines (129 loc) · 2.56 KB
/
Copy pathhttp_response_reader.go
File metadata and controls
147 lines (129 loc) · 2.56 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
package copilot
import (
"context"
"errors"
"io"
"runtime"
"sync"
)
const httpResponseReadAheadSize = 32 * 1024
// httpResponseReader keeps one bounded buffer of response bytes ready while
// the preceding chunk waits for its runtime acknowledgement.
type httpResponseReader struct {
body io.ReadCloser
mu sync.Mutex
buffered []byte
err error
notify chan struct{}
space chan struct{}
stop chan struct{}
done chan struct{}
closeOnce sync.Once
}
func newHTTPResponseReader(body io.ReadCloser) *httpResponseReader {
r := &httpResponseReader{
body: body,
buffered: make([]byte, 0, httpResponseReadAheadSize),
notify: make(chan struct{}, 1),
space: make(chan struct{}, 1),
stop: make(chan struct{}),
done: make(chan struct{}),
}
go r.readLoop()
return r
}
func (r *httpResponseReader) readLoop() {
defer close(r.done)
scratch := make([]byte, httpResponseReadAheadSize)
for {
select {
case <-r.stop:
return
default:
}
r.mu.Lock()
remaining := httpResponseReadAheadSize - len(r.buffered)
stopped := r.err != nil
r.mu.Unlock()
if stopped {
return
}
if remaining == 0 {
select {
case <-r.space:
continue
case <-r.stop:
return
}
}
n, err := r.body.Read(scratch[:remaining])
r.mu.Lock()
if n > 0 {
r.buffered = append(r.buffered, scratch[:n]...)
}
if err != nil {
r.err = err
}
r.mu.Unlock()
if n > 0 || err != nil {
signal(r.notify)
}
if err != nil {
return
}
if n == 0 {
// Broken readers are permitted to return (0, nil). Do not let one
// starve acknowledgement or cancellation processing.
runtime.Gosched()
}
}
}
func (r *httpResponseReader) nextChunk(ctx context.Context, output *[]byte) (bool, error) {
for {
select {
case <-ctx.Done():
return false, ctx.Err()
default:
}
r.mu.Lock()
if len(r.buffered) > 0 {
replacement := (*output)[:0]
if cap(replacement) < httpResponseReadAheadSize {
replacement = make([]byte, 0, httpResponseReadAheadSize)
}
*output, r.buffered = r.buffered, replacement
r.mu.Unlock()
signal(r.space)
return true, nil
}
err := r.err
if err != nil {
r.err = nil
}
r.mu.Unlock()
if err != nil {
if errors.Is(err, io.EOF) {
return false, nil
}
return false, err
}
select {
case <-r.notify:
case <-ctx.Done():
return false, ctx.Err()
}
}
}
func (r *httpResponseReader) Close() {
r.closeOnce.Do(func() {
close(r.stop)
_ = r.body.Close()
<-r.done
})
}
func signal(ch chan struct{}) {
select {
case ch <- struct{}{}:
default:
}
}