Ejemplo n.º 1
0
func (s *Server) newClientConn(co net.Conn) *ClientConn {
	c := new(ClientConn)
	switch co.(type) {
	case *net.TCPConn:
		tcpConn := co.(*net.TCPConn)

		//SetNoDelay controls whether the operating system should delay packet transmission
		// in hopes of sending fewer packets (Nagle's algorithm).
		// The default is true (no delay),
		// meaning that data is sent as soon as possible after a Write.
		//I set this option false.
		tcpConn.SetNoDelay(false)
		c.c = tcpConn
	default:
		c.c = co
	}

	c.pkg = mysql.NewPacketIO(c.c)
	c.proxy = s

	c.pkg.Sequence = 0

	c.connectionId = atomic.AddUint32(&baseConnId, 1)

	c.status = mysql.SERVER_STATUS_AUTOCOMMIT

	c.salt = mysql.RandomBuf(20)

	c.closed = false

	c.collation = mysql.DEFAULT_COLLATION_ID
	c.charset = mysql.DEFAULT_CHARSET

	return c
}
Ejemplo n.º 2
0
func (c *Conn) ReConnect() error {
	if c.conn != nil {
		c.conn.Close()
	}

	n := "tcp"
	if strings.Contains(c.addr, "/") {
		n = "unix"
	}

	var err error
	var netConn net.Conn
	if c.client.proxy.cfg.TlsClient {
		netConn, err = tls.Dial(n, c.addr, c.client.proxy.cfg.TlsClientConf)
	} else {
		netConn, err = net.Dial(n, c.addr)
	}
	if err != nil {
		return err
	}

	switch netConn.(type) {
	case *net.TCPConn:
		tcpConn := netConn.(*net.TCPConn)

		//SetNoDelay controls whether the operating system should delay packet transmission
		// in hopes of sending fewer packets (Nagle's algorithm).
		// The default is true (no delay),
		// meaning that data is sent as soon as possible after a Write.
		//I set this option false.
		tcpConn.SetNoDelay(false)
		c.conn = tcpConn
	default:
		c.conn = netConn
	}
	c.pkg = mysql.NewPacketIO(c.conn)

	if err := c.readInitialHandshake(); err != nil {
		c.conn.Close()
		return err
	}

	if err := c.writeAuthHandshake(); err != nil {
		c.conn.Close()

		return err
	}

	if _, err := c.readOK(); err != nil {
		c.conn.Close()

		return err
	}

	//we must always use autocommit
	if !c.IsAutoCommit() {
		if _, err := c.exec("set autocommit = 1"); err != nil {
			c.conn.Close()

			return err
		}
	}

	c.lastPing = time.Now().Unix()

	return nil
}