fhlbclient

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: MIT Imports: 6 Imported by: 0

README

FastHTTP Load Balancing Client

Flexible replacement of fasthttp's LBClient.

Our experience: we have ~20 instances of the service on different servers. We use LBClient together with HostClient to balance requests among them. Average RPS is 3-5k requests.

Sometime one of these clients become unavailable, mostly due to deployment process. HostClient instance of this server collects zero pending requests and maximum 300 penalty counter.

In result this condition decides that this server is least loaded and prioritize it. Almost 100% of our traffic drains into the trash.

This package was developed to solve that problem and also provides few handy features, see next section.

Features

  • Health check function replaced to interface with corresponding function.
  • Added Penalty field to handle penalty duration.
  • Added Balancer field where you can specify your own balance algorithm, see existing.
  • Added RequestHooker field where you can specify implementation of PreRequest() and PostRequest() methods.

Documentation

Index

Constants

View Source
const (
	DefaultTimeout = time.Second
	DefaultPenalty = time.Second * 3
)

Variables

View Source
var ErrNoAliveClients = errors.New("no alive clients available")

Functions

This section is empty.

Types

type Balancer

type Balancer interface {
	Evaluate([]PenalizingClient) *PenalizingClient
}

Clients balancer interface.

type DummyBalancer

type DummyBalancer struct{}

func (*DummyBalancer) Evaluate

type DummyRequestHooks

type DummyRequestHooks struct{}

func (*DummyRequestHooks) PostRequest

func (*DummyRequestHooks) PreRequest

type HealthChecker

type HealthChecker interface {
	Check(req *fasthttp.Request, resp *fasthttp.Response, err error) bool
}

Health checker interface.

type LBClient

type LBClient struct {
	// Array of clients to balance.
	Clients []fasthttp.BalancingClient
	// Health check helper.
	HealthCheck HealthChecker
	// Timeout duration to execute request.
	// Will used DefaultTimeout if empty.
	Timeout time.Duration
	// Penalty duration to ban the client.
	Penalty time.Duration
	// Balancer helper.
	Balancer Balancer
	// Request hooks helper.
	RequestHooker RequestHooker
	// contains filtered or unexported fields
}

Load balancing client.

See https://github.com/valyala/fasthttp/blob/master/lbclient.go for details and comparison.

func (*LBClient) Do

func (c *LBClient) Do(req *fasthttp.Request, resp *fasthttp.Response) error

Execute request with internal timeout.

func (*LBClient) DoDeadline

func (c *LBClient) DoDeadline(req *fasthttp.Request, resp *fasthttp.Response, deadline time.Time) error

Execute request with given deadline.

func (*LBClient) DoDeadlineWB

func (c *LBClient) DoDeadlineWB(req *fasthttp.Request, resp *fasthttp.Response, deadline time.Time, b Balancer) error

Execute request with given deadline and balancer.

func (*LBClient) DoTimeout

func (c *LBClient) DoTimeout(req *fasthttp.Request, resp *fasthttp.Response, timeout time.Duration) error

Execute request with given timeout.

func (*LBClient) DoTimeoutWB

func (c *LBClient) DoTimeoutWB(req *fasthttp.Request, resp *fasthttp.Response, timeout time.Duration, b Balancer) error

Execute request with given timeout and balancer.

func (*LBClient) DoWB

func (c *LBClient) DoWB(req *fasthttp.Request, resp *fasthttp.Response, b Balancer) error

Execute request with internal timeout and balancer.

type PenalizingClient

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

A wrapper around BalancingClient with penalty support.

func (*PenalizingClient) DoDeadline

func (c *PenalizingClient) DoDeadline(req *fasthttp.Request, resp *fasthttp.Response, deadline time.Time) error

Execute request with given deadline.

func (*PenalizingClient) Instance

Get inner fasthttp's balancing client instance.

func (*PenalizingClient) RequestStats

func (c *PenalizingClient) RequestStats() (uint64, uint64)

Get two requests metrics: pending requests and total requests counts.

Pending requests value includes penalty counter value.

func (*PenalizingClient) UnderPenalty

func (c *PenalizingClient) UnderPenalty() bool

Check if client is under penalty.

type RequestHooker

type RequestHooker interface {
	PreRequest(req *fasthttp.Request, resp *fasthttp.Response, client *PenalizingClient)
	PostRequest(req *fasthttp.Request, resp *fasthttp.Response, client *PenalizingClient, err error)
}

Request hooks helper interface.

Needs to perform some actions before and after request's execution.

Directories

Path Synopsis
metrics
prometheus Module

Jump to

Keyboard shortcuts

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