balancer

package
v2.8.3 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package balancer defines APIs for load balancing in gRPC.

Index

Constants

This section is empty.

Variables

View Source
var (
	Random          = gsel.NewBuilderRandom()
	Weight          = gsel.NewBuilderWeight()
	RoundRobin      = gsel.NewBuilderRoundRobin()
	LeastConnection = gsel.NewBuilderLeastConnection()
)

Functions

This section is empty.

Types

type Balancer

type Balancer struct{}

func (Balancer) Register

func (Balancer) Register(builders ...gsel.Builder)

Register registers the given balancer builder with the given name.

func (Balancer) WithLeastConnection

func (b Balancer) WithLeastConnection() grpc.DialOption

WithLeastConnection returns a grpc.DialOption which enables the least connection load balancing.

func (Balancer) WithName

func (b Balancer) WithName(name string) grpc.DialOption

WithName returns a grpc.DialOption which enables the load balancing by name.

func (Balancer) WithRandom

func (b Balancer) WithRandom() grpc.DialOption

WithRandom returns a grpc.DialOption which enables random load balancing.

func (Balancer) WithRoundRobin

func (b Balancer) WithRoundRobin() grpc.DialOption

WithRoundRobin returns a grpc.DialOption which enables round-robin load balancing.

func (Balancer) WithWeight

func (b Balancer) WithWeight() grpc.DialOption

WithWeight returns a grpc.DialOption which enables weight load balancing.

type Builder

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

Builder implements grpc balancer base.PickerBuilder, which returns a picker that will be used by gRPC to pick a SubConn.

func (*Builder) Build

func (b *Builder) Build(info base.PickerBuildInfo) balancer.Picker

Build returns a picker that will be used by gRPC to pick a SubConn.

type Node

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

Node is the node for the balancer.

func (*Node) Address

func (n *Node) Address() string

Address returns the address of the node.

func (*Node) Service

func (n *Node) Service() gsvc.Service

Service returns the service of the node.

type Picker

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

Picker implements grpc balancer.Picker, which is used by gRPC to pick a SubConn to send an RPC. Balancer is expected to generate a new picker from its snapshot every time its internal state has changed.

The pickers used by gRPC can be updated by ClientConn.UpdateState().

func (*Picker) Pick

func (p *Picker) Pick(info balancer.PickInfo) (balancer.PickResult, error)

Pick returns the connection to use for this RPC and related information.

Pick should not block. If the balancer needs to do I/O or any blocking or time-consuming work to service this call, it should return ErrNoSubConnAvailable, and the Pick call will be repeated by gRPC when the Picker is updated (using ClientConn.UpdateState).

If an error is returned:

  • If the error is ErrNoSubConnAvailable, gRPC will block until a new Picker is provided by the balancer (using ClientConn.UpdateState).

  • If the error is a status error (implemented by the grpc/status package), gRPC will terminate the RPC with the code and message provided.

  • For all other errors, wait for ready RPCs will wait, but non-wait for ready RPCs will be terminated with this error's Error() string and status code Unavailable.

Jump to

Keyboard shortcuts

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