Skip to content
/ pertr Public

Go (net/http).Transport wrapper with auto resume download HTTP response body

License

Notifications You must be signed in to change notification settings

belolap/pertr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pertr

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.

Usage

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.

Errors

Body.Read() will return first error from original first request.

Notes

  1. This wrapper will repeat request to server. Please use it with caution.

  2. Wrapper will try to repeat request if server supports resume downloading. It will detects if server responds with http.StatusPartialContent in first retry request.

  3. Go docs says that http.RoundTripper should not attempt to interpret the response. However, this library does this.

About

Go (net/http).Transport wrapper with auto resume download HTTP response body

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages