Skip to content

server: Rework NewConn/defaultServer #1021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 2, 2025
3 changes: 2 additions & 1 deletion cmd/go-mysqlserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"log"
"net"

"github.com/go-mysql-org/go-mysql/server"

Check failure on line 7 in cmd/go-mysqlserver/main.go

View workflow job for this annotation

GitHub Actions / golangci

could not import github.com/go-mysql-org/go-mysql/server (-: # github.com/go-mysql-org/go-mysql/server
)

func main() {
Expand All @@ -26,7 +26,8 @@

// Create a connection with user root and an empty password.
// You can use your own handler to handle command here.
conn, err := server.NewConn(c, "root", "", server.EmptyHandler{})
srv := server.NewDefaultServer()
conn, err := srv.NewConn(c, "root", "", server.EmptyHandler{})
if err != nil {
log.Fatal(err)
}
Expand Down
46 changes: 20 additions & 26 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,50 +37,44 @@
}

var baseConnID uint32 = 10000
var defaultServer *Server

// NewConn: create connection with default server settings
//
// Deprecated: Use [Server.NewConn] instead.
func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, error) {
p := NewInMemoryProvider()
p.AddUser(user, password)

var packetConn *packet.Conn
if defaultServer.tlsConfig != nil {
packetConn = packet.NewTLSConn(conn)
} else {
packetConn = packet.NewConn(conn)
if defaultServer == nil {
defaultServer = NewDefaultServer()
}
}

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / golangci

missing return (typecheck)

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / golangci

missing return) (typecheck)

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / golangci

missing return) (typecheck)

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / Tests with MySQL 8.0.40

missing return

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / Tests with MySQL 8.4.3

missing return

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.24 on ubuntu-22.04

missing return

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.23 on ubuntu-24.04

missing return

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.24 on ubuntu-24.04

missing return

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / platforms (amd64, freebsd)

missing return

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.22 on ubuntu-24.04

missing return

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.22 on ubuntu-22.04

missing return

Check failure on line 49 in server/conn.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.23 on ubuntu-22.04

missing return

c := &Conn{
Conn: packetConn,
serverConf: defaultServer,
credentialProvider: p,
h: h,
connectionID: atomic.AddUint32(&baseConnID, 1),
stmts: make(map[uint32]*Stmt),
salt: mysql.RandomBuf(20),
}
c.closed.Store(false)
// NewCustomizedConn: create connection with customized server settings
//
// Deprecated: Use [Server.NewConn] instead.
func NewCustomizedConn(conn net.Conn, serverConf *Server, p CredentialProvider, h Handler) (*Conn, error) {
return serverConf.NewCustomizedConn(conn, p, h)
}

if err := c.handshake(); err != nil {
c.Close()
return nil, err
}
// NewConn: create connection with default server settings
func (s *Server) NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, error) {
p := NewInMemoryProvider()
p.AddUser(user, password)

return c, nil
return s.NewCustomizedConn(conn, p, h)
}

// NewCustomizedConn: create connection with customized server settings
func NewCustomizedConn(conn net.Conn, serverConf *Server, p CredentialProvider, h Handler) (*Conn, error) {
func (s *Server) NewCustomizedConn(conn net.Conn, p CredentialProvider, h Handler) (*Conn, error) {
var packetConn *packet.Conn
if serverConf.tlsConfig != nil {
if s.tlsConfig != nil {
packetConn = packet.NewTLSConn(conn)
} else {
packetConn = packet.NewConn(conn)
}

c := &Conn{
Conn: packetConn,
serverConf: serverConf,
serverConf: s,
credentialProvider: p,
h: h,
connectionID: atomic.AddUint32(&baseConnID, 1),
Expand Down
2 changes: 0 additions & 2 deletions server/server_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/go-mysql-org/go-mysql/mysql"
)

var defaultServer = NewDefaultServer()

// Defines a basic MySQL server with configs.
//
// We do not aim at implementing the whole MySQL connection suite to have the best compatibilities for the clients.
Expand Down
Loading