client

package module
v0.0.0-...-45431f7 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2025 License: GPL-3.0 Imports: 13 Imported by: 0

README

github.com/treussart/articles/http/client

Link to published article

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client(options ...CustomOption) *http.Client

Client creates a http.Client with configurable options for timeout, concurrency, retries, keep-alive, and circuit breaker.

Example
httpClient := Client()
response, err := httpClient.Get("http://detectportal.firefox.com")
if err != nil {
	fmt.Println("Error:", err.Error())
	return
}
if response.StatusCode != http.StatusOK {
	fmt.Println("Error:", "status %s", response.Status)
	return
}
content, err := io.ReadAll(response.Body)
if err != nil {
	fmt.Println("Error:", err.Error())
	return
}
_ = response.Body.Close()
fmt.Println(string(content))
Output:

success

func OperationalEndpointFilter

func OperationalEndpointFilter(r *http.Request) bool

OperationalEndpointFilter filters out requests to operational endpoints like "health", and "ready".

Types

type CustomOption

type CustomOption func(*customConfig)

func WithCBConsecutiveFailures

func WithCBConsecutiveFailures(failures uint32) CustomOption

WithCBConsecutiveFailures set circuit breaker consecutive failures/errors.

func WithCBHTTPSatusCodeMax

func WithCBHTTPSatusCodeMax(d int) CustomOption

WithCBHTTPSatusCodeMax is the HTTP status code from which the CircuitBreaker counts errors.

func WithCBMaxRequests

func WithCBMaxRequests(d uint32) CustomOption

WithCBMaxRequests is the maximum number of requests allowed to pass through when the CircuitBreaker is half-open. If MaxRequests is 0, the CircuitBreaker allows only 1 request.

func WithCBTimeout

func WithCBTimeout(d time.Duration) CustomOption

WithCBTimeout set circuit breaker timeout, is the period of the open state, after which the state of the CircuitBreaker becomes half-open. If WithTimeout is less than or equal to 0, the timeout value of the CircuitBreaker is set to 60 seconds.

func WithCircuitBreakerStats

func WithCircuitBreakerStats(stats *circuitbreaker.Stats, moduleName string) CustomOption

WithCircuitBreakerStats set stats and module name for metrics OTEL.

func WithConcurrency

func WithConcurrency(c int) CustomOption

WithConcurrency set max conns and idles per host.

func WithDialer

func WithDialer(dialer func(ctx context.Context, network string, addr string) (net.Conn, error)) CustomOption

WithDialer set dialer.

func WithDisableKeepAlive

func WithDisableKeepAlive(d bool) CustomOption

WithDisableKeepAlive set true to disable keep-alive.

func WithEnableCircuitBreaker

func WithEnableCircuitBreaker(enable bool) CustomOption

WithEnableCircuitBreaker for activate circuit breaker.

func WithInsecureSkipVerify

func WithInsecureSkipVerify(d bool) CustomOption

WithInsecureSkipVerify controls whether a client verifies the server's certificate chain and host name.

func WithKeepAliveTimeout

func WithKeepAliveTimeout(d time.Duration) CustomOption

WithKeepAliveTimeout set time duration for keep alive timeout.

func WithProxyHost

func WithProxyHost(d string) CustomOption

func WithRetryMax

func WithRetryMax(c int) CustomOption

WithRetryMax set max retry.

func WithRetryWaitMax

func WithRetryWaitMax(d time.Duration) CustomOption

WithRetryWaitMax set time duration for maximum duration for retry.

func WithRetryWaitMin

func WithRetryWaitMin(d time.Duration) CustomOption

WithRetryWaitMin set time duration for minimum duration for retry.

func WithRetryableStats

func WithRetryableStats(stats *retryable.Stats, moduleName string) CustomOption

WithRetryableStats set stats and module name for metrics OTEL.

func WithTimeout

func WithTimeout(d time.Duration) CustomOption

WithTimeout set time duration for timeout.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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