Documentation ¶
Overview ¶
Package steelix wraps http client and makes it more resilient.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backoff ¶
type Backoff interface { // NextInterval returns the interval for the subsequent requests. NextInterval() time.Duration }
Backoff is a contract for implementing backoff strategy.
type BreakerConfig ¶
type BreakerConfig struct { // Name is the name of circuit breaker. Name string // MinRequests is the minimum requests needed for breaker to start applying // the logic whether it needs to change state. // // If we set MinRequests = 10, the breaker will apply the logic // if there is at least 10 requests. Otherwise, the logic doesn't apply. MinRequests uint32 // MinConsecutiveFailures is the minimum number of failed requests that will // make the breaker changes its states from closed to open or half-open. // // This configuration is used together with MinRequests, which means // if we set MinConsecutiveFailures=5 and MinRequests=10, then there are // 7 failed requests, the breaker will not change its state. // // The breaker will change from closed to open // if either MinConsecutiveFailures or FailurePercentage condition are met. MinConsecutiveFailures uint32 // FailurePercentage is a percentage which will change the breaker state // from closed to open if the percentage of failure requests is equal or higher // than the given value. // // This configuration will run together with MinRequests and alongside // the MinConsecutiveFailures. // // The breaker will change from closed to open // if either MinConsecutiveFailures or FailurePercentage condition are met. FailurePercentage float64 }
BreakerConfig holds configuration for implementing circuit breaker strategy.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps native golang http.Client but imbued with retry and circuit breaker strategy if supplied.
func NewClient ¶
func NewClient(hc *http.Client, rc *RetryConfig, bc *BreakerConfig) *Client
NewClient creates an instance of steelix.Client.
If RetryConfig is unset, the default retry config will be applied. Leave the RetryConfig unset is the same thing as not using retry strategy.
Default retry config is:
- Backoff: NoBackoff
- MaxRetry: 0
If BreakerConfig is unset, the circuit breaker will not be applied.
func (*Client) Do ¶
Do does almost the same things http.Client.Do does. The differences are resiliency strategies. While the native http.Client.Do only sends a request and returns a response, this method wraps it with resiliency strategies, such as retry and circuit breaker.
For example, when RetryConfig is set, the failed request will be repeated until max retry is exceeded. Before sending a request, a header X-Steelix-Retry will be set to the request. Its value is the current retry count.
When BreakerConfig is set, the request will be launched inside circuit breaker.
type RetryConfig ¶
type RetryConfig struct { // Backoff is backoff strategy. Backoff Backoff // MaxRetry sets how many times a request should be tried if error happens. // To make it clear, this is how the request life cycle. // Say we set MaxRetry to 1. // First, a request will be launched. If an error occurred, then the request will be tried once again. // In other words, by setup MaxRetry to 1, at most there will be two trials. MaxRetry uint32 }
RetryConfig holds configuration for implementing retry strategy.