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 ¶
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.
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) Start ¶
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.
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.
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.