Documentation
¶
Index ¶
- Variables
- type HTTPHealthCheckConfig
- type HTTPHealthCheckPolicy
- func (hcp *HTTPHealthCheckPolicy) Body() []byte
- func (hcp *HTTPHealthCheckPolicy) Headers() map[string]string
- func (hcp *HTTPHealthCheckPolicy) Method() string
- func (hcp *HTTPHealthCheckPolicy) Path() string
- func (hcp *HTTPHealthCheckPolicy) SetBody(value []byte) *HTTPHealthCheckPolicy
- func (hcp *HTTPHealthCheckPolicy) SetHeaders(value map[string]string) *HTTPHealthCheckPolicy
- func (hcp *HTTPHealthCheckPolicy) SetMethod(value string) *HTTPHealthCheckPolicy
- func (hcp *HTTPHealthCheckPolicy) SetPath(value string) *HTTPHealthCheckPolicy
- func (hcp *HTTPHealthCheckPolicy) SetTimeout(value time.Duration) *HTTPHealthCheckPolicy
- func (hcp *HTTPHealthCheckPolicy) Timeout() time.Duration
- type HTTPHealthCheckPolicyBuilder
- func (hb *HTTPHealthCheckPolicyBuilder) Build(endpoint *url.URL) (*HTTPHealthCheckPolicy, error)
- func (hb *HTTPHealthCheckPolicyBuilder) FailureThreshold() uint
- func (hb *HTTPHealthCheckPolicyBuilder) Interval() time.Duration
- func (hb *HTTPHealthCheckPolicyBuilder) SuccessStatus() int
- func (hb *HTTPHealthCheckPolicyBuilder) SuccessThreshold() uint
- func (hb *HTTPHealthCheckPolicyBuilder) WithFailureThreshold(value uint) *HTTPHealthCheckPolicyBuilder
- func (hb *HTTPHealthCheckPolicyBuilder) WithInterval(value time.Duration) *HTTPHealthCheckPolicyBuilder
- func (hb *HTTPHealthCheckPolicyBuilder) WithSuccessStatus(status int) *HTTPHealthCheckPolicyBuilder
- func (hb *HTTPHealthCheckPolicyBuilder) WithSuccessThreshold(value uint) *HTTPHealthCheckPolicyBuilder
- type Host
- func (s *Host) AddCurrentWeight()
- func (s *Host) Authenticator() authscheme.HTTPClientAuthenticator
- func (s *Host) CheckHealth(ctx context.Context)
- func (s *Host) Close()
- func (s *Host) CurrentWeight() int32
- func (s *Host) Do(req *http.Request) (*http.Response, error)
- func (s *Host) GetLastHTTPErrorStatus() (int32, bool)
- func (s *Host) HTTPClient() *http.Client
- func (s *Host) Headers() map[string]string
- func (s *Host) HealthCheckPolicy() *HTTPHealthCheckPolicy
- func (s *Host) Name() string
- func (s *Host) NewRequest(ctx context.Context, method string, requestPath string, body io.Reader) (*http.Request, error)
- func (s *Host) ResetCurrentWeight(totalWeight int32)
- func (s *Host) SetAuthenticator(authenticator authscheme.HTTPClientAuthenticator) *Host
- func (s *Host) SetHTTPClient(client *http.Client) *Host
- func (s *Host) SetHeaders(headers map[string]string) *Host
- func (s *Host) SetHealthCheckPolicy(policy *HTTPHealthCheckPolicy) *Host
- func (s *Host) SetName(name string) *Host
- func (s *Host) SetURL(baseURL string) (*url.URL, error)
- func (s *Host) SetWeight(weight int32) *Host
- func (s *Host) State() circuitbreaker.State
- func (s *Host) URL() *url.URL
- func (s *Host) Weight() int32
- type HostOption
- type LoadBalancer
- type LoadBalancerClient
- func (lbc *LoadBalancerClient) Close() error
- func (lbc *LoadBalancerClient) HTTPClient() (gohttpc.HTTPClient, error)
- func (lbc *LoadBalancerClient) R(method string, url string) *gohttpc.RequestWithClient
- func (lbc *LoadBalancerClient) ServerMetrics() map[string]ServerMetrics
- func (lbc *LoadBalancerClient) StartHealthCheck(ctx context.Context)
- type ServerMetrics
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidHealthCheckMethod occurs when the HTTP method of the health check config is invalid. ErrInvalidHealthCheckMethod = errors.New( "invalid health check method. Expects one of GET or POST", ) // ErrInvalidHealthCheckSuccessStatus occurs when the HTTP success status of the health check config is invalid. ErrInvalidHealthCheckSuccessStatus = errors.New( "invalid status of HTTP health check. Expects one of 200, 201 or 204", ) // ErrInvalidHealthCheckFailureThreshold occurs when the failure threshold of the health check config is invalid. ErrInvalidHealthCheckFailureThreshold = errors.New( "failure threshold of HTTP health check must be positive", ) )
var ErrNoActiveHost = errors.New("no active host")
ErrNoActiveHost occurs when all hosts are inactive on the load balancer.
Functions ¶
This section is empty.
Types ¶
type HTTPHealthCheckConfig ¶
type HTTPHealthCheckConfig struct {
// Health check path, e.g, /healthz.
Path string `json:"path" yaml:"path"`
// Health check method. Default to GET
Method string `json:"method,omitempty" yaml:"method,omitempty" jsonschema:"default=GET,enum=GET,enum=POST"`
// Request body is used if the method is POST.
Body any `json:"body,omitempty" yaml:"body,omitempty"`
// Request headers to be sent to health check requests.
Headers map[string]goenvconf.EnvString `json:"headers,omitempty" yaml:"headers,omitempty"`
// Health check interval in seconds. Disabled if the interval is negative or equals 0. Default to 60 seconds
Interval *int `json:"interval,omitempty" yaml:"interval,omitempty" jsonschema:"default=60,min=0"`
// Timeout in seconds. Disabled if the timeout is negative or equals 0. Default to 5 seconds
Timeout *int `json:"timeout,omitempty" yaml:"timeout,omitempty" jsonschema:"default=5,min=0"`
// SuccessStatus is expected successful HTTP status. Default to HTTP 200 OK.
SuccessStatus *int `json:"successStatus,omitempty" yaml:"successStatus,omitempty" jsonschema:"default=200,enum=200,enum=201,enum=204"`
// SuccessThreshold is the minimum consecutive successes for the probe to be considered successful after having failed. Defaults to 1. Minimum value is 1.
SuccessThreshold *int `json:"successThreshold,omitempty" yaml:"successThreshold,omitempty" jsonschema:"default=1,min=1"`
// Failure threshold. After a probe fails threshold times in a row, the HTTP client considers that the overall check has failed. Default to 5. Minimum value is 1
FailureThreshold *int `json:"failureThreshold,omitempty" yaml:"failureThreshold,omitempty" jsonschema:"default=3,min=1"`
}
HTTPHealthCheckConfig holds configurations for health checking the server and recovery.
func (HTTPHealthCheckConfig) ToPolicy ¶
func (hc HTTPHealthCheckConfig) ToPolicy(endpoint *url.URL) (*HTTPHealthCheckPolicy, error)
ToPolicy validates the health check config and create the policy.
func (HTTPHealthCheckConfig) ToPolicyBuilder ¶
func (hc HTTPHealthCheckConfig) ToPolicyBuilder() (*HTTPHealthCheckPolicyBuilder, error)
ToPolicyBuilder validates the health check config and create the policy builder.
type HTTPHealthCheckPolicy ¶
type HTTPHealthCheckPolicy struct {
circuitbreaker.CircuitBreaker[int]
// contains filtered or unexported fields
}
HTTPHealthCheckPolicy represents an HTTP health check policy state.
func (*HTTPHealthCheckPolicy) Body ¶
func (hcp *HTTPHealthCheckPolicy) Body() []byte
Body returns the health check body.
func (*HTTPHealthCheckPolicy) Headers ¶
func (hcp *HTTPHealthCheckPolicy) Headers() map[string]string
Headers returns the health check headers.
func (*HTTPHealthCheckPolicy) Method ¶
func (hcp *HTTPHealthCheckPolicy) Method() string
Method returns the health check method.
func (*HTTPHealthCheckPolicy) Path ¶
func (hcp *HTTPHealthCheckPolicy) Path() string
Path returns the health check path.
func (*HTTPHealthCheckPolicy) SetBody ¶
func (hcp *HTTPHealthCheckPolicy) SetBody(value []byte) *HTTPHealthCheckPolicy
SetBody sets the health check body.
func (*HTTPHealthCheckPolicy) SetHeaders ¶
func (hcp *HTTPHealthCheckPolicy) SetHeaders(value map[string]string) *HTTPHealthCheckPolicy
SetHeaders sets the health check headers.
func (*HTTPHealthCheckPolicy) SetMethod ¶
func (hcp *HTTPHealthCheckPolicy) SetMethod(value string) *HTTPHealthCheckPolicy
SetMethod sets the health check method.
func (*HTTPHealthCheckPolicy) SetPath ¶
func (hcp *HTTPHealthCheckPolicy) SetPath(value string) *HTTPHealthCheckPolicy
SetPath sets the health check path.
func (*HTTPHealthCheckPolicy) SetTimeout ¶
func (hcp *HTTPHealthCheckPolicy) SetTimeout(value time.Duration) *HTTPHealthCheckPolicy
SetTimeout sets the health check timeout duration.
func (*HTTPHealthCheckPolicy) Timeout ¶
func (hcp *HTTPHealthCheckPolicy) Timeout() time.Duration
Timeout returns the health check timeout duration.
type HTTPHealthCheckPolicyBuilder ¶
type HTTPHealthCheckPolicyBuilder struct {
*HTTPHealthCheckPolicy
// contains filtered or unexported fields
}
HTTPHealthCheckPolicyBuilder represents an HTTP health check policy builder.
func NewHTTPHealthCheckPolicyBuilder ¶
func NewHTTPHealthCheckPolicyBuilder() *HTTPHealthCheckPolicyBuilder
NewHTTPHealthCheckPolicyBuilder creates an HTTP health check policy builder.
func (*HTTPHealthCheckPolicyBuilder) Build ¶
func (hb *HTTPHealthCheckPolicyBuilder) Build(endpoint *url.URL) (*HTTPHealthCheckPolicy, error)
Build builds the HTTPHealthCheckPolicy.
func (*HTTPHealthCheckPolicyBuilder) FailureThreshold ¶
func (hb *HTTPHealthCheckPolicyBuilder) FailureThreshold() uint
FailureThreshold gets the failure threshold.
func (*HTTPHealthCheckPolicyBuilder) Interval ¶
func (hb *HTTPHealthCheckPolicyBuilder) Interval() time.Duration
Interval gets the health check interval.
func (*HTTPHealthCheckPolicyBuilder) SuccessStatus ¶
func (hb *HTTPHealthCheckPolicyBuilder) SuccessStatus() int
SuccessStatus gets the success status.
func (*HTTPHealthCheckPolicyBuilder) SuccessThreshold ¶
func (hb *HTTPHealthCheckPolicyBuilder) SuccessThreshold() uint
SuccessThreshold gets the success threshold.
func (*HTTPHealthCheckPolicyBuilder) WithFailureThreshold ¶
func (hb *HTTPHealthCheckPolicyBuilder) WithFailureThreshold( value uint, ) *HTTPHealthCheckPolicyBuilder
WithFailureThreshold sets the failure threshold of the health check.
func (*HTTPHealthCheckPolicyBuilder) WithInterval ¶
func (hb *HTTPHealthCheckPolicyBuilder) WithInterval( value time.Duration, ) *HTTPHealthCheckPolicyBuilder
WithInterval sets the health check interval.
func (*HTTPHealthCheckPolicyBuilder) WithSuccessStatus ¶
func (hb *HTTPHealthCheckPolicyBuilder) WithSuccessStatus( status int, ) *HTTPHealthCheckPolicyBuilder
WithSuccessStatus sets the expected success status of the health check.
func (*HTTPHealthCheckPolicyBuilder) WithSuccessThreshold ¶
func (hb *HTTPHealthCheckPolicyBuilder) WithSuccessThreshold( value uint, ) *HTTPHealthCheckPolicyBuilder
WithSuccessThreshold sets the success threshold of the health check.
type Host ¶
type Host struct {
// contains filtered or unexported fields
}
Host represents the host information and its weight to load balance the requests.
func (*Host) AddCurrentWeight ¶
func (s *Host) AddCurrentWeight()
AddCurrentWeight adds the weight to the current weight.
func (*Host) Authenticator ¶
func (s *Host) Authenticator() authscheme.HTTPClientAuthenticator
Authenticator returns the custom authenticator for this host.
func (*Host) CheckHealth ¶
CheckHealth runs an HTTP request to checking the health of the host.
func (*Host) CurrentWeight ¶
CurrentWeight adds the weight to the current weight.
func (*Host) Do ¶
Do sends an HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth) as configured on the client.
func (*Host) GetLastHTTPErrorStatus ¶
GetLastHTTPErrorStatus returns the last HTTP error status, and the flag to determine if it is the server outage status.
func (*Host) HTTPClient ¶
HTTPClient returns the HTTP client of this host.
func (*Host) HealthCheckPolicy ¶
func (s *Host) HealthCheckPolicy() *HTTPHealthCheckPolicy
HealthCheckPolicy returns the HTTP health check policy of this host.
func (*Host) NewRequest ¶
func (s *Host) NewRequest( ctx context.Context, method string, requestPath string, body io.Reader, ) (*http.Request, error)
NewRequest returns a new http.Request given a method, URL, and optional body.
func (*Host) ResetCurrentWeight ¶
ResetCurrentWeight resets the current weight.
func (*Host) SetAuthenticator ¶
func (s *Host) SetAuthenticator(authenticator authscheme.HTTPClientAuthenticator) *Host
SetAuthenticator sets the authenticator for this host.
func (*Host) SetHTTPClient ¶
SetHTTPClient sets the HTTP client of this host.
func (*Host) SetHeaders ¶
SetHeaders sets headers of this host.
func (*Host) SetHealthCheckPolicy ¶
func (s *Host) SetHealthCheckPolicy(policy *HTTPHealthCheckPolicy) *Host
SetHealthCheckPolicy sets the health check policy to this host.
func (*Host) SetURL ¶
SetURL sets the base URL of this host. NOTE: the name won't be updated if it is not empty.
func (*Host) State ¶
func (s *Host) State() circuitbreaker.State
State returns the circuit breaker state of this host.
type HostOption ¶
type HostOption func(*hostOptions)
HostOption represents a function to modify host options.
func WithHTTPHealthCheckPolicyBuilder ¶
func WithHTTPHealthCheckPolicyBuilder(builder *HTTPHealthCheckPolicyBuilder) HostOption
WithHTTPHealthCheckPolicyBuilder sets the http health check builder for the host.
type LoadBalancer ¶
type LoadBalancer interface {
Hosts() []*Host
Next() (*Host, error)
// StartHealthCheck starts a ticker to run health checking for servers in the background.
StartHealthCheck(ctx context.Context)
Close() error
}
LoadBalancer is the interface that wraps the HTTP client load-balancing algorithm that returns the appropriate host for the request to target.
type LoadBalancerClient ¶
type LoadBalancerClient struct {
// contains filtered or unexported fields
}
LoadBalancerClient represents an HTTP client that accepts a list of hosts and load balance requests to each host.
func NewLoadBalancerClient ¶
func NewLoadBalancerClient( loadBalancer LoadBalancer, options ...gohttpc.ClientOption, ) *LoadBalancerClient
NewLoadBalancerClient creates a new LoadBalancerClient instance.
func NewLoadBalancerClientWithOptions ¶
func NewLoadBalancerClientWithOptions( loadBalancer LoadBalancer, options gohttpc.RequestOptionsGetter, ) *LoadBalancerClient
NewLoadBalancerClientWithOptions creates a new LoadBalancerClient instance with explicit client options.
func (*LoadBalancerClient) Close ¶
func (lbc *LoadBalancerClient) Close() error
Close terminates the client and clean up internal processes.
func (*LoadBalancerClient) HTTPClient ¶
func (lbc *LoadBalancerClient) HTTPClient() (gohttpc.HTTPClient, error)
HTTPClient returns the current or inner HTTP client for load balancing.
func (*LoadBalancerClient) R ¶
func (lbc *LoadBalancerClient) R(method string, url string) *gohttpc.RequestWithClient
R is the shortcut to create a Request given a method, URL with default request options.
func (*LoadBalancerClient) ServerMetrics ¶
func (lbc *LoadBalancerClient) ServerMetrics() map[string]ServerMetrics
ServerMetrics returns summary metrics of server hosts.
func (*LoadBalancerClient) StartHealthCheck ¶
func (lbc *LoadBalancerClient) StartHealthCheck(ctx context.Context)
StartHealthCheck starts a ticker to run health checking for servers in the background.
type ServerMetrics ¶
type ServerMetrics struct {
// Executions returns the number of executions recorded in the current state when the state is ClosedState or
// HalfOpenState. When the state is OpenState, this returns the executions recorded during the previous ClosedState.
//
// For count based thresholding, the max number of executions is limited to the execution threshold. For time based
// thresholds, the number of executions may vary within the thresholding period.
Executions uint `json:"executions"`
// Failures returns the number of failures recorded in the current state when in a ClosedState or HalfOpenState. When
// in OpenState, this returns the failures recorded during the previous ClosedState.
//
// For count based thresholds, the max number of failures is based on the failure threshold. For time based thresholds,
// the number of failures may vary within the failure thresholding period.
Failures uint `json:"failures"`
// FailureRate returns the rate of failed executions in the current state when in a ClosedState or HalfOpenState. When
// in OpenState, this returns the rate recorded during the previous ClosedState.
//
// The rate is based on the configured failure thresholding capacity.
FailureRate float64 `json:"failure_rate"`
// Successes returns the number of successes recorded in the current state when in a ClosedState or HalfOpenState.
// When in OpenState, this returns the successes recorded during the previous ClosedState.
//
// The max number of successes is based on the success threshold.
Successes uint `json:"successes"`
// SuccessRate returns rate of successful executions in the current state when in a ClosedState or HalfOpenState. When
// in OpenState, this returns the successes recorded during the previous ClosedState.
//
// The rate is based on the configured success thresholding capacity.
SuccessRate float64 `json:"success_rate"`
}
ServerMetrics represents the metrics data of a server.