Exemplo n.º 1
0
func (c *Client) handleResponse(session *Conn) error {
	length := c.pending.Len()
	for i := 0; i < length; i++ {
		var resp proto.Response
		err := resp.Decode(session)
		if err != nil {
			return errors.Trace(err)
		}
		c.notifyOne(&resp)
	}

	return nil
}
Exemplo n.º 2
0
func (s *testMultiServerSuite) testGetTimestamp(c *C, duration time.Duration) {
	// get leader tso
	var (
		addr  string
		watch <-chan zk.Event
		err   error

		conn net.Conn

		last proto.Response

		begin = time.Now()
	)

	for {
		if time.Now().Sub(begin) > duration {
			break
		}

		if len(addr) == 0 {
			addr, watch, err = util.GetWatchLeader(s.zkConn, s.rootPath)
			c.Assert(err, IsNil)

			conn, err = net.Dial("tcp", addr)
			c.Assert(err, IsNil)
		}

		_, err = conn.Write([]byte{0x00})
		if err == nil {
			var resp proto.Response
			err = resp.Decode(conn)
			if err == nil {
				c.Assert(resp.Physical, GreaterEqual, last.Physical)
				c.Assert(resp.Logical, Greater, last.Logical)
			}
		}

		if err != nil {
			conn.Close()

			// try connect the closed leader again, must error.
			conn, err = net.Dial("tcp", addr)
			if err == nil {
				conn.Write([]byte{0x00})
				var resp proto.Response
				err = resp.Decode(conn)
				conn.Close()
			}
			c.Assert(err, NotNil)

			// leader stop or close, we will wait some time for leader change.
			select {
			case <-watch:
				log.Warnf("leader changed")
			case <-time.After(500 * time.Millisecond):
				log.Warnf("wait change timeout, force get leader again")
			}

			addr = ""
		}
	}

	conn.Close()
}