@@ -3,6 +3,7 @@ package net
3
3
import (
4
4
"bytes"
5
5
"crypto/tls"
6
+ "errors"
6
7
"fmt"
7
8
"io"
8
9
"net/http"
@@ -15,6 +16,7 @@ import (
15
16
16
17
"github.com/opentracing/opentracing-go"
17
18
"github.com/opentracing/opentracing-go/ext"
19
+ skpio "github.com/zalando/skipper/io"
18
20
"github.com/zalando/skipper/logging"
19
21
"github.com/zalando/skipper/secrets"
20
22
)
@@ -24,43 +26,7 @@ const (
24
26
defaultRefreshInterval = 5 * time .Minute
25
27
)
26
28
27
- type mybuf struct { * bytes.Buffer }
28
-
29
- func (buf * mybuf ) Close () error {
30
- return nil
31
- }
32
-
33
- type copyBodyStream struct {
34
- left int
35
- buf * mybuf
36
- input io.ReadCloser
37
- }
38
-
39
- func newCopyBodyStream (left int , buf * bytes.Buffer , rc io.ReadCloser ) * copyBodyStream {
40
- return & copyBodyStream {
41
- left : left ,
42
- buf : & mybuf {Buffer : buf },
43
- input : rc ,
44
- }
45
- }
46
-
47
- func (cb * copyBodyStream ) Read (p []byte ) (n int , err error ) {
48
- n , err = cb .input .Read (p )
49
- if cb .left > 0 && n > 0 {
50
- m := min (n , cb .left )
51
- cb .buf .Write (p [:m ])
52
- cb .left -= m
53
- }
54
- return n , err
55
- }
56
-
57
- func (cb * copyBodyStream ) Close () error {
58
- return cb .input .Close ()
59
- }
60
-
61
- func (cb * copyBodyStream ) GetBody () io.ReadCloser {
62
- return cb .buf
63
- }
29
+ var errRequestNotFound = errors .New ("request not found" )
64
30
65
31
// Client adds additional features like Bearer token injection, and
66
32
// opentracing to the wrapped http.Client with the same interface as
@@ -166,8 +132,8 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
166
132
req .Header .Set ("Authorization" , "Bearer " + string (b ))
167
133
}
168
134
}
169
- if req .Body != nil && req .Body != http .NoBody {
170
- retryBuffer := newCopyBodyStream (int (req .ContentLength ), & bytes.Buffer {}, req .Body )
135
+ if req .Body != nil && req .Body != http .NoBody && req . ContentLength > 0 {
136
+ retryBuffer := skpio . NewCopyBodyStream (int (req .ContentLength ), & bytes.Buffer {}, req .Body )
171
137
c .retryBuffers .Store (req , retryBuffer )
172
138
req .Body = retryBuffer
173
139
}
@@ -179,20 +145,20 @@ func (c *Client) Retry(req *http.Request) (*http.Response, error) {
179
145
return c .Do (req )
180
146
}
181
147
182
- if rc , err := req .GetBody (); err == nil {
183
- println ("req.GetBody() case" )
184
- c .retryBuffers .Delete (req )
185
- req .Body = rc
186
- return c .Do (req )
187
- }
148
+ // Next line panics on TestClientRetryBodyHalfReader
149
+ // if rc, err := req.GetBody(); err == nil {
150
+ // c.retryBuffers.Delete(req)
151
+ // req.Body = rc
152
+ // return c.Do(req)
153
+ // }
154
+ // return nil, fmt.Errorf("failed to retry")
188
155
189
- println ("our own retry buffer impl" )
190
156
buf , ok := c .retryBuffers .Load (req )
191
157
if ! ok {
192
- return nil , fmt .Errorf ("no retry possible, request not found : %s %s" , req .Method , req .URL )
158
+ return nil , fmt .Errorf ("no retry possible, %w : %s %s" , errRequestNotFound , req .Method , req .URL )
193
159
}
194
160
195
- retryBuffer , ok := buf .(* copyBodyStream )
161
+ retryBuffer , ok := buf .(* skpio. CopyBodyStream )
196
162
if ! ok {
197
163
return nil , fmt .Errorf ("no retry possible, no retry buffer for request: %s %s" , req .Method , req .URL )
198
164
}
0 commit comments