This is http.RoundTripper transport wrapper that handles network errors while download request body. When connection problem occurs it repeats request to server with set Range header and continue download response body.
Not production ready yet.
Just put new wrapper instance to client's transport:
wrapper := pertr.New()
cli := http.Client{Transport: wrapper}
rsp, err := cli.Do(req)
You can use custom context to manage retry timeout. If context will expire download will continue until next error occured.
cxt := context.WithTimeout(context.Background(), 15 * time.Minute)
wrapper := pertr.New(pertr.WithContext(ctx))
If context specified wrapper cannot be neither reused or used in parallel operations. To reuse wrapper just specify new context with:
wrapper.SetContext(newctx)
Please do not call SetContext()
until download operation finished.
Wrapper can use custom transport (http.RoundTripper) for outgoing calls:
mytr := http.Transport{}
wrapper := pertr.New(pertr.WithTransport(&mytr))
if custom transport not specified http.DefaultTransport
will be used.
Body.Read()
will return first error from original first request.
-
This wrapper will repeat request to server. Please use it with caution.
-
Wrapper will try to repeat request if server supports resume downloading. It will detects if server responds with
http.StatusPartialContent
in first retry request. -
Go docs says that http.RoundTripper should not attempt to interpret the response. However, this library does this.