Skip to content

Commit

Permalink
Merge pull request #191 from funnyAnt/master
Browse files Browse the repository at this point in the history
#189 连接池相关优化
  • Loading branch information
xiyangxixian authored Dec 30, 2021
2 parents f49351a + 172a8dc commit dd6678d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
16 changes: 13 additions & 3 deletions backend/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

const (
getConnTimeout = 2 * time.Second
PING_PEROID = 5 * time.Second
)

var (
Expand Down Expand Up @@ -114,7 +115,7 @@ func (cp *connectionPoolImpl) tryReuse(pc *pooledConnectImpl) error {
}

// Get return a connection, you should call PooledConnect's Recycle once done
func (cp *connectionPoolImpl) Get(ctx context.Context) (PooledConnect, error) {
func (cp *connectionPoolImpl) Get(ctx context.Context) (pc PooledConnect, err error) {
p := cp.pool()
if p == nil {
return nil, ErrConnectionPoolClosed
Expand All @@ -126,7 +127,16 @@ func (cp *connectionPoolImpl) Get(ctx context.Context) (PooledConnect, error) {
if err != nil {
return nil, err
}
return r.(*pooledConnectImpl), nil

pc = r.(*pooledConnectImpl)

//do ping when over the ping time. if error happen, create new one
if !pc.GetReturnTime().IsZero() && time.Until(pc.GetReturnTime().Add(PING_PEROID)) < 0 {
if err = pc.Ping(); err != nil {
err = pc.Reconnect()
}
}
return pc, err
}

// Put recycle a connection into the pool
Expand Down Expand Up @@ -258,4 +268,4 @@ func (cp *connectionPoolImpl) IdleClosed() int64 {
return 0
}
return p.IdleClosed()
}
}
2 changes: 2 additions & 0 deletions backend/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ type PooledConnect interface {
Begin() error
Commit() error
Rollback() error
Ping() error
SetCharset(charset string, collation mysql.CollationID) (bool, error)
FieldList(table string, wildcard string) ([]*mysql.Field, error)
GetAddr() string
SetSessionVariables(frontend *mysql.SessionVariables) (bool, error)
WriteSetStatement() error
GetConnectionID() int64
GetReturnTime() time.Time
}

type ConnectionPool interface {
Expand Down
19 changes: 19 additions & 0 deletions backend/mocks/PooledConnect.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions backend/pooled_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,30 @@
package backend

import (
"time"

"github.com/XiaoMi/Gaea/mysql"
)

// PooledConnect app use this object to exec sql
type pooledConnectImpl struct {
directConnection *DirectConnection
pool *connectionPoolImpl
returnTime time.Time
}

// Recycle return PooledConnect to the pool
func (pc *pooledConnectImpl) Recycle() {
//if has error,the connection can’t be recycled
if pc.directConnection.pkgErr != nil {
pc.Close()
}

if pc.IsClosed() {
pc.pool.Put(nil)
} else {
pc.pool.Put(pc)
pc.returnTime = time.Now()
}
}

Expand Down Expand Up @@ -63,6 +72,9 @@ func (pc *pooledConnectImpl) UseDB(db string) error {
return pc.directConnection.UseDB(db)
}

func (pc *pooledConnectImpl) Ping() error {
return pc.directConnection.Ping()
}
// Execute wrapper of direct connection, execute sql
func (pc *pooledConnectImpl) Execute(sql string, maxRows int) (*mysql.Result, error) {
return pc.directConnection.Execute(sql, maxRows)
Expand Down Expand Up @@ -116,3 +128,7 @@ func (pc *pooledConnectImpl) WriteSetStatement() error {
func (pc *pooledConnectImpl) GetConnectionID() int64 {
return int64(pc.directConnection.conn.ConnectionID)
}

func (pc *pooledConnectImpl) GetReturnTime() time.Time {
return pc.returnTime
}

0 comments on commit dd6678d

Please sign in to comment.