1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package xugu
- import (
- "context"
- "database/sql/driver"
- "fmt"
- "net"
- "time"
- )
- type connector struct {
- dsn string
- }
- // Driver implements driver.Connector interface.
- // Driver returns &XuguDriver{}
- func (conntor *connector) Driver() driver.Driver {
- fmt.Println(">>>>>(conntor *connector) Driver()")
- return &XuguDriver{}
- }
- // Connect implements driver.Connector interface.
- // Connect returns a connection to the database.
- /*
- dsn解析
- 创建连接
- 设置为 tcp 长连接(
- 创建连接缓冲区
- 设置连接超时配置
- 接收来自服务端的握手请求
- */
- func (conntor *connector) Connect(ctx context.Context) (driver.Conn, error) {
- fmt.Println(">>>>>(conntor *connector) Connect(ctx context.Context) ")
- dsnConfig := parseDSN(conntor.dsn)
- xgConn := &xuguConn{conn: nil}
- xgConn.dsnConfig = dsnConfig
- nd := net.Dialer{Timeout: 5 * time.Second}
- netConn, err := nd.DialContext(ctx, "tcp", fmt.Sprintf("%s:%s", xgConn.IP, xgConn.Port))
- if err != nil {
- fmt.Println("tcp error", err)
- return nil, err
- }
- // 启用 TCP 保活
- if tc, ok := netConn.(*net.TCPConn); ok {
- if err := tc.SetKeepAlive(true); err != nil {
- //c.cfg.Logger.Print(err) // 如果设置保活失败,记录错误但不终止
- fmt.Println("SetKeepAlive error", err)
- }
- }
- xgConn.conn = netConn
- xgConn.Type = HT_CONN
- xgConn.readBuff = newBuffer(xgConn.conn)
- fmt.Println("连接串为: ", conntor.dsn)
-
- err = xgSockOpenConn(ctx, xgConn)
- if err != nil {
- return nil, err
- }
- return xgConn, nil
- }
|