2016-03-12 11:52:13 +03:00
|
|
|
package pool
|
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
import "sync"
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
type StickyConnPool struct {
|
|
|
|
pool *ConnPool
|
|
|
|
reusable bool
|
|
|
|
|
|
|
|
cn *Conn
|
|
|
|
closed bool
|
2017-01-28 11:53:10 +03:00
|
|
|
mu sync.Mutex
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
var _ Pooler = (*StickyConnPool)(nil)
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func NewStickyConnPool(pool *ConnPool, reusable bool) *StickyConnPool {
|
|
|
|
return &StickyConnPool{
|
|
|
|
pool: pool,
|
|
|
|
reusable: reusable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
func (p *StickyConnPool) NewConn() (*Conn, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *StickyConnPool) CloseConn(*Conn) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2016-09-29 15:07:04 +03:00
|
|
|
func (p *StickyConnPool) Get() (*Conn, bool, error) {
|
2017-01-28 11:53:10 +03:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
if p.closed {
|
2016-09-29 15:07:04 +03:00
|
|
|
return nil, false, ErrClosed
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
if p.cn != nil {
|
2016-09-29 15:07:04 +03:00
|
|
|
return p.cn, false, nil
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-09-29 15:07:04 +03:00
|
|
|
cn, _, err := p.pool.Get()
|
2016-03-12 11:52:13 +03:00
|
|
|
if err != nil {
|
2016-09-29 15:07:04 +03:00
|
|
|
return nil, false, err
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
p.cn = cn
|
2016-09-29 15:07:04 +03:00
|
|
|
return cn, true, nil
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
func (p *StickyConnPool) putUpstream() (err error) {
|
2016-03-12 11:52:13 +03:00
|
|
|
err = p.pool.Put(p.cn)
|
|
|
|
p.cn = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *StickyConnPool) Put(cn *Conn) error {
|
2017-01-28 11:53:10 +03:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
if p.closed {
|
2016-03-14 14:17:33 +03:00
|
|
|
return ErrClosed
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
func (p *StickyConnPool) removeUpstream() error {
|
|
|
|
err := p.pool.Remove(p.cn)
|
2016-03-12 11:52:13 +03:00
|
|
|
p.cn = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
func (p *StickyConnPool) Remove(cn *Conn) error {
|
2017-01-28 11:53:10 +03:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
if p.closed {
|
2016-03-14 14:17:33 +03:00
|
|
|
return nil
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2017-04-17 15:43:58 +03:00
|
|
|
return p.removeUpstream()
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *StickyConnPool) Len() int {
|
2017-01-28 11:53:10 +03:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
if p.cn == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *StickyConnPool) FreeLen() int {
|
2017-01-28 11:53:10 +03:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
if p.cn == nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
func (p *StickyConnPool) Stats() *Stats {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
func (p *StickyConnPool) Close() error {
|
2017-01-28 11:53:10 +03:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
if p.closed {
|
2016-03-14 14:17:33 +03:00
|
|
|
return ErrClosed
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
p.closed = true
|
|
|
|
var err error
|
|
|
|
if p.cn != nil {
|
|
|
|
if p.reusable {
|
2016-12-03 18:30:13 +03:00
|
|
|
err = p.putUpstream()
|
2016-03-12 11:52:13 +03:00
|
|
|
} else {
|
2017-04-17 15:43:58 +03:00
|
|
|
err = p.removeUpstream()
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|