zhttp

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 15 Imported by: 2

Documentation

Index

Constants

View Source
const (
	MaxJitter = 1.0
	NoJitter  = 0.0
)

Variables

View Source
var (
	MaxRetry  = 3
	RetryUnit = 100 * time.Millisecond
	RetryCap  = time.Second
	Random    = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
)
View Source
var DefaultClient = &http.Client{
	Transport: Transport(false, nil),
}
View Source
var (
	DefaultRespParse = &defaultParse{}
)
View Source
var SuccessStatusCodes = map[int]struct{}{
	http.StatusOK:             {},
	http.StatusNoContent:      {},
	http.StatusPartialContent: {},
}

Functions

func HeaderMap

func HeaderMap(h http.Header) map[string]string

func IsHTTPStatusRetryable

func IsHTTPStatusRetryable(httpStatusCode int) (ok bool)

func IsHTTPSuccessStatus

func IsHTTPSuccessStatus(httpStatusCode int) (ok bool)

func NewRetryTimer

func NewRetryTimer(ctx context.Context, opts ...func(option *RetryOption)) <-chan int

func ParseProxy

func ParseProxy(proxy string) (*url.URL, error)

func Transport

func Transport(secure bool, proxy func(*http.Request) (*url.URL, error)) *http.Transport

Types

type DefaultRequest

type DefaultRequest struct{}

func (*DefaultRequest) ContextDo

func (*DefaultRequest) ContextDo(ctx context.Context, r *http.Request) (*http.Response, error)

func (*DefaultRequest) Do

type Request

type Request interface {
	Do(*http.Request) (*http.Response, error)
	ContextDo(context.Context, *http.Request) (*http.Response, error)
}

func NewRequest

func NewRequest(opts ...RequestOption) Request

type RequestOption

type RequestOption func(r *DefaultRequest)

type ResponseParse

type ResponseParse interface {
	Parse(*http.Response, any) error
	ParseHeader(*http.Response, any) error
	ParseBody(*http.Response, any) error
}

type Retry

type Retry func(option *RetryOption)

func WithJitter

func WithJitter(jitter float64) Retry

func WithMaxRetry

func WithMaxRetry(maxRetry int) Retry

func WithRandom

func WithRandom(random *rand.Rand) Retry

func WithRetryCap

func WithRetryCap(retryCap time.Duration) Retry

func WithRetryUnit

func WithRetryUnit(retryUnit time.Duration) Retry

type RetryOption

type RetryOption struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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