timeouthttp

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2022 License: MIT Imports: 8 Imported by: 23

README

go-timeout-httpclient

Yet another HTTP Client with configurable timeouts and TLS options.

Usage

Create a HTTP client with specific timeouts once and re-use it wherever you need it.


import "github.com/pteich/go-timeout-httpclient"

// Create an HTTP client with a timeout of 5s
httpClient := timeouthttp.New(timeouthttp.WithTimeout(5))

Built-in Circuit Breaker

Using the option timeouthttp.WithCircuitBreaker() automatically enables a circuit breaker for the requested host. This is implemented using the http.RoundTripper interface.

By now there are no further configuration values possible. This means the circuit is considered open if there are more than 3 requests and a failure ratio > 0.6.

Caveat: A new circuit breaker is created for every new host. There will be a memory issue if you request a lot of different hosts as there is no cleaning build in for the time being.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New added in v0.2.0

func New(opts ...Option) *http.Client

New returns a new clean HTTP.Client with variable options set

func NewClient

func NewClient(config Config) *http.Client

NewClient returns a new clean HTTP.Client with timeouts (default 1s for connection and request), disabled idle connections and disabled keep-alives

func NewPooledClient

func NewPooledClient(config Config) *http.Client

NewPooledClient returns a new clean HTTP.Client with timeouts (default 1s for connection and request) and shared transport across hosts with keepalive on, you can set the number of idle connections per host with Config.MaxIdleConnsPerHost (default 1)

Types

type Client added in v0.3.0

type Client struct {
	http.Client
}

type Config

type Config struct {
	ConnectTimeout            int
	RequestTimeout            int
	KeepAliveTimeout          int
	MaxIdleConnectionsPerHost int
	KeepAlive                 bool
	// contains filtered or unexported fields
}

Config defines timeouts and max idle connections per host if pooled transport is used all timeouts are seconds

type Option added in v0.2.0

type Option func(c *Config)

func WithCircuitBreaker added in v0.3.0

func WithCircuitBreaker() Option

func WithConnectTimeout added in v0.2.0

func WithConnectTimeout(timeout int) Option

func WithKeepAliveTimeout added in v0.2.0

func WithKeepAliveTimeout(timeout int) Option

func WithMaxIdleConnections added in v0.2.0

func WithMaxIdleConnections(count int) Option

func WithRequestTimeout added in v0.2.0

func WithRequestTimeout(timeout int) Option

func WithTimeout added in v0.2.0

func WithTimeout(timeout int) Option

WithTimeout sets all timeouts to the provided value in seconds

func WithTlsConfig added in v0.2.1

func WithTlsConfig(tlsConfig *tls.Config) Option

type Transport added in v0.3.0

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

func DefaultPooledTransport

func DefaultPooledTransport(config Config) *Transport

func DefaultTransport

func DefaultTransport(config Config) *Transport

func (*Transport) RoundTrip added in v0.3.0

func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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