Skip to content
Permalink
Browse files

Configure database connection

  • Loading branch information...
takonomura committed Sep 8, 2019
1 parent 430fa35 commit adca81f6ae380ec01847900edbffd84357f3aa05
Showing with 38 additions and 0 deletions.
  1. +7 −0 isucari/webapp/go/main.go
  2. +31 −0 isucari/webapp/go/mysql.go
@@ -324,6 +324,13 @@ func main() {
dbx = sqlx.NewDb(db, "mysql")
defer dbx.Close()

waitDB(db)
go pollDB(db)

db.SetConnMaxLifetime(10 * time.Second)
db.SetMaxIdleConns(512)
db.SetMaxOpenConns(512)

mux := goji.NewMux()

// API
@@ -0,0 +1,31 @@
package main

import (
"database/sql"
"log"
"time"
)

func waitDB(db *sql.DB) {
for {
err := db.Ping()
if err == nil {
return
}

log.Printf("Failed to ping DB: %s", err)
log.Println("Retrying...")
time.Sleep(time.Second)
}
}

func pollDB(db *sql.DB) {
for {
err := db.Ping()
if err != nil {
log.Printf("Failed to ping DB: %s", err)
}

time.Sleep(time.Second)
}
}

0 comments on commit adca81f

Please sign in to comment.
You can’t perform that action at this time.