Documentation ¶
Index ¶
- Constants
- Variables
- func GenerateSalt(size int) []byte
- func IsMySQLError(err error) bool
- func Uint32N(a uint64) uint64
- type Authenticator
- type BCConfig
- type BackendConnManager
- func (mgr *BackendConnManager) ClientAddr() string
- func (mgr *BackendConnManager) ClientInBytes() uint64
- func (mgr *BackendConnManager) ClientOutBytes() uint64
- func (mgr *BackendConnManager) Close() error
- func (mgr *BackendConnManager) Connect(ctx context.Context, clientIO *pnet.PacketIO, ...) error
- func (mgr *BackendConnManager) ConnectionID() uint64
- func (mgr *BackendConnManager) ExecuteCmd(ctx context.Context, request []byte) (err error)
- func (mgr *BackendConnManager) GracefulClose()
- func (mgr *BackendConnManager) NotifyBackendStatus(status router.BackendStatus)
- func (mgr *BackendConnManager) QuitSource() ErrorSource
- func (mgr *BackendConnManager) Redirect(newAddr string) bool
- func (mgr *BackendConnManager) ServerAddr() string
- func (mgr *BackendConnManager) SetEventReceiver(receiver router.ConnEventReceiver)
- func (mgr *BackendConnManager) SetValue(key, val any)
- func (mgr *BackendConnManager) UpdateLogger(fields ...zap.Field)
- func (mgr *BackendConnManager) Value(key any) any
- type CmdProcessor
- type ConnContext
- type ConnContextKey
- type CustomHandshakeHandler
- func (h *CustomHandshakeHandler) GetCapability() pnet.Capability
- func (h *CustomHandshakeHandler) GetRouter(ctx ConnContext, resp *pnet.HandshakeResp) (router.Router, error)
- func (h *CustomHandshakeHandler) GetServerVersion() string
- func (h *CustomHandshakeHandler) HandleHandshakeResp(ctx ConnContext, resp *pnet.HandshakeResp) error
- func (h *CustomHandshakeHandler) OnConnClose(ctx ConnContext) error
- func (h *CustomHandshakeHandler) OnHandshake(ctx ConnContext, addr string, err error)
- func (h *CustomHandshakeHandler) OnTraffic(ctx ConnContext)
- type DefaultHandshakeHandler
- func (handler *DefaultHandshakeHandler) GetCapability() pnet.Capability
- func (handler *DefaultHandshakeHandler) GetRouter(ctx ConnContext, resp *pnet.HandshakeResp) (router.Router, error)
- func (handler *DefaultHandshakeHandler) GetServerVersion() string
- func (handler *DefaultHandshakeHandler) HandleHandshakeResp(ConnContext, *pnet.HandshakeResp) error
- func (handler *DefaultHandshakeHandler) OnConnClose(ConnContext) error
- func (handler *DefaultHandshakeHandler) OnHandshake(ConnContext, string, error)
- func (handler *DefaultHandshakeHandler) OnTraffic(ConnContext)
- type ErrorSource
- type HandshakeHandler
Constants ¶
const ( DialTimeout = 1 * time.Second CheckBackendInterval = time.Minute )
const ( StatusInTrans uint32 = 1 << iota StatusQuit StatusPrepareWaitExecute StatusPrepareWaitFetch )
const SupportedServerCapabilities = pnet.ClientLongPassword | pnet.ClientFoundRows | pnet.ClientConnectWithDB | pnet.ClientODBC | pnet.ClientLocalFiles | pnet.ClientInteractive | pnet.ClientLongFlag | pnet.ClientSSL | pnet.ClientTransactions | pnet.ClientReserved | pnet.ClientSecureConnection | pnet.ClientMultiStatements | pnet.ClientMultiResults | pnet.ClientPluginAuth | pnet.ClientConnectAttrs | pnet.ClientPluginAuthLenencClientData | requiredFrontendCaps | defRequiredBackendCaps
SupportedServerCapabilities is the default supported capabilities. Other server capabilities are not supported. TiDB supports ClientDeprecateEOF since v6.3.0.
Variables ¶
var ( ErrCapabilityNegotiation = errors.New("capability negotiation failed") ErrTLSConfigRequired = errors.New("require TLS config on TiProxy when require-backend-tls=true") )
var ( ErrClientConn = errors.New("this is an error from client") ErrBackendConn = errors.New("this is an error from backend") )
var (
ErrCloseConnMgr = errors.New("failed to close connection manager")
)
Functions ¶
func GenerateSalt ¶
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 ¶
IsMySQLError returns true if the error is a MySQL error.
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 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 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 (h *CustomHandshakeHandler) GetRouter(ctx ConnContext, resp *pnet.HandshakeResp) (router.Router, error)
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 }