httpclientutil

package module
Version: v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2018 License: BSD-3-Clause Imports: 14 Imported by: 0

README

httpclientutil

GoDoc Build Status

Documentation

Index

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 ParseRetryAfter

func ParseRetryAfter(resp *http.Response, now time.Time) time.Duration

func RetryCount added in v1.1.0

func RetryCount(resp *http.Response) int

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.

func (*ClosableTransport) RoundTrip

func (p *ClosableTransport) RoundTrip(req *http.Request) (*http.Response, error)

type DumpTransport

type DumpTransport struct {
	Transport   http.RoundTripper
	Output      io.Writer
	WithoutBody bool
}

func (*DumpTransport) RoundTrip

func (t *DumpTransport) RoundTrip(req *http.Request) (*http.Response, error)

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.

func (*RateLimitTransport) RoundTrip

func (t *RateLimitTransport) RoundTrip(req *http.Request) (*http.Response, error)

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.

func (*RetriableTransport) RoundTrip

func (p *RetriableTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface. This returns a response with X-Retry-Count header.

type RoundTripperFunc

type RoundTripperFunc func(*http.Request) (*http.Response, error)

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.

func (RoundTripperFunc) RoundTrip

func (f RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)

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.

func (*SemaphoreTransport) RoundTrip

func (t *SemaphoreTransport) RoundTrip(req *http.Request) (*http.Response, error)

type Sender

type Sender interface {
	Send(req *http.Request, next http.RoundTripper) (*http.Response, error)
}

Sender is an interface representing the ability to execute a single HTTP transaction.

type SenderFunc

type SenderFunc func(req *http.Request, next http.RoundTripper) (*http.Response, error)

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

type Waiter

type Waiter interface {
	Wait(ctx context.Context) error
	SetNext(d time.Duration)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL