Home > Blockchain >  Google Cloud SQL Go Gin connection timeout with 200 concurrent
Google Cloud SQL Go Gin connection timeout with 200 concurrent

Time:03-10

I was trying to do load testing for the production. I see a lot of errors get from Google Cloud SQL "connection timeout". Firstly, I was trying to increases the Google SQL instance to higher, but the errors still happened.

FYI, I run an application connected to the database via proxy.

Anyone, please suggest to me how to fix this. I am OK to know it is impossible in the real world.

EDIT: Add example code

db.go

func DB() *pgxpool.Pool {

schema := os.Getenv("DB_SCHEMA")
port := os.Getenv("DB_PORT")
user := os.Getenv("DB_USERNAME")
password := os.Getenv("DB_PASSWORD")
host := os.Getenv("DB_HOST")
dbName := os.Getenv("DB_DATABASE")
sslMode := os.Getenv("DB_SSL_MODE")
sslCertificate := os.Getenv("DB_SSL_CERTIFICATE")
sslPrivateKey := os.Getenv("DB_SSL_PRIVATE_KEY")
sslRootCert := os.Getenv("DB_SSL_ROOT_CA")

connStr := fmt.Sprintf(
    "host=%s port=%s user=%s password=%s dbname=%s sslmode=%s search_path=%s sslcert=%s sslkey=%s sslrootcert=%s",
    host,
    port,
    user,
    password,
    dbName,
    sslMode,
    schema,
    sslCertificate,
    sslPrivateKey,
    sslRootCert,
)

db, err := pgxpool.Connect(context.Background(), connStr)
helpers.CheckErr(err)

return db

}

data_repository.go

func GetDataByPhone(phone string) (model.Data, error) {
db := db.DB()
defer db.Close()

var d model.Data
var strQuery = "SELECT phone, payload, updated_at "  
    "FROM example_db.public.data WHERE phone=$1"

db.QueryRow(
    context.Background(),
    strQuery,
    phone,
).Scan(
    &d.Phone,
    &d.Payload,
    &d.UpdatedAt,
)

return d, nil

}

CodePudding user response:

It looks like you are not closing your connections properly.

You can have a look here about how you should open and close connections properly. Specifically in Go, it should be something like that:

   sqlInsert := "INSERT INTO votes(candidate, created_at, updated_at) VALUES(?, NOW(), NOW())"

    if team == "TABS" || team == "SPACES" {

    if _, err := app.db.Exec(sqlInsert, team); err != nil {
            fmt.Fprintf(w, "unable to save vote: %s", err)
            return fmt.Errorf("DB.Exec: %v", err)
    }
    fmt.Fprintf(w, "Vote successfully cast for %s!\n", team)}
    return nil

You can also set a connection timeout limit with:

   // Set Maximum time (in seconds) that a connection can remain open.
   db.SetConnMaxLifetime(1800 * time.Second)`
  • Related