Skip to content

Commit

Permalink
Move helper buffer into connection struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Rozlach committed Sep 2, 2016
1 parent 79b13fe commit cc4edb7
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions zk/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ type Conn struct {
reconnectDelay time.Duration

logger Logger

buf []byte
}

// connOption represents a connection option.
Expand Down Expand Up @@ -195,6 +197,7 @@ func Connect(servers []string, sessionTimeout time.Duration, options ...connOpti
watchers: make(map[watchPathType][]chan Event),
passwd: emptyPassword,
logger: DefaultLogger,
buf: make([]byte, bufferSize),

// Debug
reconnectDelay: 0,
Expand Down Expand Up @@ -601,24 +604,22 @@ func (c *Conn) authenticate() error {
}

func (c *Conn) sendData(req *request) error {
buf := make([]byte, bufferSize)

header := &requestHeader{req.xid, req.opcode}
n, err := encodePacket(buf[4:], header)
n, err := encodePacket(c.buf[4:], header)
if err != nil {
req.recvChan <- response{-1, err}
return nil
}

n2, err := encodePacket(buf[4+n:], req.pkt)
n2, err := encodePacket(c.buf[4+n:], req.pkt)
if err != nil {
req.recvChan <- response{-1, err}
return nil
}

n += n2

binary.BigEndian.PutUint32(buf[:4], uint32(n))
binary.BigEndian.PutUint32(c.buf[:4], uint32(n))

c.requestsLock.Lock()
select {
Expand All @@ -632,7 +633,7 @@ func (c *Conn) sendData(req *request) error {
c.requestsLock.Unlock()

c.conn.SetWriteDeadline(time.Now().Add(c.recvTimeout))
_, err = c.conn.Write(buf[:n+4])
_, err = c.conn.Write(c.buf[:n+4])
c.conn.SetWriteDeadline(time.Time{})
if err != nil {
req.recvChan <- response{-1, err}
Expand All @@ -647,23 +648,22 @@ func (c *Conn) sendLoop() error {
pingTicker := time.NewTicker(c.pingInterval)
defer pingTicker.Stop()

buf := make([]byte, bufferSize)
for {
select {
case req := <-c.sendChan:
if err := c.sendData(req); err != nil {
return err
}
case <-pingTicker.C:
n, err := encodePacket(buf[4:], &requestHeader{Xid: -2, Opcode: opPing})
n, err := encodePacket(c.buf[4:], &requestHeader{Xid: -2, Opcode: opPing})
if err != nil {
panic("zk: opPing should never fail to serialize")
}

binary.BigEndian.PutUint32(buf[:4], uint32(n))
binary.BigEndian.PutUint32(c.buf[:4], uint32(n))

c.conn.SetWriteDeadline(time.Now().Add(c.recvTimeout))
_, err = c.conn.Write(buf[:n+4])
_, err = c.conn.Write(c.buf[:n+4])
c.conn.SetWriteDeadline(time.Time{})
if err != nil {
c.conn.Close()
Expand Down

0 comments on commit cc4edb7

Please sign in to comment.