35 lines
728 B
Go
35 lines
728 B
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func ApplyPoolFromEnv(d *sql.DB, prefix string) {
|
|
if d == nil {
|
|
return
|
|
}
|
|
if v := os.Getenv(prefix + "MAX_OPEN_CONNS"); v != "" {
|
|
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
|
d.SetMaxOpenConns(n)
|
|
}
|
|
}
|
|
if v := os.Getenv(prefix + "MAX_IDLE_CONNS"); v != "" {
|
|
if n, err := strconv.Atoi(v); err == nil && n >= 0 {
|
|
d.SetMaxIdleConns(n)
|
|
}
|
|
}
|
|
if v := os.Getenv(prefix + "CONN_MAX_LIFETIME"); v != "" {
|
|
if dur, err := time.ParseDuration(v); err == nil {
|
|
d.SetConnMaxLifetime(dur)
|
|
}
|
|
}
|
|
if v := os.Getenv(prefix + "CONN_MAX_IDLE_TIME"); v != "" {
|
|
if dur, err := time.ParseDuration(v); err == nil {
|
|
d.SetConnMaxIdleTime(dur)
|
|
}
|
|
}
|
|
}
|