From fe2f173901cdf26c6b118497a8c9f332ec04e179 Mon Sep 17 00:00:00 2001 From: hopehook Date: Sat, 22 Sep 2018 19:56:41 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Connect=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95,=20=E6=94=AF=E6=8C=81=E5=88=9B=E5=BB=BA=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8D=95=E7=8B=AC=E7=9A=84=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel.go | 16 ++++++++++------ pool.go | 2 ++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/channel.go b/channel.go index e1da7b5..d125b88 100644 --- a/channel.go +++ b/channel.go @@ -110,16 +110,20 @@ func (c *channelPool) Get() (interface{}, error) { } return wrapConn.conn, nil default: - conn, err := c.factory() - if err != nil { - return nil, err - } - - return conn, nil + return c.Connect() } } } +//Conn 重新创建一个连接 +func (c *channelPool) Connect() (interface{}, error) { + conn, err := c.factory() + if err != nil { + return nil, err + } + return conn, nil +} + //Put 将连接放回pool中 func (c *channelPool) Put(conn interface{}) error { if conn == nil { diff --git a/pool.go b/pool.go index 7ec40ff..e7bb520 100644 --- a/pool.go +++ b/pool.go @@ -11,6 +11,8 @@ var ( type Pool interface { Get() (interface{}, error) + Connect() (interface{}, error) + Put(interface{}) error Close(interface{}) error From 5bb2e1d2e1073e8444ca7e82c9bd32e1c3ae712f Mon Sep 17 00:00:00 2001 From: hopehook Date: Mon, 24 Sep 2018 11:06:53 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Fix:=20Connect=20Close=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=81=9A=E4=B8=A5=E6=A0=BC=E7=9A=84=E6=A3=80=E6=9F=A5=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=20Release=20=E4=B9=8B=E5=90=8E=E8=AF=AF?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/channel.go b/channel.go index d125b88..b560b5e 100644 --- a/channel.go +++ b/channel.go @@ -117,6 +117,9 @@ func (c *channelPool) Get() (interface{}, error) { //Conn 重新创建一个连接 func (c *channelPool) Connect() (interface{}, error) { + if c.factory == nil { + return errors.New("factory func is nil. rejecting") + } conn, err := c.factory() if err != nil { return nil, err @@ -151,6 +154,9 @@ func (c *channelPool) Close(conn interface{}) error { if conn == nil { return errors.New("connection is nil. rejecting") } + if c.close == nil { + return errors.New("close func is nil. rejecting") + } return c.close(conn) } From 4d69fc69b78d3e6a69df85954515485b5bed2867 Mon Sep 17 00:00:00 2001 From: hopehook Date: Sun, 30 Sep 2018 15:05:49 +0800 Subject: [PATCH 3/4] =?UTF-8?q?BugFix:=20Connect=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=BC=BA=E5=A4=B1=20&&=20Release=20=E6=8A=8A?= =?UTF-8?q?=20ping=20=E7=BD=AE=E4=B8=BA=20nil=20&&=20Ping=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20nil=20=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/channel.go b/channel.go index b560b5e..bc8f0e1 100644 --- a/channel.go +++ b/channel.go @@ -118,7 +118,7 @@ func (c *channelPool) Get() (interface{}, error) { //Conn 重新创建一个连接 func (c *channelPool) Connect() (interface{}, error) { if c.factory == nil { - return errors.New("factory func is nil. rejecting") + return nil, errors.New("factory func is nil. rejecting") } conn, err := c.factory() if err != nil { @@ -165,6 +165,9 @@ func (c *channelPool) Ping(conn interface{}) error { if conn == nil { return errors.New("connection is nil. rejecting") } + if c.ping == nil { + return errors.New("ping func is nil. rejecting") + } return c.ping(conn) } @@ -176,6 +179,7 @@ func (c *channelPool) Release() { c.factory = nil closeFun := c.close c.close = nil + c.ping = nil c.mu.Unlock() if conns == nil { From 96e69241f3089ecf0b21003aa2df9976167e9e5e Mon Sep 17 00:00:00 2001 From: hopehook Date: Mon, 22 Apr 2019 14:25:14 +0800 Subject: [PATCH 4/4] =?UTF-8?q?Feat:=20=E4=BF=AE=E6=94=B9=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel.go | 44 +++++++++++++++++++------------------------- pool.go | 4 ++-- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/channel.go b/channel.go index bc8f0e1..abf2f2b 100644 --- a/channel.go +++ b/channel.go @@ -9,21 +9,15 @@ import ( // PoolConfig 连接池相关配置 type PoolConfig struct { - //连接池中拥有的最小连接数 - InitialCap int - //连接池中拥有的最大的连接数 - MaxCap int - //生成连接的方法 - Factory func() (interface{}, error) - //关闭连接的方法 - Close func(interface{}) error - //检查连接是否有效的方法 - Ping func(interface{}) error - //连接最大空闲时间,超过该事件则将失效 - IdleTimeout time.Duration + InitialCap int // 连接池中拥有的最小连接数 + MaxCap int // 连接池中拥有的最大的连接数 + Factory func() (interface{}, error) // 生成连接的方法 + Close func(interface{}) error // 关闭连接的方法 + Ping func(interface{}) error // 检查连接是否有效的方法 + IdleTimeout time.Duration // 连接最大空闲时间,超过该事件则将失效 } -//channelPool 存放连接信息 +// channelPool 存放连接信息 type channelPool struct { mu sync.Mutex conns chan *idleConn @@ -38,7 +32,7 @@ type idleConn struct { t time.Time } -//NewChannelPool 初始化连接 +// NewChannelPool 初始化连接 func NewChannelPool(poolConfig *PoolConfig) (Pool, error) { if poolConfig.InitialCap < 0 || poolConfig.MaxCap <= 0 || poolConfig.InitialCap > poolConfig.MaxCap { return nil, errors.New("invalid capacity settings") @@ -73,7 +67,7 @@ func NewChannelPool(poolConfig *PoolConfig) (Pool, error) { return c, nil } -//getConns 获取所有连接 +// getConns 获取所有连接 func (c *channelPool) getConns() chan *idleConn { c.mu.Lock() conns := c.conns @@ -81,7 +75,7 @@ func (c *channelPool) getConns() chan *idleConn { return conns } -//Get 从pool中取一个连接 +// Get 从pool中取一个连接 func (c *channelPool) Get() (interface{}, error) { conns := c.getConns() if conns == nil { @@ -93,7 +87,7 @@ func (c *channelPool) Get() (interface{}, error) { if wrapConn == nil { return nil, ErrClosed } - //判断是否超时,超时则丢弃 + // 判断是否超时,超时则丢弃 if timeout := c.idleTimeout; timeout > 0 { if wrapConn.t.Add(timeout).Before(time.Now()) { //丢弃并关闭该连接 @@ -101,7 +95,7 @@ func (c *channelPool) Get() (interface{}, error) { continue } } - //判断是否失效,失效则丢弃,如果用户没有设定 ping 方法,就不检查 + // 判断是否失效,失效则丢弃,如果用户没有设定 ping 方法,就不检查 if c.ping != nil { if err := c.Ping(wrapConn.conn); err != nil { fmt.Println("conn is not able to be connected: ", err) @@ -115,7 +109,7 @@ func (c *channelPool) Get() (interface{}, error) { } } -//Conn 重新创建一个连接 +// Conn 重新创建一个连接 func (c *channelPool) Connect() (interface{}, error) { if c.factory == nil { return nil, errors.New("factory func is nil. rejecting") @@ -127,7 +121,7 @@ func (c *channelPool) Connect() (interface{}, error) { return conn, nil } -//Put 将连接放回pool中 +// Put 将连接放回pool中 func (c *channelPool) Put(conn interface{}) error { if conn == nil { return errors.New("connection is nil. rejecting") @@ -149,7 +143,7 @@ func (c *channelPool) Put(conn interface{}) error { } } -//Close 关闭单条连接 +// Close 关闭单条连接 func (c *channelPool) Close(conn interface{}) error { if conn == nil { return errors.New("connection is nil. rejecting") @@ -160,7 +154,7 @@ func (c *channelPool) Close(conn interface{}) error { return c.close(conn) } -//Ping 检查单条连接是否有效 +// Ping 检查单条连接是否有效 func (c *channelPool) Ping(conn interface{}) error { if conn == nil { return errors.New("connection is nil. rejecting") @@ -171,15 +165,15 @@ func (c *channelPool) Ping(conn interface{}) error { return c.ping(conn) } -//Release 释放连接池中所有连接 +// Release 释放连接池中所有连接 func (c *channelPool) Release() { c.mu.Lock() conns := c.conns c.conns = nil c.factory = nil + c.ping = nil closeFun := c.close c.close = nil - c.ping = nil c.mu.Unlock() if conns == nil { @@ -192,7 +186,7 @@ func (c *channelPool) Release() { } } -//Len 连接池中已有的连接 +// Len 连接池中已有的连接 func (c *channelPool) Len() int { return len(c.getConns()) } diff --git a/pool.go b/pool.go index e7bb520..23680ca 100644 --- a/pool.go +++ b/pool.go @@ -3,11 +3,11 @@ package pool import "errors" var ( - //ErrClosed 连接池已经关闭Error + // ErrClosed 连接池已经关闭Error ErrClosed = errors.New("pool is closed") ) -//Pool 基本方法 +// Pool 基本方法 type Pool interface { Get() (interface{}, error)