hostpool

package
v0.0.0-...-5ed304f Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoHealthyHosts = fmt.Errorf("no healthy hosts")
)

Functions

This section is empty.

Types

type GRPCClient

type GRPCClient interface {
	URL() string
	Send(interface{}) (interface{}, error)
	Reconnect() error
}

type GRPCClientFactory

type GRPCClientFactory func(string, time.Duration) (GRPCClient, error)

type GRPCHealthCheck

type GRPCHealthCheck struct {
	Interval time.Duration
	Timeout  time.Duration
	Request  interface{}
}

GRPCHealthCheck specifies the definition of connection health for all connections in the pool. Interval is the frequency that the health check runs, Timeout is the connection timeout.

Either the HTTP or RPC fields are required, if both are defined then HTTP will take precedence.

type GRPCPool

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

HTTPPool represents a pool of HTTP hosts with methods to make standard HTTP calls.

func NewGRPCPool

func NewGRPCPool(
	ctx context.Context,
	factory GRPCClientFactory,
	logger *log.Logger,
	healthCheck *GRPCHealthCheck,
	tunnel *sshtunnel.SSHTunnel,
) *GRPCPool

NewGRPCPool creates a GRPC pool that manages connection health and optimizes for latency (while maintaining a degree of "stickiness" to avoid excessive reordering).

The GRPCHealthCheck is not required, but without it the pool has little purpose. The health check interval defaults to one minute, the timeout defaults to

func (*GRPCPool) AddHost

func (p *GRPCPool) AddHost(url string, port int) error

Adds a host to the pool. If the host already exists, nothing happens.

func (*GRPCPool) DisableHost

func (p *GRPCPool) DisableHost(id string)

Disables a host from being used for requests, though the host is not deleted (and can be enabled again).

func (*GRPCPool) EnableHost

func (p *GRPCPool) EnableHost(id string)

Enables a host, returning it to the active pool to be used in requests.

func (*GRPCPool) Exec

func (p *GRPCPool) Exec(req interface{}) (interface{}, error)

Executes a GRPC call. All healthy hosts will be attempted, if there are no healthy hosts to begin with, ErrNoHealthyHosts is returned. If there are healthy hosts, though all are actively unhealthy, the last error is returned.

func (*GRPCPool) ExecSticky

func (p *GRPCPool) ExecSticky(hostID string, req interface{}) (interface{}, string, error)

func (*GRPCPool) ExecSynced

func (p *GRPCPool) ExecSynced(req interface{}) (interface{}, string, error)

func (*GRPCPool) GetAllHosts

func (p *GRPCPool) GetAllHosts() []string

func (*GRPCPool) HandleInfoRequest

func (p *GRPCPool) HandleInfoRequest(w http.ResponseWriter, r *http.Request)

func (*GRPCPool) SetHostSyncStatus

func (p *GRPCPool) SetHostSyncStatus(id string, synced bool)

Sets the sync status of a given host.

type HTTPError

type HTTPError struct {
	StatusCode int
	Status     string
	Body       []byte
	Data       []byte
}

HTTPError is a convenient custom error type for HTTP errors.

func (HTTPError) Error

func (err HTTPError) Error() string

String representation of an HTTPError.

type HTTPHealthCheck

type HTTPHealthCheck struct {
	Interval   time.Duration
	Timeout    time.Duration
	HTTPMethod string
	HTTPPath   string
	HTTPBody   interface{}
	RPCRequest *rpc.Request
}

HTTPHealthCheck specifies the definition of connection health for all connections in the pool. Interval is the frequency that the health check runs, Timeout is the connection timeout.

Either the HTTP or RPC fields are required, if both are defined then HTTP will take precedence.

type HTTPHostOptions

type HTTPHostOptions struct {
	Username string
	Password string
	Headers  map[string]string
}

type HTTPPool

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

HTTPPool represents a pool of HTTP hosts with methods to make standard HTTP calls.

func NewHTTPPool

func NewHTTPPool(
	ctx context.Context,
	logger *log.Logger,
	healthCheck *HTTPHealthCheck,
	tunnel *sshtunnel.SSHTunnel,
) *HTTPPool

NewHTTPPool creates an HTTP pool that manages connection health and optimizes for latency (while maintaining a degree of "stickiness" to avoid excessive reordering).

The HTTPHealthCheck is not required, but without it the pool has little purpose. The health check interval defaults to one minute, the timeout defaults to

func (*HTTPPool) AddHost

func (p *HTTPPool) AddHost(url string, port int, opt *HTTPHostOptions) error

Adds a host to the pool. If the host already exists, nothing happens.

func (*HTTPPool) DisableHost

func (p *HTTPPool) DisableHost(id string)

Disables a host from being used for requests, though the host is not deleted (and can be enabled again).

func (*HTTPPool) EnableHost

func (p *HTTPPool) EnableHost(id string)

Enables a host, returning it to the active pool to be used in requests.

func (*HTTPPool) ExecHTTP

func (p *HTTPPool) ExecHTTP(
	method, path string,
	body, target interface{},
) error

Executes a HTTP call. All healthy hosts will be attempted, if there are no healthy hosts to begin with, ErrNoHealthyHosts is returned. If there are healthy hosts, though all are actively unhealthy, the last error is returned.

Note that user error is a possibility for hosts being marked as unhealthy if the json decoder fails. Target should be a pointer to the response object.

func (*HTTPPool) ExecHTTPSticky

func (p *HTTPPool) ExecHTTPSticky(
	hostID, method, path string,
	body, target interface{},
) (string, error)

Executes a HTTP call. All healthy hosts will be attempted, if there are no healthy hosts to begin with, ErrNoHealthyHosts is returned. If there are healthy hosts, though all are actively unhealthy, the last error is returned.

Note that user error is a possibility for hosts being marked as unhealthy if the json decoder fails. Target should be a pointer to the response object.

func (*HTTPPool) ExecHTTPSynced

func (p *HTTPPool) ExecHTTPSynced(
	method, path string,
	body, target interface{},
) (string, error)

func (*HTTPPool) ExecRPC

func (p *HTTPPool) ExecRPC(req *rpc.Request) (*rpc.Response, error)

func (*HTTPPool) ExecRPCBulk

func (p *HTTPPool) ExecRPCBulk(reqs []*rpc.Request) ([]*rpc.Response, error)

Executes a bulk RPC call to all healthy hosts.

func (*HTTPPool) ExecRPCFromArgs

func (p *HTTPPool) ExecRPCFromArgs(method string, params ...interface{}) (*rpc.Response, error)

Executes an RPC call and generates the *rpc.Request internally, requiring only the method and the parameters.

func (*HTTPPool) ExecRPCFromArgsSynced

func (p *HTTPPool) ExecRPCFromArgsSynced(method string, params ...interface{}) (*rpc.Response, error)

Executes an RPC call the same way as ExecRPCFromArgs, except it will only attempt the request on synced hosts.

func (*HTTPPool) ExecRPCSynced

func (p *HTTPPool) ExecRPCSynced(req *rpc.Request) (*rpc.Response, error)

func (*HTTPPool) GetAllHosts

func (p *HTTPPool) GetAllHosts() []string

func (*HTTPPool) HandleInfoRequest

func (p *HTTPPool) HandleInfoRequest(w http.ResponseWriter, r *http.Request)

func (*HTTPPool) SetHostSyncStatus

func (p *HTTPPool) SetHostSyncStatus(id string, synced bool)

Sets the sync status of a given host.

type HealthCheckFunc

type HealthCheckFunc func() int

type TCPHealthCheck

type TCPHealthCheck struct {
	Interval   time.Duration
	Timeout    time.Duration
	RPCRequest *rpc.Request
}

type TCPPool

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

func NewTCPPool

func NewTCPPool(
	ctx context.Context,
	logger *log.Logger,
	healthCheck *TCPHealthCheck,
	tunnel *sshtunnel.SSHTunnel,
) *TCPPool

func (*TCPPool) AddHost

func (p *TCPPool) AddHost(url string, port int) error

Adds a host to the pool. If the host already exists, nothing happens.

func (*TCPPool) DisableHost

func (p *TCPPool) DisableHost(id string)

Disables a host from being used for requests, though the host is not deleted (and can be enabled again).

func (*TCPPool) EnableHost

func (p *TCPPool) EnableHost(id string)

Enables a host, returning it to the active pool to be used in requests.

func (*TCPPool) Exec

func (p *TCPPool) Exec(req *rpc.Request) (*rpc.Response, error)

func (*TCPPool) ExecSynced

func (p *TCPPool) ExecSynced(req *rpc.Request) (*rpc.Response, error)

func (*TCPPool) HandleInfoRequest

func (p *TCPPool) HandleInfoRequest(w http.ResponseWriter, r *http.Request)

func (*TCPPool) SetHostSyncStatus

func (p *TCPPool) SetHostSyncStatus(id string, synced bool)

Sets the sync status of a given host.

func (*TCPPool) Subscribe

func (p *TCPPool) Subscribe(method string) chan *rpc.Request

Jump to

Keyboard shortcuts

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