-
-
Save methane/2d3ea01bfa88bfa275514146791817db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"database/sql" | |
_ "github.com/go-sql-driver/mysql" | |
"log" | |
"sync" | |
"time" | |
) | |
func main() { | |
db, _ := sql.Open("mysql", "root@tcp(127.0.0.1:3306)/") | |
var waitTimeout int | |
err := db.QueryRow("SELECT @@wait_timeout").Scan(&waitTimeout) | |
if err != nil { | |
panic(err) | |
} | |
log.Printf("wait_timeout=%v", waitTimeout) | |
var wg sync.WaitGroup | |
for i := 60; i < 3600; i += 30 { | |
c, err := db.Conn(context.Background()) | |
if err != nil { | |
log.Print(err) | |
break | |
} | |
wg.Add(1) | |
go func(c *sql.Conn, n int) { | |
time.Sleep(time.Second * time.Duration(n)) | |
_, err := c.ExecContext(context.Background(), "SELECT 42") | |
log.Printf("%v - %v", n, err) | |
c.Close() | |
wg.Done() | |
}(c, i) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment