network

package
v0.0.0-...-f1100e8 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = http.Client{
	Timeout:   time.Second * 30,
	Transport: tr,
}
View Source
var (
	// ErrRetryRequest is the error reported by Error handler if error is considered insignificant and request might be
	// re-attempted again.
	ErrRetryRequest = errors.New("retry request")
)

Functions

func Download

func Download(url string, dest string, options ...Option) (written int64, err error)

Download sends a request to a given URL and writes response body to the destination. It does not account for status codes and will write any body it receives without errors.

It will be removed in the future when the better APIs are available. Avoid using it.

func FormPostRequest

func FormPostRequest(url string, form url.Values) (*http.Request, error)

func IsNetworkError

func IsNetworkError(e error) bool

func PerformRequest

func PerformRequest(request *http.Request, options ...Option) (*http.Response, error)

func PostRequest

func PostRequest(url string, contentType string, body io.Reader) (*http.Request, error)

Types

type ActionOptions

type ActionOptions struct {
	// Error handlers defined in sequential order. If error occurs during the execution of an action, then the first
	// handler is called, if it returns an error that is not ErrRetryRequest, then this error is returned to the
	// caller of the action. If the error is ErrRetryRequest, then the action is repeated. If no error is returned,
	// then the next handler is called. If no handlers handle the error, then the error is returned to the caller.
	ErrorHandlers []ErrorHandler
	// HTTP client used to execute the action.
	Client *http.Client
}

type ErrorHandler

type ErrorHandler func(e error) error

ErrorHandler handles errors that occur during the execution of an action. It may return an error if it needs a raise, or ErrRetryRequest error, if request can be repeated. If nil is returned, then the next error handler is called.

type Option

type Option func(*http.Request, *ActionOptions)

func WithRetries

func WithRetries(options ...RetrierOption) Option

type RequestRetrierOptions

type RequestRetrierOptions struct {
	MaxRetries            int              // Maximum number of retries, 0 means infinite.
	AllowNonNetworkErrors bool             // Whether to allow non-network errors to be retried.
	RetryDelay            time.Duration    // Initial delay between retries. It cannot be less than a zero.
	RetryDelayMultiplier  float64          // Multiplier of retry delay for each retry. It cannot be less than one.
	RetryDelayMax         time.Duration    // Maximum delay between retries. It cannot be less than RetryDelay.
	ConnectionChecker     concheck.Checker // Network connection checker in case of network error. If nil, then connection is not checked.
	Context               context.Context  // Context for the retrier.
}

type RetrierOption

type RetrierOption func(*RequestRetrierOptions)

func WithConnectionChecker

func WithConnectionChecker(checker concheck.Checker) RetrierOption

func WithConstRetryDelay

func WithConstRetryDelay(delay time.Duration) RetrierOption

func WithMaxRetries

func WithMaxRetries(maxRetries int) RetrierOption

func WithRetrierContext

func WithRetrierContext(ctx context.Context) RetrierOption

func WithRetryDelay

func WithRetryDelay(delay time.Duration, multiplier float64, max time.Duration) RetrierOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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