From 5abcdf4c427e817ecb5b272b235a8356e6f85674 Mon Sep 17 00:00:00 2001 From: lrita Date: Tue, 16 Jan 2018 20:26:54 +0800 Subject: [PATCH] using sync.RWMutex instead of sync.Mutex in channelPool. When there are high concurrent invoking with channelPool, thus, the send and receive method of channel will be slow because of lock contention which in channel internal. So we using a sync.RWMutex instead of sync.Mutex in channelPool to reduce the mutex contention when we call channelPool.Get() and channelPool.put() --- channel.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/channel.go b/channel.go index aeed94f..36bb20d 100644 --- a/channel.go +++ b/channel.go @@ -10,7 +10,7 @@ import ( // channelPool implements the Pool interface based on buffered channels. type channelPool struct { // storage for our net.Conn connections - mu sync.Mutex + mu sync.RWMutex conns chan net.Conn // net.Conn generator @@ -51,10 +51,10 @@ func NewChannelPool(initialCap, maxCap int, factory Factory) (Pool, error) { } func (c *channelPool) getConnsAndFactory() (chan net.Conn, Factory) { - c.mu.Lock() + c.mu.RLock() conns := c.conns factory := c.factory - c.mu.Unlock() + c.mu.RUnlock() return conns, factory } @@ -93,8 +93,8 @@ func (c *channelPool) put(conn net.Conn) error { return errors.New("connection is nil. rejecting") } - c.mu.Lock() - defer c.mu.Unlock() + c.mu.RLock() + defer c.mu.RUnlock() if c.conns == nil { // pool is closed, close passed connection