Skip to content

Commit 155998a

Browse files
committed
fix: client prompt return on context cancellation
Signed-off-by: Umegbewe Nwebedu <nwebedujunior55@gmail.com>
1 parent 96a5ad6 commit 155998a

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

api/client.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,36 +132,26 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
132132
req = req.WithContext(ctx)
133133
}
134134
resp, err := c.client.Do(req)
135-
defer func() {
136-
if resp != nil {
137-
_, _ = io.Copy(io.Discard, resp.Body)
138-
_ = resp.Body.Close()
139-
}
140-
}()
141-
142135
if err != nil {
143136
return nil, nil, err
144137
}
145138

146139
var body []byte
147-
done := make(chan struct{})
140+
done := make(chan error, 1)
148141
go func() {
149142
var buf bytes.Buffer
150-
// TODO(bwplotka): Add LimitReader for too long err messages (e.g. limit by 1KB)
151-
_, err = buf.ReadFrom(resp.Body)
143+
_, err := buf.ReadFrom(resp.Body)
152144
body = buf.Bytes()
153-
close(done)
145+
done <- err
154146
}()
155147

156148
select {
157149
case <-ctx.Done():
150+
resp.Body.Close()
158151
<-done
159-
err = resp.Body.Close()
160-
if err == nil {
161-
err = ctx.Err()
162-
}
163-
case <-done:
152+
return resp, nil, ctx.Err()
153+
case err = <-done:
154+
resp.Body.Close()
155+
return resp, body, err
164156
}
165-
166-
return resp, body, err
167157
}

0 commit comments

Comments
 (0)