router

package
v1.3.3-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoBackend    = errors.New("no available backend")
	ErrPortConflict = errors.New("port routing conflict")
)

Functions

This section is empty.

Types

type BackendInst

type BackendInst interface {
	ID() string
	Addr() string
	Healthy() bool
	Local() bool
	Keyspace() string
	ClusterName() string
}

BackendInst defines a backend that a connection is redirecting to.

type BackendSelector

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

func (*BackendSelector) Finish

func (bs *BackendSelector) Finish(conn RedirectableConn, succeed bool)

func (*BackendSelector) Next

func (bs *BackendSelector) Next() (BackendInst, error)

type ClientInfo

type ClientInfo struct {
	ClientAddr net.Addr
	ProxyAddr  net.Addr
	// ListenerPort is the SQL listener port that accepted the connection.
	ListenerPort string
}

type ConnEventReceiver

type ConnEventReceiver interface {
	OnRedirectSucceed(from, to string, conn RedirectableConn) error
	OnRedirectFail(from, to string, conn RedirectableConn) error
	OnConnClosed(backendID string, conn RedirectableConn) error
}

ConnEventReceiver receives connection events.

type Group

type Group struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Group is used for one backend group that can be matched by CIDR, username, database, or resource group list.

func NewGroup

func NewGroup(values []string, bpCreator func(lg *zap.Logger) policy.BalancePolicy, matchType MatchType, lg *zap.Logger) (*Group, error)

func (*Group) AddBackend

func (g *Group) AddBackend(backendID string, backend *backendWrapper)

func (*Group) Balance

func (g *Group) Balance(ctx context.Context)

func (*Group) CloseTimedOutFailoverConnections

func (g *Group) CloseTimedOutFailoverConnections(now time.Time)

func (*Group) ConnCount

func (g *Group) ConnCount() int

func (*Group) EqualValues

func (g *Group) EqualValues(values []string) bool

func (*Group) Intersect

func (g *Group) Intersect(values []string) bool

Intersect returns if any cidrs are the same. In next-gen, backend cidrs may increase or decrease but they stay in the same group. E.g. enable public endpoint (3 cidrs) -> enable private endpoint (6 cidrs) -> disable public endpoint (3 cidrs).

func (*Group) Match

func (g *Group) Match(clientInfo ClientInfo) bool

func (*Group) OnConnClosed

func (g *Group) OnConnClosed(backendID string, conn RedirectableConn) error

OnConnClosed implements ConnEventReceiver.OnConnClosed interface.

func (*Group) OnRedirectFail

func (g *Group) OnRedirectFail(from, to string, conn RedirectableConn) error

OnRedirectFail implements ConnEventReceiver.OnRedirectFail interface.

func (*Group) OnRedirectSucceed

func (g *Group) OnRedirectSucceed(from, to string, conn RedirectableConn) error

OnRedirectSucceed implements ConnEventReceiver.OnRedirectSucceed interface.

func (*Group) RedirectConnections

func (g *Group) RedirectConnections() error

RedirectConnections implements Router.RedirectConnections interface. It redirects all connections compulsively. It's only used for testing.

func (*Group) RefreshCidr

func (g *Group) RefreshCidr()

Backend CIDRs may change anytime.

func (*Group) Route

func (g *Group) Route(excluded []BackendInst) (policy.BackendCtx, error)

func (*Group) SetConfig

func (g *Group) SetConfig(cfg *config.Config)

func (*Group) UpdateFailover

func (g *Group) UpdateFailover(now time.Time)

type MatchType

type MatchType int
const (
	// Match all connections, used when there is only one backend group.
	MatchAll MatchType = iota
	// Match connections based on the client CIDR.
	MatchClientCIDR
	// Match connections based on proxy CIDR. If proxy-protocol is disabled, route by the client CIDR.
	MatchProxyCIDR
	// Match connections based on the local SQL listener port.
	MatchPort
)

type RedirectableConn

type RedirectableConn interface {
	SetEventReceiver(receiver ConnEventReceiver)
	SetValue(key, val any)
	Value(key any) any
	// Redirect returns false if the current conn is not redirectable.
	Redirect(backend BackendInst) bool
	// ForceClose closes the connection immediately and returns false if it's already closing.
	ForceClose() bool
	ConnectionID() uint64
	ConnInfo() []zap.Field
}

RedirectableConn indicates a redirect-able connection.

type Router

type Router interface {
	GetBackendSelector(clientInfo ClientInfo) BackendSelector
	HealthyBackendCount() int
	RefreshBackend()
	RedirectConnections() error
	ConnCount() int
	// ServerVersion returns the TiDB version.
	ServerVersion() string
	Close()
}

Router routes client connections to backends.

type ScoreBasedRouter

type ScoreBasedRouter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ScoreBasedRouter is an implementation of Router interface. It routes a connection based on score.

func NewScoreBasedRouter

func NewScoreBasedRouter(logger *zap.Logger) *ScoreBasedRouter

NewScoreBasedRouter creates a ScoreBasedRouter.

func (*ScoreBasedRouter) Close

func (router *ScoreBasedRouter) Close()

Close implements Router.Close interface.

func (*ScoreBasedRouter) ConnCount

func (router *ScoreBasedRouter) ConnCount() int

func (*ScoreBasedRouter) GetBackendSelector

func (router *ScoreBasedRouter) GetBackendSelector(clientInfo ClientInfo) BackendSelector

GetBackendSelector implements Router.GetBackendSelector interface.

func (*ScoreBasedRouter) HealthyBackendCount added in v1.3.0

func (router *ScoreBasedRouter) HealthyBackendCount() int

func (*ScoreBasedRouter) Init

func (r *ScoreBasedRouter) Init(ctx context.Context, ob observer.BackendObserver, bpCreator func(lg *zap.Logger) policy.BalancePolicy,
	cfgGetter config.ConfigGetter, cfgCh <-chan *config.Config)

func (*ScoreBasedRouter) RedirectConnections

func (router *ScoreBasedRouter) RedirectConnections() error

RedirectConnections implements Router.RedirectConnections interface. It redirects all connections compulsively. It's only used for testing.

func (*ScoreBasedRouter) RefreshBackend

func (router *ScoreBasedRouter) RefreshBackend()

RefreshBackend implements Router.GetBackendSelector interface.

func (*ScoreBasedRouter) ServerVersion

func (router *ScoreBasedRouter) ServerVersion() string

type StaticBackend

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

func NewStaticBackend

func NewStaticBackend(addr string) *StaticBackend

func (*StaticBackend) Addr

func (b *StaticBackend) Addr() string

func (*StaticBackend) ClusterName

func (b *StaticBackend) ClusterName() string

func (*StaticBackend) Healthy

func (b *StaticBackend) Healthy() bool

func (*StaticBackend) ID

func (b *StaticBackend) ID() string

func (*StaticBackend) Keyspace

func (b *StaticBackend) Keyspace() string

func (*StaticBackend) Local

func (b *StaticBackend) Local() bool

func (*StaticBackend) SetHealthy

func (b *StaticBackend) SetHealthy(healthy bool)

func (*StaticBackend) SetKeyspace

func (b *StaticBackend) SetKeyspace(k string)

type StaticRouter

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

func NewStaticRouter

func NewStaticRouter(addrs []string) *StaticRouter

func (*StaticRouter) Close

func (r *StaticRouter) Close()

func (*StaticRouter) ConnCount

func (r *StaticRouter) ConnCount() int

func (*StaticRouter) GetBackendSelector

func (r *StaticRouter) GetBackendSelector(_ ClientInfo) BackendSelector

func (*StaticRouter) HealthyBackendCount added in v1.3.0

func (r *StaticRouter) HealthyBackendCount() int

func (*StaticRouter) OnConnClosed

func (r *StaticRouter) OnConnClosed(backendID string, conn RedirectableConn) error

func (*StaticRouter) OnRedirectFail

func (r *StaticRouter) OnRedirectFail(from, to string, conn RedirectableConn) error

func (*StaticRouter) OnRedirectSucceed

func (r *StaticRouter) OnRedirectSucceed(from, to string, conn RedirectableConn) error

func (*StaticRouter) RedirectConnections

func (r *StaticRouter) RedirectConnections() error

func (*StaticRouter) RefreshBackend

func (r *StaticRouter) RefreshBackend()

func (*StaticRouter) ServerVersion

func (r *StaticRouter) ServerVersion() string

Jump to

Keyboard shortcuts

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