Skip to content

Commit

Permalink
fix(mysql): mysql-monitor db-up增加重连 #3506
Browse files Browse the repository at this point in the history
  • Loading branch information
xfwduke authored and zhangzhw8 committed Mar 11, 2024
1 parent a294bfa commit 9e2b9eb
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func connectDB(ip string, port int, ca *config.ConnectAuth) (*sqlx.DB, error) {
ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout)
defer cancel()

return sqlx.ConnectContext(
db, err := sqlx.ConnectContext(
ctx,
"mysql", fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?parseTime=true&loc=%s&timeout=%s",
Expand All @@ -169,4 +169,21 @@ func connectDB(ip string, port int, ca *config.ConnectAuth) (*sqlx.DB, error) {
config.MonitorConfig.InteractTimeout,
),
)
if err != nil {
slog.Warn("first time connect failed", slog.String("error", err.Error()))
slog.Info("retry connect after 3 seconds")
time.Sleep(3 * time.Second)
return sqlx.ConnectContext(
ctx,
"mysql", fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?parseTime=true&loc=%s&timeout=%s",
ca.User, ca.Password, ip, port,
"",
time.Local.String(),
config.MonitorConfig.InteractTimeout,
),
)
}

return db, nil
}

0 comments on commit 9e2b9eb

Please sign in to comment.