Documentation ¶
Index ¶
- Constants
- Variables
- func Attr2ZapFields(attrs map[string]string) []zap.Field
- func CheckSqlPort(conn net.Conn) error
- func DumpLengthEncodedInt(buffer []byte, n uint64) []byte
- func DumpLengthEncodedString(buffer []byte, bytes []byte) []byte
- func DumpUint16(buffer []byte, n uint16) []byte
- func DumpUint32(buffer []byte, n uint32) []byte
- func IsDisconnectError(err error) bool
- func IsEOFPacket(firstByte byte, length int) bool
- func IsErrorPacket(firstByte byte) bool
- func IsMySQLError(err error) bool
- func IsOKPacket(firstByte byte) bool
- func IsResultSetOKPacket(firstByte byte, length int) bool
- func IsRetryableError(err error) bool
- func MakeChangeUser(req *ChangeUserReq, capability Capability) []byte
- func MakeHandshakeResponse(resp *HandshakeResp) []byte
- func ParseErrorPacket(data []byte) *gomysql.MyError
- func ParseLengthEncodedBytes(b []byte) ([]byte, bool, int, error)
- func ParseLengthEncodedInt(b []byte) (num uint64, isNull bool, n int)
- func ParseNullTermString(b []byte) (str []byte, remain []byte)
- func ParseOKPacket(data []byte) uint16
- func ParseQueryPacket(data []byte) string
- func ReadFull(prw packetReadWriter, b []byte) error
- func SkipLengthEncodedInt(b []byte) int
- func WithProxy(pi *PacketIO)
- func WithRemoteAddr(readdr string, addr net.Addr) func(pi *PacketIO)
- func WithWrapError(err error) func(pi *PacketIO)
- type Capability
- type ChangeUserReq
- type Command
- type CompressAlgorithm
- type HandshakeResp
- type Header
- type PacketIO
- func (p *PacketIO) ApplyOpts(opts ...PacketIOption)
- func (p *PacketIO) ClientTLSHandshake(tlsConfig *tls.Config) error
- func (p *PacketIO) Close() error
- func (p *PacketIO) EnableProxyClient(proxy *proxyprotocol.Proxy)
- func (p *PacketIO) EnableProxyServer()
- func (p *PacketIO) Flush() error
- func (p *PacketIO) ForwardUntil(dest *PacketIO, ...) error
- func (p *PacketIO) GetSequence() uint8
- func (p *PacketIO) GracefulClose() error
- func (p *PacketIO) InBytes() uint64
- func (p *PacketIO) InPackets() uint64
- func (p *PacketIO) IsPeerActive() bool
- func (p *PacketIO) LastKeepAlive() config.KeepAlive
- func (p *PacketIO) LocalAddr() net.Addr
- func (p *PacketIO) OutBytes() uint64
- func (p *PacketIO) OutPackets() uint64
- func (p *PacketIO) Proxy() *proxyprotocol.Proxy
- func (p *PacketIO) ReadPacket() (data []byte, err error)
- func (p *PacketIO) ReadSSLRequestOrHandshakeResp() (pkt []byte, isSSL bool, err error)
- func (p *PacketIO) RemoteAddr() net.Addr
- func (p *PacketIO) ResetSequence()
- func (p *PacketIO) ServerTLSHandshake(tlsConfig *tls.Config) (tls.ConnectionState, error)
- func (p *PacketIO) SetCompressionAlgorithm(algorithm CompressAlgorithm, zstdLevel int) error
- func (p *PacketIO) SetKeepalive(cfg config.KeepAlive) error
- func (p *PacketIO) TLSConnectionState() tls.ConnectionState
- func (p *PacketIO) WriteEOFPacket(status uint16) error
- func (p *PacketIO) WriteErrPacket(merr *mysql.MyError) error
- func (p *PacketIO) WriteInitialHandshake(capability Capability, salt [20]byte, authPlugin string, serverVersion string, ...) error
- func (p *PacketIO) WriteOKPacket(status uint16, header Header) error
- func (p *PacketIO) WritePacket(data []byte, flush bool) (err error)
- func (p *PacketIO) WriteShaCommand() error
- func (p *PacketIO) WriteSwitchRequest(authPlugin string, salt [20]byte) error
- func (p *PacketIO) WriteUserError(err error)
- type PacketIOption
Constants ¶
const ( AuthNativePassword = "mysql_native_password" AuthCachingSha2Password = "caching_sha2_password" AuthTiDBSM3Password = "tidb_sm3_password" AuthMySQLClearPassword = "mysql_clear_password" AuthSocket = "auth_socket" AuthTiDBSessionToken = "tidb_session_token" AuthTiDBAuthToken = "tidb_auth_token" )
const ( ShaCommand = 1 FastAuthFail = 4 )
const ( AttrNameClientVersion = "_client_version" // libmysqlclient & Connector/C++ & Connector/J & Connector/Net & Connector/Python AttrNameClientName1 = "_client_name" // libmysqlclient & Connector/C++ & Connector/J & Connector/Python & mysqlnd AttrNameClientName2 = "_program_name" // Connector/Net AttrNameProgramName = "program_name" // MySQL Client & MySQL Shell )
The connection attribute names that are logged. https://dev.mysql.com/doc/mysql-perfschema-excerpt/8.2/en/performance-schema-connection-attribute-tables.html
const ( ServerStatusInTrans uint16 = 0x0001 ServerStatusAutocommit uint16 = 0x0002 ServerMoreResultsExists uint16 = 0x0008 ServerStatusNoGoodIndexUsed uint16 = 0x0010 ServerStatusNoIndexUsed uint16 = 0x0020 ServerStatusCursorExists uint16 = 0x0040 ServerStatusLastRowSend uint16 = 0x0080 ServerStatusDBDropped uint16 = 0x0100 ServerStatusNoBackslashEscaped uint16 = 0x0200 ServerStatusMetadataChanged uint16 = 0x0400 ServerStatusWasSlow uint16 = 0x0800 ServerPSOutParams uint16 = 0x1000 )
Server information.
const (
DefaultConnBufferSize = 32 * 1024
)
const (
// MaxPayloadLen is the max packet payload length.
MaxPayloadLen = 1<<24 - 1
)
Variables ¶
var ( ErrReadConn = errors.New("failed to read the connection") ErrWriteConn = errors.New("failed to write the connection") ErrRelayConn = errors.New("failed to relay the connection") ErrFlushConn = errors.New("failed to flush the connection") ErrCloseConn = errors.New("failed to close the connection") ErrHandshakeTLS = errors.New("failed to complete tls handshake") )
var ( ServerVersion = mysql.ServerVersion Collation = uint8(mysql.DefaultCollationID) Status = ServerStatusAutocommit )
var (
ErrInvalidSequence = dbterror.ClassServer.NewStd(errno.ErrInvalidSequence)
)
var (
ErrSaltNotLongEnough = errors.New("salt is not long enough")
)
Functions ¶
func Attr2ZapFields ¶
Attr2ZapFields converts connection attributes to log fields. We only pick some of them because others may be too sensitive to be logged.
func CheckSqlPort ¶ added in v1.1.0
CheckSqlPort checks whether the SQL port is available.
func DumpLengthEncodedInt ¶
func DumpLengthEncodedString ¶
DumpLengthEncodedString dumps string<int>.
func DumpUint16 ¶
func DumpUint32 ¶
func IsDisconnectError ¶
IsDisconnectError returns whether the error is caused by peer disconnection.
func IsEOFPacket ¶
IsEOFPacket returns true if it's an EOF packet.
func IsErrorPacket ¶
IsErrorPacket returns true if it's an error packet.
func IsMySQLError ¶ added in v0.2.0
IsMySQLError returns true if the error is a MySQL error.
func IsOKPacket ¶
IsOKPacket returns true if it's an OK packet (but not ResultSet OK).
func IsResultSetOKPacket ¶
IsResultSetOKPacket returns true if it's an OK packet after the result set when CLIENT_DEPRECATE_EOF is enabled. A row packet may also begin with 0xfe, so we need to judge it with the packet length. See https://mariadb.com/kb/en/result-set-packets/
func IsRetryableError ¶ added in v1.1.0
func MakeChangeUser ¶
func MakeChangeUser(req *ChangeUserReq, capability Capability) []byte
MakeChangeUser creates the data of COM_CHANGE_USER.
func MakeHandshakeResponse ¶
func MakeHandshakeResponse(resp *HandshakeResp) []byte
func ParseErrorPacket ¶
ParseErrorPacket transforms an error packet into a MyError object.
func ParseNullTermString ¶
func ParseOKPacket ¶
ParseOKPacket parses an OK packet and only returns server status.
func ParseQueryPacket ¶ added in v1.0.0
ParseQueryPacket returns the statement in the CMD_QUERY packet. data is the payload after byte CMD_QUERY.
func ReadFull ¶
ReadFull is used to replace io.ReadFull to erase boundary check, function calls and interface conversion. It is a hot path when many rows are returned.
func SkipLengthEncodedInt ¶
SkipLengthEncodedInt skips the int and only returns the length of the encoded int.
func WithWrapError ¶
Types ¶
type Capability ¶
type Capability uint32
const ( ClientLongPassword Capability = 1 << iota ClientFoundRows ClientLongFlag ClientConnectWithDB ClientNoSchema ClientCompress ClientODBC ClientLocalFiles ClientIgnoreSpace ClientProtocol41 ClientInteractive ClientSSL ClientIgnoreSigpipe ClientTransactions ClientReserved ClientSecureConnection ClientMultiStatements ClientMultiResults ClientPSMultiResults ClientPluginAuth ClientConnectAttrs ClientPluginAuthLenencClientData ClientCanHandleExpiredPasswords ClientSessionTrack ClientDeprecateEOF ClientOptionalResultsetMetadata ClientZstdCompressionAlgorithm ClientQueryAttributes MultiFactorAuthentication ClientCapabilityExtension ClientSSLVerifyServerCert ClientRememberOptions )
Capability flags. Ref https://dev.mysql.com/doc/dev/mysql-server/latest/group__group__cs__capabilities__flags.html.
func ParseInitialHandshake ¶
func ParseInitialHandshake(data []byte) (Capability, uint64, string)
ParseInitialHandshake parses the initial handshake received from the server.
func (*Capability) MarshalText ¶
func (f *Capability) MarshalText() ([]byte, error)
func (Capability) String ¶
func (f Capability) String() string
func (Capability) Uint32 ¶
func (f Capability) Uint32() uint32
func (*Capability) UnmarshalText ¶
func (f *Capability) UnmarshalText(o []byte) error
type ChangeUserReq ¶
type ChangeUserReq struct { Attrs map[string]string User string DB string AuthPlugin string AuthData []byte Charset []byte }
func ParseChangeUser ¶
func ParseChangeUser(data []byte, capability Capability) (*ChangeUserReq, error)
ParseChangeUser parses the data of COM_CHANGE_USER.
type Command ¶
type Command byte
const ( ComSleep Command = iota ComQuit ComInitDB ComQuery ComFieldList ComCreateDB ComDropDB ComRefresh ComDeprecated1 ComStatistics ComProcessInfo ComConnect ComProcessKill ComDebug ComPing ComTime ComDelayedInsert ComChangeUser ComBinlogDump ComTableDump ComConnectOut ComRegisterSlave ComStmtPrepare ComStmtExecute ComStmtSendLongData ComStmtClose ComStmtReset ComSetOption ComStmtFetch ComDaemon ComBinlogDumpGtid ComResetConnection ComEnd // Not a real command )
Command information. Ref https://dev.mysql.com/doc/dev/mysql-server/latest/my__command_8h.html#ae2ff1badf13d2b8099af8b47831281e1.
func (*Command) MarshalText ¶
func (*Command) UnmarshalText ¶
type CompressAlgorithm ¶
type CompressAlgorithm int
CompressAlgorithm is the algorithm for MySQL compressed protocol.
const ( // CompressionNone indicates no compression in use. CompressionNone CompressAlgorithm = iota // CompressionZlib is zlib/deflate. CompressionZlib // CompressionZstd is Facebook's Zstandard. CompressionZstd )
type HandshakeResp ¶
type HandshakeResp struct { Attrs map[string]string User string DB string AuthPlugin string AuthData []byte Capability Capability ZstdLevel int Collation uint8 }
HandshakeResp indicates the response read from the client.
func ParseHandshakeResponse ¶
func ParseHandshakeResponse(data []byte) (*HandshakeResp, error)
type Header ¶
type Header byte
type PacketIO ¶
type PacketIO struct {
// contains filtered or unexported fields
}
PacketIO is a helper to read and write sql and proxy protocol.
func NewPacketIO ¶
func (*PacketIO) ApplyOpts ¶
func (p *PacketIO) ApplyOpts(opts ...PacketIOption)
func (*PacketIO) ClientTLSHandshake ¶
func (*PacketIO) EnableProxyClient ¶
func (p *PacketIO) EnableProxyClient(proxy *proxyprotocol.Proxy)
func (*PacketIO) EnableProxyServer ¶
func (p *PacketIO) EnableProxyServer()
func (*PacketIO) ForwardUntil ¶
func (*PacketIO) GetSequence ¶
GetSequence is used in tests to assert that the sequences on the client and server are equal.
func (*PacketIO) GracefulClose ¶
func (*PacketIO) IsPeerActive ¶
func (*PacketIO) LastKeepAlive ¶
LastKeepAlive is used for test.
func (*PacketIO) OutPackets ¶ added in v1.0.0
func (*PacketIO) Proxy ¶
func (p *PacketIO) Proxy() *proxyprotocol.Proxy
Proxy returned parsed proxy header from clients if any.
func (*PacketIO) ReadPacket ¶
ReadPacket reads data and removes the header
func (*PacketIO) ReadSSLRequestOrHandshakeResp ¶
func (*PacketIO) RemoteAddr ¶
func (*PacketIO) ResetSequence ¶
func (p *PacketIO) ResetSequence()
func (*PacketIO) ServerTLSHandshake ¶
func (*PacketIO) SetCompressionAlgorithm ¶
func (p *PacketIO) SetCompressionAlgorithm(algorithm CompressAlgorithm, zstdLevel int) error
func (*PacketIO) TLSConnectionState ¶
func (p *PacketIO) TLSConnectionState() tls.ConnectionState
func (*PacketIO) WriteEOFPacket ¶
WriteEOFPacket writes an EOF packet. It's only for testing.
func (*PacketIO) WriteErrPacket ¶
WriteErrPacket writes an Error packet.
func (*PacketIO) WriteInitialHandshake ¶
func (p *PacketIO) WriteInitialHandshake(capability Capability, salt [20]byte, authPlugin string, serverVersion string, connID uint64) error
WriteInitialHandshake writes an initial handshake as a server. It's used for tenant-aware routing and testing.
func (*PacketIO) WriteOKPacket ¶
WriteOKPacket writes an OK packet. It's only for testing.
func (*PacketIO) WritePacket ¶
WritePacket writes data without a header
func (*PacketIO) WriteShaCommand ¶
func (*PacketIO) WriteSwitchRequest ¶
WriteSwitchRequest writes a switch request to the client. It's only for testing.
func (*PacketIO) WriteUserError ¶
WriteUserError writes an unknown error to the client.
type PacketIOption ¶
type PacketIOption = func(*PacketIO)