backend

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2023 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DialTimeout          = 1 * time.Second
	CheckBackendInterval = time.Minute
)
View Source
const (
	StatusInTrans uint32 = 1 << iota
	StatusQuit
	StatusPrepareWaitExecute
	StatusPrepareWaitFetch
)

SupportedServerCapabilities is the default supported capabilities. Other server capabilities are not supported. TiDB supports ClientDeprecateEOF since v6.3.0.

Variables

View Source
var (
	ErrCapabilityNegotiation = errors.New("capability negotiation failed")
	ErrTLSConfigRequired     = errors.New("require TLS config on TiProxy when require-backend-tls=true")
)
View Source
var (
	ErrClientConn  = errors.New("this is an error from client")
	ErrBackendConn = errors.New("this is an error from backend")
)
View Source
var (
	ErrCloseConnMgr = errors.New("failed to close connection manager")
)

Functions

func GenerateSalt

func GenerateSalt(size int) []byte

Buf generates a random string using ASCII characters but avoid separator character. Ref https://github.com/mysql/mysql-server/blob/5.7/mysys_ssl/crypt_genhash_impl.cc#L435.

func IsMySQLError

func IsMySQLError(err error) bool

IsMySQLError returns true if the error is a MySQL error.

func Uint32N

func Uint32N(a uint64) uint64

Types

type Authenticator

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

Authenticator handshakes with the client and the backend.

func (*Authenticator) String

func (auth *Authenticator) String() string

type BCConfig

type BCConfig struct {
	ProxyProtocol        bool
	RequireBackendTLS    bool
	CheckBackendInterval time.Duration
	HealthyKeepAlive     config.KeepAlive
	UnhealthyKeepAlive   config.KeepAlive
}

type BackendConnManager

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

BackendConnManager migrates a session from one BackendConnection to another.

The signal processing goroutine tries to migrate the session once it receives a signal. If the session is not ready at that time, the cmd executing goroutine will try after executing commands.

If redirection fails, it doesn't retry and waits for the next signal, because: - If it disconnects immediately: it's even worse than graceful shutdown. - If it retries after each command: the latency will be unacceptable afterwards if it always fails. - If it stops receiving signals: the previous new backend may be abnormal but the next new backend may be good.

func NewBackendConnManager

func NewBackendConnManager(logger *zap.Logger, handshakeHandler HandshakeHandler, connectionID uint64, config *BCConfig) *BackendConnManager

NewBackendConnManager creates a BackendConnManager.

func (*BackendConnManager) ClientAddr

func (mgr *BackendConnManager) ClientAddr() string

func (*BackendConnManager) ClientInBytes

func (mgr *BackendConnManager) ClientInBytes() uint64

func (*BackendConnManager) ClientOutBytes

func (mgr *BackendConnManager) ClientOutBytes() uint64

func (*BackendConnManager) Close

func (mgr *BackendConnManager) Close() error

Close releases all resources.

func (*BackendConnManager) Connect

func (mgr *BackendConnManager) Connect(ctx context.Context, clientIO *pnet.PacketIO, frontendTLSConfig, backendTLSConfig *tls.Config) error

Connect connects to the first backend and then start watching redirection signals.

func (*BackendConnManager) ConnectionID

func (mgr *BackendConnManager) ConnectionID() uint64

ConnectionID implements RedirectableConn.ConnectionID interface. It returns the ID of the frontend connection. The ID stays still after session migration.

func (*BackendConnManager) ExecuteCmd

func (mgr *BackendConnManager) ExecuteCmd(ctx context.Context, request []byte) (err error)

ExecuteCmd forwards messages between the client and the backend. If it finds that the session is ready for redirection, it migrates the session.

func (*BackendConnManager) GracefulClose

func (mgr *BackendConnManager) GracefulClose()

GracefulClose waits for the end of the transaction and closes the session.

func (*BackendConnManager) NotifyBackendStatus

func (mgr *BackendConnManager) NotifyBackendStatus(status router.BackendStatus)

NotifyBackendStatus notifies the backend status to mgr. The request to the unhealthy backend may block sometimes, instead of fail immediately. So we set a shorter keep alive timeout for the unhealthy backends.

func (*BackendConnManager) QuitSource

func (mgr *BackendConnManager) QuitSource() ErrorSource

func (*BackendConnManager) Redirect

func (mgr *BackendConnManager) Redirect(newAddr string) bool

Redirect implements RedirectableConn.Redirect interface. It redirects the current session to the newAddr. Note that the caller requires the function to be non-blocking.

func (*BackendConnManager) ServerAddr

func (mgr *BackendConnManager) ServerAddr() string

func (*BackendConnManager) SetEventReceiver

func (mgr *BackendConnManager) SetEventReceiver(receiver router.ConnEventReceiver)

SetEventReceiver implements RedirectableConn.SetEventReceiver interface. The receiver sends redirection signals and watches redirecting events.

func (*BackendConnManager) SetValue

func (mgr *BackendConnManager) SetValue(key, val any)

func (*BackendConnManager) UpdateLogger

func (mgr *BackendConnManager) UpdateLogger(fields ...zap.Field)

func (*BackendConnManager) Value

func (mgr *BackendConnManager) Value(key any) any

type CmdProcessor

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

CmdProcessor maintains the transaction and prepared statement status and decides whether the session can be redirected.

func NewCmdProcessor

func NewCmdProcessor() *CmdProcessor

type ConnContext

type ConnContext interface {
	ClientAddr() string
	ServerAddr() string
	ClientInBytes() uint64
	ClientOutBytes() uint64
	QuitSource() ErrorSource
	UpdateLogger(fields ...zap.Field)
	SetValue(key, val any)
	Value(key any) any
}

type ConnContextKey

type ConnContextKey string

Context keys.

const (
	ConnContextKeyTLSState ConnContextKey = "tls-state"
)

type CustomHandshakeHandler

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

func (*CustomHandshakeHandler) GetCapability

func (h *CustomHandshakeHandler) GetCapability() pnet.Capability

func (*CustomHandshakeHandler) GetRouter

func (*CustomHandshakeHandler) GetServerVersion

func (h *CustomHandshakeHandler) GetServerVersion() string

func (*CustomHandshakeHandler) HandleHandshakeResp

func (h *CustomHandshakeHandler) HandleHandshakeResp(ctx ConnContext, resp *pnet.HandshakeResp) error

func (*CustomHandshakeHandler) OnConnClose

func (h *CustomHandshakeHandler) OnConnClose(ctx ConnContext) error

func (*CustomHandshakeHandler) OnHandshake

func (h *CustomHandshakeHandler) OnHandshake(ctx ConnContext, addr string, err error)

func (*CustomHandshakeHandler) OnTraffic

func (h *CustomHandshakeHandler) OnTraffic(ctx ConnContext)

type DefaultHandshakeHandler

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

func NewDefaultHandshakeHandler

func NewDefaultHandshakeHandler(nsManager *namespace.NamespaceManager, serverVersion string) *DefaultHandshakeHandler

func (*DefaultHandshakeHandler) GetCapability

func (handler *DefaultHandshakeHandler) GetCapability() pnet.Capability

func (*DefaultHandshakeHandler) GetRouter

func (handler *DefaultHandshakeHandler) GetRouter(ctx ConnContext, resp *pnet.HandshakeResp) (router.Router, error)

func (*DefaultHandshakeHandler) GetServerVersion

func (handler *DefaultHandshakeHandler) GetServerVersion() string

func (*DefaultHandshakeHandler) HandleHandshakeResp

func (handler *DefaultHandshakeHandler) HandleHandshakeResp(ConnContext, *pnet.HandshakeResp) error

func (*DefaultHandshakeHandler) OnConnClose

func (handler *DefaultHandshakeHandler) OnConnClose(ConnContext) error

func (*DefaultHandshakeHandler) OnHandshake

func (handler *DefaultHandshakeHandler) OnHandshake(ConnContext, string, error)

func (*DefaultHandshakeHandler) OnTraffic

func (handler *DefaultHandshakeHandler) OnTraffic(ConnContext)

type ErrorSource

type ErrorSource int
const (
	// SrcClientQuit includes: client quit; bad client conn
	SrcClientQuit ErrorSource = iota
	// SrcClientErr includes: wrong password; mal format packet
	SrcClientErr
	// SrcProxyQuit includes: proxy graceful shutdown
	SrcProxyQuit
	// SrcProxyErr includes: cannot get backend list; capability negotiation
	SrcProxyErr
	// SrcBackendQuit includes: backend quit
	SrcBackendQuit
	// SrcBackendErr is reserved
	SrcBackendErr
)

func (ErrorSource) String

func (es ErrorSource) String() string

type HandshakeHandler

type HandshakeHandler interface {
	HandleHandshakeResp(ctx ConnContext, resp *pnet.HandshakeResp) error
	GetRouter(ctx ConnContext, resp *pnet.HandshakeResp) (router.Router, error)
	OnHandshake(ctx ConnContext, to string, err error)
	OnConnClose(ctx ConnContext) error
	OnTraffic(ctx ConnContext)
	GetCapability() pnet.Capability
	GetServerVersion() string
}

Jump to

Keyboard shortcuts

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