Documentation
¶
Index ¶
- func NewResilientClient(cfg *ResilienceConfig) *http.Client
- func NewResilientTransport(baseTransport http.RoundTripper, cfg *ResilienceConfig) http.RoundTripper
- type CircuitBreakerConfig
- type Client
- func (c *Client) Delete(ctx context.Context, path string) (*http.Response, error)
- func (c *Client) DeleteJSON(ctx context.Context, path string, result interface{}) error
- func (c *Client) Do(ctx context.Context, method, path string, body interface{}) (*http.Response, error)
- func (c *Client) DoWithResponse(ctx context.Context, method, path string, body, result interface{}) error
- func (c *Client) Get(ctx context.Context, path string) (*http.Response, error)
- func (c *Client) GetErrorParser() *ErrorParserChain
- func (c *Client) GetHTTPClient() *http.Client
- func (c *Client) GetJSON(ctx context.Context, path string, result interface{}) error
- func (c *Client) Patch(ctx context.Context, path string, body interface{}) (*http.Response, error)
- func (c *Client) PatchJSON(ctx context.Context, path string, body, result interface{}) error
- func (c *Client) Post(ctx context.Context, path string, body interface{}) (*http.Response, error)
- func (c *Client) PostJSON(ctx context.Context, path string, body, result interface{}) error
- func (c *Client) Put(ctx context.Context, path string, body interface{}) (*http.Response, error)
- func (c *Client) PutJSON(ctx context.Context, path string, body, result interface{}) error
- func (c *Client) RemoveHeader(key string)
- func (c *Client) SetBaseURL(baseURL string)
- func (c *Client) SetErrorParser(parser *ErrorParserChain)
- func (c *Client) SetHTTPClient(httpClient *http.Client)
- func (c *Client) SetHeader(key, value string)
- func (c *Client) SetHeaders(headers map[string]string)
- type ClientOption
- func WithBaseURL(baseURL string) ClientOption
- func WithErrorParser(parser ErrorResponseParser) ClientOption
- func WithHTTPClient(httpClient *http.Client) ClientOption
- func WithHeader(key, value string) ClientOption
- func WithHeaders(headers map[string]string) ClientOption
- func WithResilienceConfig(cfg *ResilienceConfig) ClientOption
- func WithTimeout(timeout time.Duration) ClientOption
- type Error
- type ErrorParserChain
- type ErrorResponseParser
- type ErrorType
- type FallbackConfig
- type GenericErrorParser
- type ParsedErrorResponse
- type RFC7807ErrorParser
- type ResilienceBuilder
- func (rb *ResilienceBuilder) Build() *ResilienceConfig
- func (rb *ResilienceBuilder) WithCircuitBreaker(cfg *CircuitBreakerConfig) *ResilienceBuilder
- func (rb *ResilienceBuilder) WithFallback(cfg *FallbackConfig) *ResilienceBuilder
- func (rb *ResilienceBuilder) WithRetryPolicy(cfg *RetryPolicyConfig) *ResilienceBuilder
- func (rb *ResilienceBuilder) WithTimeout(cfg *TimeoutConfig) *ResilienceBuilder
- type ResilienceConfig
- type RetryPolicyConfig
- type StripeErrorParser
- type SumsubErrorParser
- type ThresholdRatio
- type TimeoutConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewResilientClient ¶
func NewResilientClient(cfg *ResilienceConfig) *http.Client
NewResilientClient creates an HTTP client with resilience policies This is the recommended way to create a resilient HTTP client
func NewResilientTransport ¶
func NewResilientTransport(baseTransport http.RoundTripper, cfg *ResilienceConfig) http.RoundTripper
NewResilientTransport wraps an existing transport with resilience policies Use this if you need to customize the base transport
Types ¶
type CircuitBreakerConfig ¶
type CircuitBreakerConfig struct {
Enabled bool
FailureThreshold uint // Count-based: Opens after N consecutive failures
FailureThresholdRatio *ThresholdRatio // Ratio-based: Opens when X of Y executions fail
SuccessThreshold uint // Count-based: Closes after N consecutive successes in half-open
SuccessThresholdRatio *ThresholdRatio // Ratio-based: Closes when X of Y executions succeed in half-open
Delay time.Duration // Time to wait in open state before half-opening
OnStateChanged func(event circuitbreaker.StateChangedEvent)
OnOpen func(event circuitbreaker.StateChangedEvent)
OnHalfOpen func(event circuitbreaker.StateChangedEvent)
OnClose func(event circuitbreaker.StateChangedEvent)
}
CircuitBreakerConfig holds circuit breaker configuration
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(opts ...ClientOption) *Client
func NewDefaultClient ¶
func (*Client) DeleteJSON ¶
func (*Client) DoWithResponse ¶
func (*Client) GetErrorParser ¶ added in v1.1.0
func (c *Client) GetErrorParser() *ErrorParserChain
func (*Client) GetHTTPClient ¶
func (*Client) RemoveHeader ¶
func (*Client) SetBaseURL ¶
func (*Client) SetErrorParser ¶ added in v1.1.0
func (c *Client) SetErrorParser(parser *ErrorParserChain)
func (*Client) SetHTTPClient ¶
func (*Client) SetHeaders ¶
type ClientOption ¶
type ClientOption func(*Client)
func WithBaseURL ¶
func WithBaseURL(baseURL string) ClientOption
func WithErrorParser ¶ added in v1.1.0
func WithErrorParser(parser ErrorResponseParser) ClientOption
func WithHTTPClient ¶
func WithHTTPClient(httpClient *http.Client) ClientOption
func WithHeader ¶
func WithHeader(key, value string) ClientOption
func WithHeaders ¶
func WithHeaders(headers map[string]string) ClientOption
func WithResilienceConfig ¶
func WithResilienceConfig(cfg *ResilienceConfig) ClientOption
func WithTimeout ¶
func WithTimeout(timeout time.Duration) ClientOption
type Error ¶
type Error struct {
Type ErrorType `json:"type"`
Message string `json:"message"`
StatusCode int `json:"status_code,omitempty"`
URL string `json:"url"`
Method string `json:"method"`
RequestID string `json:"request_id,omitempty"`
CorrelationID string `json:"correlation_id,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
Details map[string]interface{} `json:"details,omitempty"`
Err error `json:"-"`
Timestamp time.Time `json:"timestamp"`
ResponseBody []byte `json:"-"`
}
Error represents an HTTP client error with detailed context
func (*Error) GetDetail ¶ added in v1.1.0
GetDetail retrieves a specific detail from the error response
type ErrorParserChain ¶ added in v1.1.0
type ErrorParserChain struct {
// contains filtered or unexported fields
}
ErrorParserChain tries multiple parsers in order
func NewErrorParserChain ¶ added in v1.1.0
func NewErrorParserChain() *ErrorParserChain
func (*ErrorParserChain) AddParser ¶ added in v1.1.0
func (c *ErrorParserChain) AddParser(parser ErrorResponseParser)
func (*ErrorParserChain) Parse ¶ added in v1.1.0
func (c *ErrorParserChain) Parse(body []byte) (*ParsedErrorResponse, error)
type ErrorResponseParser ¶ added in v1.1.0
type ErrorResponseParser interface {
Parse(body []byte) (*ParsedErrorResponse, error)
CanParse(body []byte) bool
}
ErrorResponseParser defines an interface for parsing custom error responses
type FallbackConfig ¶
type FallbackConfig struct {
Enabled bool
FallbackFunc func(exec failsafe.Execution[*http.Response]) (*http.Response, error)
OnFallbackExecuted func(event failsafe.ExecutionDoneEvent[*http.Response])
}
FallbackConfig holds fallback configuration
type GenericErrorParser ¶ added in v1.1.0
type GenericErrorParser struct{}
GenericErrorParser parses common error response formats
func (*GenericErrorParser) CanParse ¶ added in v1.1.0
func (p *GenericErrorParser) CanParse(body []byte) bool
func (*GenericErrorParser) Parse ¶ added in v1.1.0
func (p *GenericErrorParser) Parse(body []byte) (*ParsedErrorResponse, error)
type ParsedErrorResponse ¶ added in v1.1.0
type ParsedErrorResponse struct {
Message string
ErrorCode string
CorrelationID string
Details map[string]interface{}
}
ParsedErrorResponse represents a parsed error response from an external API
type RFC7807ErrorParser ¶ added in v1.1.0
type RFC7807ErrorParser struct {
GenericErrorParser
}
RFC7807ErrorParser parses RFC 7807 Problem Details for HTTP APIs
func (*RFC7807ErrorParser) CanParse ¶ added in v1.1.0
func (p *RFC7807ErrorParser) CanParse(body []byte) bool
func (*RFC7807ErrorParser) Parse ¶ added in v1.1.0
func (p *RFC7807ErrorParser) Parse(body []byte) (*ParsedErrorResponse, error)
type ResilienceBuilder ¶
type ResilienceBuilder struct {
// contains filtered or unexported fields
}
ResilienceBuilder builds resilience configurations using fluent API
func NewResilienceBuilder ¶
func NewResilienceBuilder() *ResilienceBuilder
NewResilienceBuilder creates a new resilience configuration builder
func (*ResilienceBuilder) Build ¶
func (rb *ResilienceBuilder) Build() *ResilienceConfig
Build constructs the final resilience configuration
func (*ResilienceBuilder) WithCircuitBreaker ¶
func (rb *ResilienceBuilder) WithCircuitBreaker(cfg *CircuitBreakerConfig) *ResilienceBuilder
WithCircuitBreaker configures circuit breaker settings
func (*ResilienceBuilder) WithFallback ¶
func (rb *ResilienceBuilder) WithFallback(cfg *FallbackConfig) *ResilienceBuilder
WithFallback configures fallback settings
func (*ResilienceBuilder) WithRetryPolicy ¶
func (rb *ResilienceBuilder) WithRetryPolicy(cfg *RetryPolicyConfig) *ResilienceBuilder
WithRetryPolicy configures retry policy settings
func (*ResilienceBuilder) WithTimeout ¶
func (rb *ResilienceBuilder) WithTimeout(cfg *TimeoutConfig) *ResilienceBuilder
WithTimeout configures timeout settings
type ResilienceConfig ¶
type ResilienceConfig struct {
CircuitBreaker *CircuitBreakerConfig
RetryPolicy *RetryPolicyConfig
Timeout *TimeoutConfig
Fallback *FallbackConfig
}
ResilienceConfig holds all resilience configurations
func DefaultResilienceConfig ¶
func DefaultResilienceConfig() *ResilienceConfig
DefaultResilienceConfig returns a production-ready default configuration
type RetryPolicyConfig ¶
type RetryPolicyConfig struct {
Enabled bool
MaxAttempts int // Maximum number of execution attempts (initial + retries)
MaxRetries int // Maximum number of retries (MaxAttempts - 1)
MaxDuration time.Duration // Maximum total duration for all attempts
Delay time.Duration // Fixed delay between retries
BackoffInitial time.Duration // Initial delay for exponential backoff
BackoffMax time.Duration // Maximum delay for exponential backoff
RandomDelayMin time.Duration // Minimum random delay
RandomDelayMax time.Duration // Maximum random delay
JitterFactor float64 // Jitter factor (0-1) to add randomness to delays
Jitter time.Duration // Time-based jitter to add to delays
RetryableStatus []int // HTTP status codes that should trigger a retry
AbortOnStatus []int // HTTP status codes that should abort retries immediately
OnRetry func(event failsafe.ExecutionEvent[*http.Response])
OnRetriesExceeded func(event failsafe.ExecutionEvent[*http.Response])
OnAbort func(event failsafe.ExecutionEvent[*http.Response])
}
RetryPolicyConfig holds retry policy configuration
type StripeErrorParser ¶ added in v1.1.0
type StripeErrorParser struct {
GenericErrorParser
}
StripeErrorParser parses Stripe-specific error responses
func (*StripeErrorParser) CanParse ¶ added in v1.1.0
func (p *StripeErrorParser) CanParse(body []byte) bool
func (*StripeErrorParser) Parse ¶ added in v1.1.0
func (p *StripeErrorParser) Parse(body []byte) (*ParsedErrorResponse, error)
type SumsubErrorParser ¶ added in v1.1.0
type SumsubErrorParser struct {
GenericErrorParser
}
SumsubErrorParser parses Sumsub-specific error responses
func (*SumsubErrorParser) CanParse ¶ added in v1.1.0
func (p *SumsubErrorParser) CanParse(body []byte) bool
func (*SumsubErrorParser) Parse ¶ added in v1.1.0
func (p *SumsubErrorParser) Parse(body []byte) (*ParsedErrorResponse, error)
type ThresholdRatio ¶
ThresholdRatio represents a ratio-based threshold (e.g., 3 failures out of 5 total)
type TimeoutConfig ¶
type TimeoutConfig struct {
Enabled bool
Duration time.Duration // Maximum time allowed for execution
OnTimeoutExceeded func(event failsafe.ExecutionDoneEvent[*http.Response])
}
TimeoutConfig holds timeout configuration