proxy

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GRPCProxy

type GRPCProxy struct {
	Proxy
}

GRPCProxy is a wrapper around Proxy that provides gRPC-specific behavior. It embeds the Proxy struct to reuse its core logic and configuration.

func NewGRPCProxy

func NewGRPCProxy(
	ch chan seed.Seed,
	log *slog.Logger,
	lb LoadBalancer,
) *GRPCProxy

NewGRPCProxy creates and returns a new instance of GRPCProxy. It initializes the embedded Proxy with the given seed channel, configuration, logger, and a custom load balancer.

func (*GRPCProxy) ServeHTTP

func (p *GRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles incoming HTTP requests for the GRPCProxy. It satisfies the http.Handler interface, allowing GRPCProxy to be used directly with an HTTP server (e.g., http.ListenAndServe).

func (*GRPCProxy) Start

func (p *GRPCProxy) Start(ctx context.Context)

Start begins the lifecycle of the GRPCProxy. It delegates to the embedded Proxy's Start method, passing in the context and the GRPCProxy's update function.

type LatencyBased

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

LatencyBased is a load balancer that selects servers based on their latency. The latency is represented as a rate, with higher rates indicating lower latency.

func NewLatencyBased

func NewLatencyBased(log *slog.Logger) *LatencyBased

NewLatencyBased returns a new LatencyBased load balancer instance.

func (*LatencyBased) Next

func (rr *LatencyBased) Next() *Server

Next returns the next server based on the weighted random selection, where the weight is determined by the latency Rate of each server. The cumulative approach is used to select a server, effectively creating a "range" for each server in the interval [0, 1]. For example, if the rates are [0.5, 0.3, 0.2], the ranges would be: Server 1: [0, 0.5), Server 2: [0.5, 0.8), Server 3: [0.8, 1). The random number will fall into one of these ranges, effectively selecting a server based on its latency rate. This approach works regardless of the order of the servers, so there's no need to sort them based on latency or rate.

func (*LatencyBased) Update

func (rr *LatencyBased) Update(servers []*Server)

Update updates the list of available servers and their corresponding rates based on their latency. The rate of each server is calculated as the inverse of its latency, and then normalized to ensure that the rates sum to 1.0. This allows the LatencyBased load balancer to select servers based on their relative latency.

type LoadBalancer

type LoadBalancer interface {
	// Update updates the list of available servers.
	Update([]*Server)
	// Next returns the next server to be used based on the load balancing algorithm.
	Next() *Server
}

LoadBalancer is an interface for load balancing algorithms. It provides methods to update the list of available servers and to select the next server to be used.

type Proxy

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

func (*Proxy) Live

func (p *Proxy) Live() bool

func (*Proxy) Ready

func (p *Proxy) Ready() bool

func (*Proxy) Start

func (p *Proxy) Start(ctx context.Context, update Updater)

Start initializes and begins the proxy's update loop. It ensures the loop is started only once using sync.Once. The loop continuously listens for new seed data from the channel and calls the provided update function. When the context is cancelled, it marks the proxy as shutting down and exits.

func (*Proxy) Stats

func (p *Proxy) Stats() []ServerStat

type RPCProxy

type RPCProxy struct {
	Proxy
}

RPCProxy is a wrapper around Proxy that provides RPC-specific functionality. It embeds the Proxy struct, inheriting its fields and behavior.

func NewRPCProxy

func NewRPCProxy(
	ch chan seed.Seed,
	cfg config.HealthConfig,
	log *slog.Logger,
	lb LoadBalancer,
) *RPCProxy

NewRPCProxy creates and returns a new instance of RPCProxy. It initializes the embedded Proxy with the provided configuration, seed channel, logger, and a load balancer.

func (*RPCProxy) ServeHTTP

func (p *RPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles incoming HTTP requests for the RPCProxy. It satisfies the http.Handler interface, allowing RPCProxy to be used directly with an HTTP server (e.g., http.ListenAndServe).

func (*RPCProxy) Start

func (p *RPCProxy) Start(ctx context.Context)

Start begins the lifecycle of the RPCProxy. It delegates to the embedded Proxy's Start method, passing in the context and the GRPCProxy's update function.

type RatedServer

type RatedServer struct {
	// Server is the underlying server instance.
	*Server
	// Rate is the rate of the server, representing its latency.
	Rate float64
}

RatedServer represents a server with a rate, which is used to determine its likelihood of being selected by the LatencyBased load balancer.

type RestProxy

type RestProxy struct {
	Proxy
}

RestProxy is a wrapper around Proxy that provides REST-specific behavior. It embeds the Proxy struct to reuse shared logic and configuration.

func NewRestProxy

func NewRestProxy(
	ch chan seed.Seed,
	cfg config.HealthConfig,
	log *slog.Logger,
	lb LoadBalancer,
) *RestProxy

NewRestProxy creates and returns a new instance of RestProxy. It initializes the embedded Proxy with the given seed channel, configuration, logger, and load balancer.

func (*RestProxy) ServeHTTP

func (p *RestProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles incoming HTTP requests for the RestProxy. It satisfies the http.Handler interface, allowing RestProxy to be used directly with an HTTP server (e.g., http.ListenAndServe).

func (*RestProxy) Start

func (p *RestProxy) Start(ctx context.Context)

Start begins the lifecycle of the RestProxy. It delegates to the embedded Proxy's Start method, passing in the context and the RestProxy's update function.

type RoundRobin

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

RoundRobin is a simple load balancer that distributes incoming requests across multiple servers in a round-robin fashion.

func NewRoundRobin

func NewRoundRobin(log *slog.Logger) *RoundRobin

NewRoundRobin returns a new RoundRobin load balancer instance.

func (*RoundRobin) Next

func (rr *RoundRobin) Next() *Server

Next returns the next server to be used based on the round-robin algorithm. If the selected server is unhealthy, it will recursively try the next server.

func (*RoundRobin) Update

func (rr *RoundRobin) Update(servers []*Server)

Update updates the list of available servers.

type Server

type Server struct {
	Url *url.URL
	// contains filtered or unexported fields
}

func (*Server) ErrorRate

func (s *Server) ErrorRate() float64

func (*Server) Healthy

func (s *Server) Healthy() bool

Healthy returns whether the server is currently healthy by delegating to the underlying node's health check. This is used by load balancers to determine if requests should be routed to this server.

type ServerStat

type ServerStat struct {
	Name        string
	URL         string
	Avg         time.Duration
	Degraded    bool
	Initialized bool
	Requests    int64
	ErrorRate   float64
}

type Updater

type Updater func(s seed.Seed)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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