Documentation ¶
Overview ¶
Package rangetripper provides a performant http.RoundTripper that handles byte-range downloads if the resulting HTTP server claims to support them in a HEAD request for the file. RangeTripper will download 1/Nth of the file asynchronously with each of the “fileChunks“ specified in a New. N+1 actual downloaders are most likely as the +1 covers any gap from non-even division of content-length.
Index ¶
- Constants
- type Client
- type RangeTripper
- func (rt *RangeTripper) Do(r *http.Request) (*http.Response, error)
- func (rt *RangeTripper) RoundTrip(r *http.Request) (*http.Response, error)
- func (rt *RangeTripper) SetChunkSize(chunkBytes int64)
- func (rt *RangeTripper) SetClient(client Client)
- func (rt *RangeTripper) SetMax(max int)
- func (rt *RangeTripper) WithProgress() <-chan int64
- type RetryClient
Examples ¶
Constants ¶
const ( ContentLengthNumericError = rtError("Content-Length value cannot be converted to a number") ContentLengthMismatchError = rtError("downloaded file size does not match content-length") SingleRequestExhaustedError = rtError("one request has already been made with this RangeTripper") )
Static errors to return
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
Client is an interface that could refer to an http.Client or a rangetripper.RetryClient
DefaultClient is what RangeTripper will use to actually make the individual GET requests. Change the values to change the outcome. Don't set the DefaultClient's Client.Transport to a RangeTripper, or :mindblown:. DefaultClient can be a lowly http.Client if you prefer
type RangeTripper ¶
type RangeTripper struct { TimingsOut *log.Logger DebugOut *log.Logger // contains filtered or unexported fields }
RangeTripper is an http.RoundTripper to be used in an http.Client. This should not be used in its default state, instead by its New functions. A single RangeTripper *must* only be used for one request.
Example ¶
// Set up a temporary file tfile, err := os.CreateTemp("/tmp", "rt") if err != nil { panic(err) } defer os.Remove(tfile.Name()) // clean up after ourselves client := new(http.Client) // make a new Client rt, _ := New(10, tfile.Name()) // make a new RangeTripper (errors ignored for brevity. Don't be dumb) client.Transport = rt // Use the RangeTripper as the Transport if _, err := client.Get("https://google.com/"); err != nil { panic(err) } // tfile is the google homepage
Output:
func New ¶
func New(fileChunks int, outputFilePath string) (*RangeTripper, error)
New simply returns a RangeTripper or an error. Logged messages are discarded.
func NewWithLoggers ¶
func NewWithLoggers(fileChunks int, outputFilePath string, timingLogger, debugLogger *log.Logger) (*RangeTripper, error)
NewWithLoggers returns a RangeTripper or an error. Logged messages are sent to the specified Logger, or discarded if nil.
func (*RangeTripper) Do ¶ added in v1.1.0
Do is a satisfier of the rangetripper.Client interface, and is identical to RoundTrip
func (*RangeTripper) RoundTrip ¶
RoundTrip is called with a formed Request, writing the Body of the Response to to the specified output file. The Response should be ignored, but errors are important. Both the Request.Body and the RangeTripper.outFile will be closed when this function returns.
func (*RangeTripper) SetChunkSize ¶ added in v1.6.0
func (rt *RangeTripper) SetChunkSize(chunkBytes int64)
SetChunkSize overrides the “fileChunks“ and instead will divide the resulting Content-Length by this to determine the appropriate chunk count dynamically. “fileChunks“ will still be used to guide the maximum number of concurrent workers, unless “SetMax()“ is used.
func (*RangeTripper) SetClient ¶
func (rt *RangeTripper) SetClient(client Client)
SetClient allows for overriding the Client used to make the requests.
func (*RangeTripper) SetMax ¶ added in v1.3.0
func (rt *RangeTripper) SetMax(max int)
SetMax allows for setting the maximum number of concurrently-running workers
func (*RangeTripper) WithProgress ¶ added in v1.5.0
func (rt *RangeTripper) WithProgress() <-chan int64
WithProgress returns a read-only chan that will first provide the total length of the content (in bytes), followed by a stream of completed byte-lengths. CAUTION: It is a generally bad idea to call this and then ignore the resulting channel.
type RetryClient ¶
type RetryClient struct {
// contains filtered or unexported fields
}
RetryClient contains variables and methods to use when making smarter HTTP requests
func NewRetryClient ¶
func NewRetryClient(retries int, every, timeout time.Duration) *RetryClient
NewRetryClient returns a RetryClient that will retry failed requests “retries“ times, every “every“, and use “timeout“ as a timeout
func NewRetryClientWithExponentialBackoff ¶
func NewRetryClientWithExponentialBackoff(retries int, initially, timeout time.Duration) *RetryClient
NewRetryClientWithExponentialBackoff returns a RetryClient that will retry failed requests “retries“ times, first after “initially“ and exponentially longer each time, and use “timeout“ as a timeout