Skip to content

make sure mysql is healthy when start proxy #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ import (
"github.com/go-mysql-org/go-mysql/client"
)

pool := client.NewPool(log.Debugf, 100, 400, 5, "127.0.0.1:3306", `root`, ``, `test`)
pool, err := client.NewPool(log.Debugf, 100, 400, 5, "127.0.0.1:3306", `root`, ``, `test`)
if err != nil {
return
}
// ...
conn, _ := pool.GetConn(ctx)
defer pool.PutConn(conn)
Expand Down
33 changes: 22 additions & 11 deletions client/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
Pool for efficient reuse of connections.

Usage:
pool := client.NewPool(log.Debugf, 100, 400, 5, `127.0.0.1:3306`, `username`, `userpwd`, `dbname`)
pool, err := client.NewPool(log.Debugf, 100, 400, 5, `127.0.0.1:3306`, `username`, `userpwd`, `dbname`)
if err != nil {
return
}
...
conn, _ := pool.GetConn(ctx)
defer pool.PutConn(conn)
Expand Down Expand Up @@ -89,7 +92,7 @@ func NewPool(
password string,
dbName string,
options ...func(conn *Conn),
) *Pool {
) (*Pool, error) {
if minAlive > maxAlive {
minAlive = maxAlive
}
Expand Down Expand Up @@ -122,12 +125,14 @@ func NewPool(

if pool.minAlive > 0 {
pool.logFunc(`Pool: Setup %d new connections (minimal pool size)...`, pool.minAlive)
pool.startNewConnections(pool.minAlive)
if err := pool.startNewConnections(pool.minAlive); err != nil {
return nil, err
}
}

go pool.closeOldIdleConnections()

return pool
return pool, nil
}

func (pool *Pool) GetStats(stats *ConnectionStats) {
Expand Down Expand Up @@ -389,7 +394,10 @@ func (pool *Pool) spawnConnectionsIfNeeded() bool {
}

pool.logFunc(`Pool: Setup %d new connections (total: %d idle: %d)...`, needSpanNew, totalCount, idleCount)
pool.startNewConnections(needSpanNew)
if err := pool.startNewConnections(needSpanNew); err != nil {
pool.logFunc(`Pool: Setup startNewConnections failed,err : %v`, err)
return false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we should return true? because for false caller will try to close redundant connections, but if we go here we are lack of connections.

}

return true
}
Expand Down Expand Up @@ -446,22 +454,25 @@ func (pool *Pool) closeConn(conn *Conn) {
_ = conn.Close() // Closing is not an instant action, so do it outside the lock
}

func (pool *Pool) startNewConnections(count int) {
func (pool *Pool) startNewConnections(count int) error {
connections := make([]Connection, 0, count)
for i := 0; i < count; i++ {
if conn, err := pool.createNewConnection(); err == nil {
pool.synchro.Lock()
pool.synchro.stats.TotalCount++
pool.synchro.Unlock()
connections = append(connections, conn)
conn, err := pool.createNewConnection()
if err != nil {
return err
}
pool.synchro.Lock()
pool.synchro.stats.TotalCount++
pool.synchro.Unlock()
connections = append(connections, conn)
}

pool.synchro.Lock()
for _, connection := range connections {
pool.putConnectionUnsafe(connection)
}
pool.synchro.Unlock()
return nil
}

func (pool *Pool) ping(conn *Conn) error {
Expand Down