Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 56 additions & 34 deletions github/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -241,49 +242,70 @@ func NewRetryTransport(rt http.RoundTripper, options ...RetryTransportOption) *R
func (t *RetryTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var err error
var resp *http.Response
var dataBuffer *bytes.Reader
var data []byte

for retry := 0; retry <= t.maxRetries; retry++ {
// Reset the body
// Code from httpretry (https://github.com/ybbus/httpretry/blob/master/roundtripper.go#L60)
// if request provides GetBody() we use it as Body,
// because GetBody can be retrieved arbitrary times for retry
if req.GetBody != nil {
bodyReadCloser, _ := req.GetBody()
req.Body = bodyReadCloser
} else if req.Body != nil {

// we need to store the complete body, since we need to reset it if a retry happens
// but: not very efficient because:
// a) huge stream data size will all be buffered completely in the memory
// imagine: 1GB stream data would work efficiently with io.Copy, but has to be buffered completely in memory
// b) unnecessary if first attempt succeeds
// a solution would be to at least support more types for GetBody()

// store it for the first time
if dataBuffer == nil {
data, err := io.ReadAll(req.Body)
req.Body.Close()
if err != nil {
return nil, err
}
dataBuffer = bytes.NewReader(data)
req.ContentLength = int64(dataBuffer.Len())
req.Body = io.NopCloser(dataBuffer)
if err := req.Context().Err(); err != nil {
// Before the first attempt we still own the request body. Once
// sent, the underlying transport is responsible for closing it.
if retry == 0 && req.Body != nil {
_ = req.Body.Close()
}
return nil, err
}

// reset the request body
if _, err = dataBuffer.Seek(0, io.SeekStart); err != nil {
return nil, err
// Shallow copy so we never mutate the caller's Request.
reqCopy := new(*req)

if reqCopy.Body != nil && reqCopy.Body != http.NoBody {
if req.GetBody != nil {
// The first attempt uses the original body. Later attempts need a
// fresh body so the request can be sent again.
if retry > 0 {
reqCopy.Body, err = req.GetBody()
if err != nil {
return nil, fmt.Errorf("failed to rewind request body: %w", err)
}
Comment thread
stevehipwell marked this conversation as resolved.
Resolved
}
} else {
// Without GetBody, buffer the body once and use a fresh reader for
// every attempt. This preserves the legacy transport's fallback for
// streaming bodies without modifying the caller's Request.
if retry == 0 {
// There is no size limit: large bodies may consume significant memory.
data, err = io.ReadAll(req.Body)
Comment thread
stevehipwell marked this conversation as resolved.
Resolved
_ = req.Body.Close()
if err != nil {
return nil, err
}
}
reqCopy.ContentLength = int64(len(data))
reqCopy.Body = io.NopCloser(bytes.NewReader(data))
}
}

resp, err = t.transport.RoundTrip(req)
if resp != nil && !t.retryableErrors[resp.StatusCode] {
return resp, err
resp, err = t.transport.RoundTrip(reqCopy)
if err != nil {
// A RoundTripper must not return a response alongside an error,
// but discard it if one is returned so it cannot leak.
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
resp = nil
} else if !t.retryableErrors[resp.StatusCode] {
return resp, nil
}

time.Sleep(t.retryDelay)
if retry < t.maxRetries {
// Close the intermediate response body before retrying, otherwise its
// connection and resources are leaked. Draining is unnecessary with
// Go 1.27 and could block indefinitely on a stalled response body.
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
resp = nil
sleep(req.Context(), t.retryDelay)
}
}

return resp, err
Expand Down
Loading
Loading