40 lines
956 B
Go
40 lines
956 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
_ "github.com/lib/pq"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
func connectDB() error {
|
||
|
con := os.Getenv("POSTGRES")
|
||
|
logger.Infof("opening connection to postgres: %s", con)
|
||
|
|
||
|
var err error
|
||
|
db, err = sql.Open("postgres", con)
|
||
|
if err != nil {
|
||
|
logger.Errorf("cannot open connection to postgres: %v", err)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
statement := `
|
||
|
CREATE TABLE IF NOT EXISTS migrations (
|
||
|
filename TEXT NOT NULL UNIQUE
|
||
|
);
|
||
|
`
|
||
|
_, err = db.Exec(statement)
|
||
|
if err != nil {
|
||
|
logger.Errorf("cannot create migrations table: %v", err)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// At most 1000 database connections will be left open in the idle state. This
|
||
|
// was found to be important when benchmarking with `wrk`: if this was unset,
|
||
|
// too many open idle connections were present, resulting in dropped requests
|
||
|
// due to the limit on the number of file handles. On benchmarking, around
|
||
|
// 100 was found to be pretty optimal.
|
||
|
db.SetMaxIdleConns(100)
|
||
|
|
||
|
return nil
|
||
|
}
|