Skip to content

Commit c87a409

Browse files
committed
only make NewDefaultServer once when neccessary
1 parent c030e48 commit c87a409

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

server/conn.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, err
4646
p := NewInMemoryProvider()
4747
p.AddUser(user, password)
4848

49+
defaultServer := NewDefaultServer()
50+
4951
var packetConn *packet.Conn
5052
if defaultServer.tlsConfig != nil {
5153
packetConn = packet.NewTLSConn(conn)

server/server_conf.go

Lines changed: 2 additions & 4 deletions
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.
@@ -43,7 +41,7 @@ type Server struct {
4341
// non-TLS connection). By default, it will verify the client certificate if present. You can enable TLS support on
4442
// the client side without providing a client-side certificate. So only when you need the server to verify client
4543
// identity for maximum security, you need to set a signed certificate for the client.
46-
func NewDefaultServer() *Server {
44+
var NewDefaultServer = sync.OnceValue(func() *Server {
4745
caPem, caKey := generateCA()
4846
certPem, keyPem := generateAndSignRSACerts(caPem, caKey)
4947
tlsConf := NewServerTLSConfig(caPem, certPem, keyPem, tls.VerifyClientCertIfGiven)
@@ -58,7 +56,7 @@ func NewDefaultServer() *Server {
5856
tlsConfig: tlsConf,
5957
cacheShaPassword: new(sync.Map),
6058
}
61-
}
59+
})
6260

6361
// NewServer: New mysql server with customized settings.
6462
//

0 commit comments

Comments
 (0)