1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-29 17:41:15 +03:00

Fix connection initialization.

This commit is contained in:
Vladimir Mihailenco
2016-03-15 14:04:35 +02:00
parent 7f594cdbe1
commit 707472c09b
17 changed files with 71 additions and 67 deletions

View File

@ -18,7 +18,9 @@ type Conn struct {
Rd *bufio.Reader
Buf []byte
UsedAt time.Time
Inited bool
UsedAt time.Time
ReadTimeout time.Duration
WriteTimeout time.Duration
}
@ -40,8 +42,12 @@ func (cn *Conn) Index() int {
return int(atomic.LoadInt32(&cn.idx))
}
func (cn *Conn) SetIndex(idx int) {
atomic.StoreInt32(&cn.idx, int32(idx))
func (cn *Conn) SetIndex(newIdx int) int {
oldIdx := cn.Index()
if !atomic.CompareAndSwapInt32(&cn.idx, int32(oldIdx), int32(newIdx)) {
return -1
}
return oldIdx
}
func (cn *Conn) IsStale(timeout time.Duration) bool {
@ -72,11 +78,6 @@ func (cn *Conn) RemoteAddr() net.Addr {
return cn.NetConn.RemoteAddr()
}
func (cn *Conn) Close() int {
idx := cn.Index()
if !atomic.CompareAndSwapInt32(&cn.idx, int32(idx), -1) {
return -1
}
_ = cn.NetConn.Close()
return idx
func (cn *Conn) Close() error {
return cn.NetConn.Close()
}