Skip to content
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

use port to probe db in playground #2296

Merged
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
18 changes: 5 additions & 13 deletions components/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ package main

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"os"
Expand Down Expand Up @@ -419,36 +419,28 @@ func populateDefaultOpt(flagSet *pflag.FlagSet) error {
return nil
}

func tryConnect(dsn string) error {
cli, err := sql.Open("mysql", dsn)
if err != nil {
return err
}
defer cli.Close()

conn, err := cli.Conn(context.Background())
func tryConnect(addr string, timeoutSec int) error {
conn, err := net.DialTimeout("tcp", addr, time.Duration(timeoutSec)*time.Second)
if err != nil {
return err
}
defer conn.Close()

return nil
}

// checkDB check if the addr is connectable by getting a connection from sql.DB. timeout <=0 means no timeout
func checkDB(dbAddr string, timeout int) bool {
dsn := fmt.Sprintf("root:@tcp(%s)/", dbAddr)
if timeout > 0 {
for i := 0; i < timeout; i++ {
if tryConnect(dsn) == nil {
if tryConnect(dbAddr, timeout) == nil {
return true
}
time.Sleep(time.Second)
}
return false
}
for {
if err := tryConnect(dsn); err == nil {
if err := tryConnect(dbAddr, timeout); err == nil {
return true
}
time.Sleep(time.Second)
Expand Down
Loading