Documentation ¶
Overview ¶
Package reqstrategy provides functions for coordinating http.Client calls. It wraps typical call strategies like making simultaneous requests, retrying, racing etc. Package is not aiming to replace standard library's client and Request types only provides the way to invoke them in different manners
First, define what response counts successful, there are two helpers adding rules to request context
req, _ = http.NewRequest("GET", "http://localhost/", nil) req = WithStatusRequired(req, 200, 404)
or do adavanced validation using custom logic
req, _ = http.NewRequest("GET", "http://localhost/", nil) req = WithValidator(req, func(resp *http.Response) error { if resp.StatusCode != http.StatusOK { return fmt.Error("oops") } return nil })
then use one of the Do, Race, All, Retry to invoke the request with following response validation
Index ¶
- func All(client *http.Client, requests ...*http.Request) ([]*http.Response, error)
- func Do(client *http.Client, request *http.Request) (*http.Response, error)
- func Race(client *http.Client, requests ...*http.Request) (*http.Response, error)
- func Retry(client *http.Client, request *http.Request, intervals ...time.Duration) (*http.Response, error)
- func Some(client *http.Client, requests ...*http.Request) ([]*http.Response, error)
- func WithStatusRequired(r *http.Request, codes ...int) *http.Request
- func WithValidator(r *http.Request, validate validator) *http.Request
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All runs requests simultaneously returning responses in same order or error if at least one request failed. Once result is determined all requests are cancelled through the context.
func Do ¶
Do is not much different from calling client.Do(request) except it runs the response validation. See WithValidator and WithSTatusRequired
func Race ¶
Race runs requests simultaneously returning first successulf result or error if all failed. Once result is determined all requests are cancelled through the context.
func Retry ¶
func Retry(client *http.Client, request *http.Request, intervals ...time.Duration) (*http.Response, error)
Retry re-attempts request with provided intervals. By manually providing intervals sequence you can have different wait strategies like exponential back-off (time.Second, 2 * time.Second, 4 * time.Second) or just multiple reties after same interval (time.Second, time.Second, time.Second). If Request had a context with timeout cancelation then it will be applied to entire chain
func Some ¶
Some runs requests simultaneously returning responses for successful requests and <nil> for failed ones. Error is returned only if all requests failed.
func WithStatusRequired ¶
WithStatusRequired adds the response validator by listing acceptable status codes
Types ¶
This section is empty.