httpclient

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCircuitOpen is returned when the circuit breaker is open
	ErrCircuitOpen = errors.New("circuit breaker is open")
)

Functions

This section is empty.

Types

type CircuitBreaker

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

CircuitBreaker implements the circuit breaker pattern

func NewCircuitBreaker

func NewCircuitBreaker(maxFailures int, timeout, resetTimeout time.Duration) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker maxFailures: number of consecutive failures before opening the circuit timeout: how long to wait before attempting recovery (half-open state) resetTimeout: how long to keep the circuit open before moving to half-open

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(fn func() (*http.Response, error)) (*http.Response, error)

Execute runs the given function with circuit breaker protection

func (*CircuitBreaker) GetFailures

func (cb *CircuitBreaker) GetFailures() int

GetFailures returns the current number of failures

func (*CircuitBreaker) GetState

func (cb *CircuitBreaker) GetState() CircuitState

GetState returns the current state of the circuit breaker

func (*CircuitBreaker) Reset

func (cb *CircuitBreaker) Reset()

Reset manually resets the circuit breaker to closed state

type CircuitState

type CircuitState int

CircuitState represents the state of the circuit breaker

const (
	// StateClosed means the circuit is closed and requests are allowed
	StateClosed CircuitState = iota
	// StateOpen means the circuit is open and requests are blocked
	StateOpen
	// StateHalfOpen means the circuit is testing if the service has recovered
	StateHalfOpen
)

type Client

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

Client is a standardized HTTP client with retry and circuit breaker support

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a new HTTP client with optional configuration

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, path string) error

Delete performs a DELETE request

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do performs a request with retry logic

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, result interface{}) error

Get performs a GET request

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, body, result interface{}) error

Post performs a POST request

func (*Client) Put

func (c *Client) Put(ctx context.Context, path string, body, result interface{}) error

Put performs a PUT request

type Option

type Option func(*Client)

Option is a function that configures the Client

func WithBaseURL

func WithBaseURL(url string) Option

WithBaseURL sets the base URL for all requests

func WithCircuitBreaker

func WithCircuitBreaker() Option

WithCircuitBreaker enables circuit breaker with default settings

func WithCircuitBreakerConfig

func WithCircuitBreakerConfig(maxFailures int, timeout, resetTimeout time.Duration) Option

WithCircuitBreakerConfig enables circuit breaker with custom settings

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient allows using a custom HTTP client

func WithHeader

func WithHeader(key, value string) Option

WithHeader adds a default header to all requests

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders adds multiple default headers to all requests

func WithRetry

func WithRetry(maxRetries int, delay time.Duration) Option

WithRetry configures retry behavior

func WithRetryableStatuses

func WithRetryableStatuses(statuses []int) Option

WithRetryableStatuses sets which HTTP status codes should trigger a retry

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the HTTP client timeout

func WithTransport

func WithTransport(transport http.RoundTripper) Option

WithTransport sets a custom HTTP transport

type RetryConfig

type RetryConfig struct {
	MaxRetries        int
	RetryDelay        time.Duration
	RetryableStatuses []int
}

RetryConfig configures retry behavior

Jump to

Keyboard shortcuts

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