diff --git a/cool/conn.go b/cool/conn.go index 511e79b..81f49e5 100644 --- a/cool/conn.go +++ b/cool/conn.go @@ -25,7 +25,7 @@ var _ net.Conn = (*Conn)(nil) type Conn struct { net.Conn - sync.RWMutex + mu sync.RWMutex unusable bool c *cool t time.Time @@ -34,8 +34,8 @@ type Conn struct { // Close overrides the net.Conn Close method // put the usable connection back to the pool instead of closing it func (cc *Conn) Close() error { - cc.RLock() - defer cc.RUnlock() + cc.mu.RLock() + defer cc.mu.RUnlock() if cc.unusable { if cc.Conn != nil { return cc.Conn.Close() @@ -46,13 +46,13 @@ func (cc *Conn) Close() error { } func (cc *Conn) MarkUnusable() { - cc.Lock() - defer cc.Unlock() + cc.mu.Lock() + defer cc.mu.Unlock() cc.unusable = true } func (cc *Conn) IsUnusable() bool { - cc.RLock() - defer cc.RUnlock() + cc.mu.RLock() + defer cc.mu.RUnlock() return cc.unusable } diff --git a/cool/cool.go b/cool/cool.go index 057dc40..e692428 100644 --- a/cool/cool.go +++ b/cool/cool.go @@ -41,8 +41,8 @@ type Cool interface { type Producer func() (net.Conn, error) type cool struct { - sync.Once - sync.RWMutex + once sync.Once + mu sync.RWMutex options *options connC chan net.Conn producer Producer @@ -69,10 +69,10 @@ func New(init, max int, producer Producer, opts ...Option) (Cool, error) { } func (c *cool) Get() (net.Conn, error) { - c.RLock() + c.mu.RLock() connC := c.connC producer := c.producer - c.RUnlock() + c.mu.RUnlock() if connC == nil { return nil, ErrClosed } @@ -91,7 +91,7 @@ func (c *cool) Get() (net.Conn, error) { } func (c *cool) Close() { - c.Do(func() { + c.once.Do(func() { connC := c.connC c.options = nil c.connC = nil @@ -104,8 +104,8 @@ func (c *cool) Close() { } func (c *cool) Len() int { - c.RLock() - defer c.RUnlock() + c.mu.RLock() + defer c.mu.RUnlock() return len(c.connC) } @@ -113,8 +113,8 @@ func (c *cool) put(conn net.Conn) error { if conn == nil { return ErrNilConn } - c.RLock() - defer c.RUnlock() + c.mu.RLock() + defer c.mu.RUnlock() if c.connC == nil { // cool is closed return conn.Close()