Navigation Menu

Skip to content

Commit

Permalink
use SetDeadline
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfitz committed Jan 24, 2012
1 parent e76709e commit 345e5c6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions memcache/memcache.go
Expand Up @@ -172,6 +172,10 @@ func (cn *conn) release() {
cn.c.putFreeConn(cn.addr, cn)
}

func (cn *conn) extendDeadline() {
cn.nc.SetDeadline(time.Now().Add(cn.c.netTimeout()))
}

// condRelease releases this connection if the error pointed to by err
// is is nil (not an error) or is only a protocol level error (e.g. a
// cache miss). The purpose is to not recycle TCP connections that
Expand Down Expand Up @@ -258,19 +262,21 @@ func (c *Client) dial(addr net.Addr) (net.Conn, error) {
func (c *Client) getConn(addr net.Addr) (*conn, error) {
cn, ok := c.getFreeConn(addr)
if ok {
cn.extendDeadline()
return cn, nil
}
nc, err := c.dial(addr)
if err != nil {
return nil, err
}
nc.SetTimeout(int64(c.netTimeout()))
return &conn{
cn = &conn{
nc: nc,
addr: addr,
rw: bufio.NewReadWriter(bufio.NewReader(nc), bufio.NewWriter(nc)),
c: c,
}, nil
}
cn.extendDeadline()
return cn, nil
}

func (c *Client) onItem(item *Item, fn func(*Client, *bufio.ReadWriter, *Item) error) error {
Expand Down
2 changes: 1 addition & 1 deletion memcache/memcache_test.go
Expand Up @@ -63,7 +63,7 @@ func TestUnixSocket(t *testing.T) {
if _, err := os.Stat(sock); err == nil {
break
}
time.Sleep(25e6 * int64(i))
time.Sleep(time.Duration(25 * i) * time.Millisecond)
}

testWithClient(t, New(sock))
Expand Down

0 comments on commit 345e5c6

Please sign in to comment.