@@ -3,6 +3,7 @@ package server
3
3
import (
4
4
"errors"
5
5
"net"
6
+ "sync"
6
7
"sync/atomic"
7
8
8
9
"github.com/go-mysql-org/go-mysql/mysql"
@@ -36,51 +37,47 @@ type Conn struct {
36
37
closed atomic.Bool
37
38
}
38
39
39
- var baseConnID uint32 = 10000
40
+ var (
41
+ baseConnID uint32 = 10000
42
+ defaultServer = sync .OnceValue (func () * Server {
43
+ return NewDefaultServer ()
44
+ })
45
+ )
40
46
41
47
// NewConn: create connection with default server settings
48
+ //
49
+ // Deprecated: Use [Server.NewConn] instead.
42
50
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
+ }
52
53
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
+ }
63
60
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 )
68
65
69
- return c , nil
66
+ return s . NewCustomizedConn ( conn , p , h )
70
67
}
71
68
72
69
// 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 ) {
74
71
var packetConn * packet.Conn
75
- if serverConf .tlsConfig != nil {
72
+ if s .tlsConfig != nil {
76
73
packetConn = packet .NewTLSConn (conn )
77
74
} else {
78
75
packetConn = packet .NewConn (conn )
79
76
}
80
77
81
78
c := & Conn {
82
79
Conn : packetConn ,
83
- serverConf : serverConf ,
80
+ serverConf : s ,
84
81
credentialProvider : p ,
85
82
h : h ,
86
83
connectionID : atomic .AddUint32 (& baseConnID , 1 ),
0 commit comments