Use a connection pool for Postgres
This commit is contained in:
parent
99cd70165e
commit
ffb19acef7
3 changed files with 15 additions and 6 deletions
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
|
|
@ -16,6 +16,7 @@
|
||||||
"congstar",
|
"congstar",
|
||||||
"datetime",
|
"datetime",
|
||||||
"dbname",
|
"dbname",
|
||||||
|
"dbpool",
|
||||||
"DBSTRING",
|
"DBSTRING",
|
||||||
"easybox",
|
"easybox",
|
||||||
"Eo's",
|
"Eo's",
|
||||||
|
|
@ -55,6 +56,7 @@
|
||||||
"PGPORT",
|
"PGPORT",
|
||||||
"pgtype",
|
"pgtype",
|
||||||
"PGUSER",
|
"PGUSER",
|
||||||
|
"pgxpool",
|
||||||
"Prés",
|
"Prés",
|
||||||
"pressly",
|
"pressly",
|
||||||
"psql",
|
"psql",
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -12,11 +12,13 @@ require (
|
||||||
github.com/andybalholm/cascadia v1.3.1 // indirect
|
github.com/andybalholm/cascadia v1.3.1 // indirect
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||||
|
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/mmcdole/goxpp v1.1.0 // indirect
|
github.com/mmcdole/goxpp v1.1.0 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
golang.org/x/crypto v0.17.0 // indirect
|
golang.org/x/crypto v0.17.0 // indirect
|
||||||
golang.org/x/net v0.10.0 // indirect
|
golang.org/x/net v0.10.0 // indirect
|
||||||
|
golang.org/x/sync v0.1.0 // indirect
|
||||||
golang.org/x/text v0.14.0 // indirect
|
golang.org/x/text v0.14.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
||||||
17
main.go
17
main.go
|
|
@ -7,12 +7,12 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/TheLovinator1/FeedVault/db"
|
"github.com/TheLovinator1/FeedVault/db"
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
conn *pgx.Conn
|
dbpool *pgxpool.Pool
|
||||||
DB *db.Queries
|
DB *db.Queries
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() { log.SetFlags(log.LstdFlags | log.Lshortfile) }
|
func init() { log.SetFlags(log.LstdFlags | log.Lshortfile) }
|
||||||
|
|
@ -21,16 +21,21 @@ func init() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// Open a database connection
|
// Open a database connection
|
||||||
conn, err := pgx.Connect(ctx, "postgresql://localhost/feedvault?user=feedvault&password=feedvault")
|
dbpool, err := pgxpool.New(ctx, "postgresql://localhost/feedvault?user=feedvault&password=feedvault")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("pgx.Connect(): %v", err)
|
log.Fatalf("pgx.Connect(): %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
DB = db.New(conn)
|
DB = db.New(dbpool)
|
||||||
|
|
||||||
|
// Test the connection
|
||||||
|
if err := dbpool.Ping(ctx); err != nil {
|
||||||
|
log.Fatalf("dbpool.Ping(): %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
defer conn.Close(context.Background())
|
defer dbpool.Close()
|
||||||
|
|
||||||
log.Print("Starting server")
|
log.Print("Starting server")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue