Skip to content

Commit ee9447d

Browse files
dveedenccoVeillerosstimothy
authored
server: Rework NewConn/defaultServer (#1021)
* server: Rework NewConn/defaultServer * Make NewConn and NewCustomizedConn into methods * Add defaultServer back for now * Update server/conn.go Co-authored-by: ccoVeille <[email protected]> * Update server/conn.go Co-authored-by: ccoVeille <[email protected]> * Update server/conn.go Co-authored-by: rosstimothy <[email protected]> * Update server/conn.go Co-authored-by: ccoVeille <[email protected]> * Update server/conn.go Co-authored-by: ccoVeille <[email protected]> * fixup * fix missing return * Fix formatting * Fix deprecation statement * Fix use of deprecated method * Use sync.OnceValue() --------- Co-authored-by: ccoVeille <[email protected]> Co-authored-by: rosstimothy <[email protected]>
1 parent 639eccd commit ee9447d

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

cmd/go-mysqlserver/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ func main() {
2626

2727
// Create a connection with user root and an empty password.
2828
// You can use your own handler to handle command here.
29-
conn, err := server.NewConn(c, "root", "", server.EmptyHandler{})
29+
srv := server.NewDefaultServer()
30+
conn, err := srv.NewConn(c, "root", "", server.EmptyHandler{})
3031
if err != nil {
3132
log.Fatal(err)
3233
}

driver/driver_options_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ func CreateMockServer(t *testing.T) *testServer {
275275

276276
handler := &mockHandler{}
277277

278+
s := server.NewDefaultServer()
279+
278280
go func() {
279281
for {
280282
conn, err := l.Accept()
@@ -283,7 +285,7 @@ func CreateMockServer(t *testing.T) *testServer {
283285
}
284286

285287
go func() {
286-
co, err := server.NewCustomizedConn(conn, defaultServer, inMemProvider, handler)
288+
co, err := s.NewCustomizedConn(conn, inMemProvider, handler)
287289
if err != nil {
288290
return
289291
}

server/conn.go

+25-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package server
33
import (
44
"errors"
55
"net"
6+
"sync"
67
"sync/atomic"
78

89
"github.com/go-mysql-org/go-mysql/mysql"
@@ -36,51 +37,47 @@ type Conn struct {
3637
closed atomic.Bool
3738
}
3839

39-
var baseConnID uint32 = 10000
40+
var (
41+
baseConnID uint32 = 10000
42+
defaultServer = sync.OnceValue(func() *Server {
43+
return NewDefaultServer()
44+
})
45+
)
4046

4147
// NewConn: create connection with default server settings
48+
//
49+
// Deprecated: Use [Server.NewConn] instead.
4250
func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, error) {
43-
p := NewInMemoryProvider()
44-
p.AddUser(user, password)
45-
46-
var packetConn *packet.Conn
47-
if defaultServer.tlsConfig != nil {
48-
packetConn = packet.NewTLSConn(conn)
49-
} else {
50-
packetConn = packet.NewConn(conn)
51-
}
51+
return defaultServer().NewConn(conn, user, password, h)
52+
}
5253

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

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

69-
return c, nil
66+
return s.NewCustomizedConn(conn, p, h)
7067
}
7168

7269
// NewCustomizedConn: create connection with customized server settings
73-
func NewCustomizedConn(conn net.Conn, serverConf *Server, p CredentialProvider, h Handler) (*Conn, error) {
70+
func (s *Server) NewCustomizedConn(conn net.Conn, p CredentialProvider, h Handler) (*Conn, error) {
7471
var packetConn *packet.Conn
75-
if serverConf.tlsConfig != nil {
72+
if s.tlsConfig != nil {
7673
packetConn = packet.NewTLSConn(conn)
7774
} else {
7875
packetConn = packet.NewConn(conn)
7976
}
8077

8178
c := &Conn{
8279
Conn: packetConn,
83-
serverConf: serverConf,
80+
serverConf: s,
8481
credentialProvider: p,
8582
h: h,
8683
connectionID: atomic.AddUint32(&baseConnID, 1),

server/server_conf.go

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"github.com/go-mysql-org/go-mysql/mysql"
99
)
1010

11-
var defaultServer = NewDefaultServer()
12-
1311
// Defines a basic MySQL server with configs.
1412
//
1513
// We do not aim at implementing the whole MySQL connection suite to have the best compatibilities for the clients.

0 commit comments

Comments
 (0)