2016-03-12 11:52:13 +03:00
|
|
|
package pool
|
|
|
|
|
2019-08-02 00:59:53 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2019-08-03 17:21:12 +03:00
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
2019-08-02 00:59:53 +03:00
|
|
|
|
2019-08-03 17:21:12 +03:00
|
|
|
const (
|
|
|
|
stateDefault = 0
|
|
|
|
stateInited = 1
|
|
|
|
stateClosed = 2
|
2019-08-02 00:59:53 +03:00
|
|
|
)
|
2019-06-04 14:05:29 +03:00
|
|
|
|
2019-08-08 10:36:13 +03:00
|
|
|
type BadConnError struct {
|
|
|
|
wrapped error
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:04:19 +03:00
|
|
|
var _ error = (*BadConnError)(nil)
|
|
|
|
|
2019-08-08 10:36:13 +03:00
|
|
|
func (e BadConnError) Error() string {
|
|
|
|
return "pg: Conn is in a bad state"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e BadConnError) Unwrap() error {
|
|
|
|
return e.wrapped
|
|
|
|
}
|
2019-08-03 17:21:12 +03:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
type SingleConnPool struct {
|
2019-08-08 14:22:01 +03:00
|
|
|
pool Pooler
|
|
|
|
level int32 // atomic
|
2019-08-03 17:21:12 +03:00
|
|
|
|
|
|
|
state uint32 // atomic
|
|
|
|
ch chan *Conn
|
|
|
|
|
2019-08-08 10:36:13 +03:00
|
|
|
_badConnError atomic.Value
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
var _ Pooler = (*SingleConnPool)(nil)
|
|
|
|
|
2019-08-03 17:21:12 +03:00
|
|
|
func NewSingleConnPool(pool Pooler) *SingleConnPool {
|
|
|
|
p, ok := pool.(*SingleConnPool)
|
|
|
|
if !ok {
|
|
|
|
p = &SingleConnPool{
|
|
|
|
pool: pool,
|
|
|
|
ch: make(chan *Conn, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
atomic.AddInt32(&p.level, 1)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SingleConnPool) SetConn(cn *Conn) {
|
|
|
|
if atomic.CompareAndSwapUint32(&p.state, stateDefault, stateInited) {
|
|
|
|
p.ch <- cn
|
|
|
|
} else {
|
|
|
|
panic("not reached")
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 17:03:32 +03:00
|
|
|
func (p *SingleConnPool) NewConn(ctx context.Context) (*Conn, error) {
|
|
|
|
return p.pool.NewConn(ctx)
|
2017-04-17 15:43:58 +03:00
|
|
|
}
|
|
|
|
|
2019-08-03 17:21:12 +03:00
|
|
|
func (p *SingleConnPool) CloseConn(cn *Conn) error {
|
|
|
|
return p.pool.CloseConn(cn)
|
2017-04-17 15:43:58 +03:00
|
|
|
}
|
|
|
|
|
2019-08-18 17:03:32 +03:00
|
|
|
func (p *SingleConnPool) Get(ctx context.Context) (*Conn, error) {
|
2019-08-03 17:21:12 +03:00
|
|
|
// In worst case this races with Close which is not a very common operation.
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
switch atomic.LoadUint32(&p.state) {
|
|
|
|
case stateDefault:
|
2019-08-18 17:03:32 +03:00
|
|
|
cn, err := p.pool.Get(ctx)
|
2019-08-03 17:21:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if atomic.CompareAndSwapUint32(&p.state, stateDefault, stateInited) {
|
|
|
|
return cn, nil
|
|
|
|
}
|
2019-08-08 10:36:13 +03:00
|
|
|
p.pool.Remove(cn, ErrClosed)
|
2019-08-03 17:21:12 +03:00
|
|
|
case stateInited:
|
2019-08-08 10:36:13 +03:00
|
|
|
if err := p.badConnError(); err != nil {
|
|
|
|
return nil, err
|
2019-08-03 17:21:12 +03:00
|
|
|
}
|
|
|
|
cn, ok := <-p.ch
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
return cn, nil
|
|
|
|
case stateClosed:
|
|
|
|
return nil, ErrClosed
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2019-08-02 00:59:53 +03:00
|
|
|
}
|
2019-08-03 17:21:12 +03:00
|
|
|
return nil, fmt.Errorf("pg: SingleConnPool.Get: infinite loop")
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
func (p *SingleConnPool) Put(cn *Conn) {
|
2019-08-03 17:21:12 +03:00
|
|
|
defer func() {
|
|
|
|
if recover() != nil {
|
|
|
|
p.freeConn(cn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
p.ch <- cn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SingleConnPool) freeConn(cn *Conn) {
|
2019-08-08 10:36:13 +03:00
|
|
|
if err := p.badConnError(); err != nil {
|
|
|
|
p.pool.Remove(cn, err)
|
2019-08-03 17:21:12 +03:00
|
|
|
} else {
|
|
|
|
p.pool.Put(cn)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 10:36:13 +03:00
|
|
|
func (p *SingleConnPool) Remove(cn *Conn, reason error) {
|
2019-08-03 17:21:12 +03:00
|
|
|
defer func() {
|
|
|
|
if recover() != nil {
|
2019-08-08 10:36:13 +03:00
|
|
|
p.pool.Remove(cn, ErrClosed)
|
2019-08-03 17:21:12 +03:00
|
|
|
}
|
|
|
|
}()
|
2019-08-08 10:36:13 +03:00
|
|
|
p._badConnError.Store(BadConnError{wrapped: reason})
|
2019-08-03 17:21:12 +03:00
|
|
|
p.ch <- cn
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SingleConnPool) Len() int {
|
2019-08-03 17:21:12 +03:00
|
|
|
switch atomic.LoadUint32(&p.state) {
|
|
|
|
case stateDefault:
|
2019-08-02 00:59:53 +03:00
|
|
|
return 0
|
2019-08-03 17:21:12 +03:00
|
|
|
case stateInited:
|
|
|
|
return 1
|
|
|
|
case stateClosed:
|
|
|
|
return 0
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
2019-08-02 00:59:53 +03:00
|
|
|
}
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
func (p *SingleConnPool) IdleLen() int {
|
2019-08-03 17:21:12 +03:00
|
|
|
return len(p.ch)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
func (p *SingleConnPool) Stats() *Stats {
|
2019-08-03 17:21:12 +03:00
|
|
|
return &Stats{}
|
2016-03-14 14:17:33 +03:00
|
|
|
}
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
func (p *SingleConnPool) Close() error {
|
2019-08-03 17:21:12 +03:00
|
|
|
level := atomic.AddInt32(&p.level, -1)
|
|
|
|
if level > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
state := atomic.LoadUint32(&p.state)
|
|
|
|
if state == stateClosed {
|
|
|
|
return ErrClosed
|
|
|
|
}
|
|
|
|
if atomic.CompareAndSwapUint32(&p.state, state, stateClosed) {
|
|
|
|
close(p.ch)
|
|
|
|
cn, ok := <-p.ch
|
|
|
|
if ok {
|
|
|
|
p.freeConn(cn)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("pg: SingleConnPool.Close: infinite loop")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SingleConnPool) Reset() error {
|
2019-08-08 10:36:13 +03:00
|
|
|
if p.badConnError() == nil {
|
2019-08-03 17:21:12 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case cn, ok := <-p.ch:
|
|
|
|
if !ok {
|
|
|
|
return ErrClosed
|
|
|
|
}
|
2019-08-08 10:36:13 +03:00
|
|
|
p.pool.Remove(cn, ErrClosed)
|
2019-08-08 14:04:19 +03:00
|
|
|
p._badConnError.Store(BadConnError{wrapped: nil})
|
2019-08-03 17:21:12 +03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("pg: SingleConnPool does not have a Conn")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !atomic.CompareAndSwapUint32(&p.state, stateInited, stateDefault) {
|
|
|
|
state := atomic.LoadUint32(&p.state)
|
|
|
|
return fmt.Errorf("pg: invalid SingleConnPool state: %d", state)
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-03 17:21:12 +03:00
|
|
|
|
2019-08-08 10:36:13 +03:00
|
|
|
func (p *SingleConnPool) badConnError() error {
|
|
|
|
if v := p._badConnError.Load(); v != nil {
|
2019-08-08 14:04:19 +03:00
|
|
|
err := v.(BadConnError)
|
|
|
|
if err.wrapped != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-08 10:36:13 +03:00
|
|
|
}
|
|
|
|
return nil
|
2019-08-03 17:21:12 +03:00
|
|
|
}
|