Documentation ¶
Index ¶
- Variables
- type BackendEventReceiver
- type BackendFetcher
- type BackendHealth
- type BackendInfo
- type BackendInst
- type BackendObserver
- type BackendSelector
- type BackendStatus
- type ConnEventReceiver
- type DefaultHealthCheck
- type HealthCheck
- type PDFetcher
- type RedirectableConn
- type Router
- type ScoreBasedRouter
- func (router *ScoreBasedRouter) Close()
- func (router *ScoreBasedRouter) ConnCount() int
- func (router *ScoreBasedRouter) GetBackendSelector() BackendSelector
- func (r *ScoreBasedRouter) Init(fetcher BackendFetcher, hc HealthCheck, cfg *config.HealthCheck) error
- func (router *ScoreBasedRouter) OnBackendChanged(backends map[string]*BackendHealth, err error)
- func (router *ScoreBasedRouter) OnConnClosed(addr string, conn RedirectableConn) error
- func (router *ScoreBasedRouter) OnRedirectFail(from, to string, conn RedirectableConn) error
- func (router *ScoreBasedRouter) OnRedirectSucceed(from, to string, conn RedirectableConn) error
- func (router *ScoreBasedRouter) RedirectConnections() error
- func (router *ScoreBasedRouter) RefreshBackend()
- func (router *ScoreBasedRouter) ServerVersion() string
- type StaticBackend
- type StaticFetcher
- type StaticRouter
- func (r *StaticRouter) Close()
- func (r *StaticRouter) ConnCount() int
- func (r *StaticRouter) GetBackendSelector() BackendSelector
- func (r *StaticRouter) OnConnClosed(addr string, conn RedirectableConn) error
- func (r *StaticRouter) OnRedirectFail(from, to string, conn RedirectableConn) error
- func (r *StaticRouter) OnRedirectSucceed(from, to string, conn RedirectableConn) error
- func (r *StaticRouter) RedirectConnections() error
- func (r *StaticRouter) RefreshBackend()
- func (r *StaticRouter) ServerVersion() string
- type TopologyFetcher
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoBackend = errors.New("no available backend")
)
Functions ¶
This section is empty.
Types ¶
type BackendEventReceiver ¶
type BackendEventReceiver interface { // OnBackendChanged is called when the backend list changes. OnBackendChanged(backends map[string]*BackendHealth, err error) }
BackendEventReceiver receives the event of backend status change.
type BackendFetcher ¶
type BackendFetcher interface {
GetBackendList(context.Context) (map[string]*BackendInfo, error)
}
BackendFetcher is an interface to fetch the backend list.
type BackendHealth ¶ added in v0.2.0
type BackendHealth struct { Status BackendStatus // The error occurred when health check fails. It's used to log why the backend becomes unhealthy. PingErr error // The backend version that returned to the client during handshake. ServerVersion string }
func (*BackendHealth) String ¶ added in v0.2.0
func (bh *BackendHealth) String() string
type BackendInfo ¶
BackendInfo stores the status info of each backend.
type BackendInst ¶ added in v0.2.0
BackendInst defines a backend that a connection is redirecting to.
type BackendObserver ¶
type BackendObserver struct {
// contains filtered or unexported fields
}
BackendObserver refreshes backend list and notifies BackendEventReceiver.
func NewBackendObserver ¶
func NewBackendObserver(logger *zap.Logger, eventReceiver BackendEventReceiver, config *config.HealthCheck, backendFetcher BackendFetcher, hc HealthCheck) *BackendObserver
NewBackendObserver creates a BackendObserver.
func StartBackendObserver ¶
func StartBackendObserver(logger *zap.Logger, eventReceiver BackendEventReceiver, config *config.HealthCheck, backendFetcher BackendFetcher, hc HealthCheck) *BackendObserver
StartBackendObserver creates a BackendObserver and starts watching.
func (*BackendObserver) Refresh ¶
func (bo *BackendObserver) Refresh()
Refresh indicates the observer to refresh immediately.
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 BackendStatus ¶
type BackendStatus int
const ( StatusHealthy BackendStatus = iota StatusCannotConnect StatusMemoryHigh StatusRunSlow StatusSchemaOutdated )
func (BackendStatus) String ¶
func (bs BackendStatus) String() string
func (BackendStatus) ToScore ¶
func (bs BackendStatus) ToScore() int
type ConnEventReceiver ¶
type ConnEventReceiver interface { OnRedirectSucceed(from, to string, conn RedirectableConn) error OnRedirectFail(from, to string, conn RedirectableConn) error OnConnClosed(addr string, conn RedirectableConn) error }
ConnEventReceiver receives connection events.
type DefaultHealthCheck ¶ added in v0.2.0
type DefaultHealthCheck struct {
// contains filtered or unexported fields
}
func NewDefaultHealthCheck ¶ added in v0.2.0
func NewDefaultHealthCheck(httpCli *http.Client, cfg *config.HealthCheck, logger *zap.Logger) *DefaultHealthCheck
func (*DefaultHealthCheck) Check ¶ added in v0.2.0
func (dhc *DefaultHealthCheck) Check(ctx context.Context, addr string, info *BackendInfo) *BackendHealth
type HealthCheck ¶ added in v0.2.0
type HealthCheck interface {
Check(ctx context.Context, addr string, info *BackendInfo) *BackendHealth
}
HealthCheck is used to check the backends of one backend. One can pass a customized health check function to the observer.
type PDFetcher ¶
type PDFetcher struct {
// contains filtered or unexported fields
}
PDFetcher fetches backend list from PD.
func NewPDFetcher ¶
func NewPDFetcher(tpFetcher TopologyFetcher, logger *zap.Logger, config *config.HealthCheck) *PDFetcher
func (*PDFetcher) GetBackendList ¶
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 ConnectionID() uint64 }
RedirectableConn indicates a redirect-able connection.
type Router ¶
type Router interface { // ConnEventReceiver handles connection events to balance connections if possible. ConnEventReceiver GetBackendSelector() BackendSelector RefreshBackend() RedirectConnections() error ConnCount() int // ServerVersion returns the TiDB version. ServerVersion() string Close() }
Router routes client connections to backends.
type ScoreBasedRouter ¶
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() BackendSelector
GetBackendSelector implements Router.GetBackendSelector interface.
func (*ScoreBasedRouter) Init ¶
func (r *ScoreBasedRouter) Init(fetcher BackendFetcher, hc HealthCheck, cfg *config.HealthCheck) error
func (*ScoreBasedRouter) OnBackendChanged ¶
func (router *ScoreBasedRouter) OnBackendChanged(backends map[string]*BackendHealth, err error)
OnBackendChanged implements BackendEventReceiver.OnBackendChanged interface.
func (*ScoreBasedRouter) OnConnClosed ¶
func (router *ScoreBasedRouter) OnConnClosed(addr string, conn RedirectableConn) error
OnConnClosed implements ConnEventReceiver.OnConnClosed interface.
func (*ScoreBasedRouter) OnRedirectFail ¶
func (router *ScoreBasedRouter) OnRedirectFail(from, to string, conn RedirectableConn) error
OnRedirectFail implements ConnEventReceiver.OnRedirectFail interface.
func (*ScoreBasedRouter) OnRedirectSucceed ¶
func (router *ScoreBasedRouter) OnRedirectSucceed(from, to string, conn RedirectableConn) error
OnRedirectSucceed implements ConnEventReceiver.OnRedirectSucceed interface.
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 ¶ added in v1.0.0
type StaticBackend struct {
// contains filtered or unexported fields
}
func NewStaticBackend ¶ added in v1.0.0
func NewStaticBackend(addr string) *StaticBackend
func (*StaticBackend) Addr ¶ added in v1.0.0
func (b *StaticBackend) Addr() string
func (*StaticBackend) Healthy ¶ added in v1.0.0
func (b *StaticBackend) Healthy() bool
func (*StaticBackend) SetHealthy ¶ added in v1.0.0
func (b *StaticBackend) SetHealthy(healthy bool)
type StaticFetcher ¶
type StaticFetcher struct {
// contains filtered or unexported fields
}
StaticFetcher uses configured static addrs. This is only used for testing.
func NewStaticFetcher ¶
func NewStaticFetcher(staticAddrs []string) *StaticFetcher
func (*StaticFetcher) GetBackendList ¶
func (sf *StaticFetcher) GetBackendList(context.Context) (map[string]*BackendInfo, error)
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() BackendSelector
func (*StaticRouter) OnConnClosed ¶
func (r *StaticRouter) OnConnClosed(addr 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