Documentation ¶
Index ¶
- func ContinueWith(t http.RoundTripper, a ...Sender) http.RoundTripper
- func ParseRetryAfter(resp *http.Response, now time.Time) time.Duration
- func RetryCount(resp *http.Response) int
- func WithTransport(p Sender, t http.RoundTripper) http.RoundTripper
- type ClosableTransport
- type DumpTransport
- type RateLimitTransport
- type RetriableTransport
- type RoundTripperFunc
- type SemaphoreTransport
- type Sender
- type SenderFunc
- type Waiter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContinueWith ¶
func ContinueWith(t http.RoundTripper, a ...Sender) http.RoundTripper
ContinueWith combines HTTP transaction.
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/lufia/httpclientutil" ) func main() { f1 := httpclientutil.SenderFunc(func(r *http.Request, t http.RoundTripper) (*http.Response, error) { fmt.Println("A1") defer fmt.Println("A2") return t.RoundTrip(r) }) f2 := httpclientutil.SenderFunc(func(r *http.Request, t http.RoundTripper) (*http.Response, error) { fmt.Println("B1") defer fmt.Println("B2") return t.RoundTrip(r) }) f3 := httpclientutil.SenderFunc(func(r *http.Request, t http.RoundTripper) (*http.Response, error) { fmt.Println("C1") defer fmt.Println("C2") return t.RoundTrip(r) }) var c http.Client c.Transport = httpclientutil.ContinueWith(c.Transport, f1, f2) c.Transport = httpclientutil.ContinueWith(c.Transport, f3) s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello")) })) defer s.Close() resp, _ := c.Get(s.URL) b, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(b)) resp.Body.Close() }
Output: C1 B1 A1 A2 B2 C2 hello
func RetryCount ¶ added in v1.1.0
func WithTransport ¶
func WithTransport(p Sender, t http.RoundTripper) http.RoundTripper
WithTransport returns a new RoundTripper. New RoundTripper is set t onto p as a parent RoundTripper.
Types ¶
type ClosableTransport ¶
type ClosableTransport struct { Transport http.RoundTripper // contains filtered or unexported fields }
func (*ClosableTransport) Close ¶
func (p *ClosableTransport) Close() error
Close closes transport and waits all requests is done.
type DumpTransport ¶
type DumpTransport struct { Transport http.RoundTripper Output io.Writer WithoutBody bool }
type RateLimitTransport ¶
type RateLimitTransport struct { Transport http.RoundTripper Interval time.Duration Limit int // contains filtered or unexported fields }
RateLimitTransport limits requests through transport. This will blocks requests over limit until decrease of rate.
type RetriableTransport ¶
type RetriableTransport struct { // Peak is maximum duration. Zero is no limit. Peak time.Duration // Initial is initial duration. Initial time.Duration // Limit is maximum retry count. Limit int // MaxAge is maximum time until transport is force expired. MaxAge time.Duration // NewWaiter overrides above parameters. NewWaiter func(r *http.Request) Waiter Transport http.RoundTripper }
RetriableTransport retries a request that is faile such as 429, 500, 503, or 504. And, This retries too when the RoundTripper that is setted to Transport field returns an temporary error that implement Temporary() bool.
type RoundTripperFunc ¶
RoundTripperFunc type is an adapter to allow the use of ordinary functions as HTTP round trippers. If f is a function with the appropriate signature, RoundTripperFunc(f) is a http.RoundTripper that calls f.
type SemaphoreTransport ¶
type SemaphoreTransport struct { Transport http.RoundTripper Limit int // contains filtered or unexported fields }
SemaphoreTransport restricts number of concurrent requests up to Limit.
Go 1.11 or later, you could use http.Client.MaxConnsPerHost.
type SenderFunc ¶
SenderFunc type is an adapter to allow the use of ordinary functions as HTTP round trippers.
func (SenderFunc) Send ¶
func (f SenderFunc) Send(req *http.Request, t http.RoundTripper) (*http.Response, error)
Send executes a single HTTP transaction. t always isn't nil