Skip to content

Commit

Permalink
1. add PoolConn and Pool interface
Browse files Browse the repository at this point in the history
  • Loading branch information
romberli committed Jan 21, 2021
1 parent 6690d91 commit 91e261d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion middleware/mysql/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/romberli/log"

"github.com/romberli/go-util/constant"
"github.com/romberli/go-util/middleware"
)

const (
Expand All @@ -22,6 +23,9 @@ const (
DefaultSleepTime = 1 // Seconds
)

var _ middleware.PoolConn = (*PoolConn)(nil)
var _ middleware.Pool = (*Pool)(nil)

type PoolConfig struct {
Config
MaxConnections int
Expand Down Expand Up @@ -150,6 +154,10 @@ func (pc *PoolConn) IsValid() bool {
return pc.CheckInstanceStatus()
}

func (pc *PoolConn) Execute(command string, args ...interface{}) (interface{}, error) {
return pc.Conn.Execute(command, args...)
}

type Pool struct {
sync.Mutex
PoolConfig
Expand Down Expand Up @@ -281,7 +289,7 @@ func (p *Pool) addToFreeChan(pc *PoolConn) {
}

// Get is an exported alias of get() function with routine safe
func (p *Pool) Get() (*PoolConn, error) {
func (p *Pool) Get() (middleware.PoolConn, error) {
p.Lock()
defer p.Unlock()

Expand Down Expand Up @@ -378,6 +386,8 @@ func (p *Pool) maintainFreeChan() {
}
}

// keepAlive keeps alive given number of connections in the pool to avoid disconnected by server side automatically,
// it also checks if connections are still valid
func (p *Pool) keepAlive(num int) error {
if len(p.freeConnChan) == 0 {
return nil
Expand Down
16 changes: 16 additions & 0 deletions middleware/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package middleware

type PoolConn interface {
Close() error
DisConnect() error
IsValid() bool
Execute(command string, args ...interface{}) (interface{}, error)
}

type Pool interface {
Close() error
IsClosed() bool
Get() (PoolConn, error)
Supply(num int) error
Release(num int) error
}

0 comments on commit 91e261d

Please sign in to comment.