From 3795e979ab0852ae3f6cccddc308060862a2289b Mon Sep 17 00:00:00 2001 From: Brandon R <54774639+b-j-roberts@users.noreply.github.com> Date: Mon, 15 Apr 2024 21:26:01 -0500 Subject: [PATCH] bug: Backend Postgres parallel access (#43) * Fix bug with concurrent access to postgres by switching to a pool * Go fmt --- backend/core/backend.go | 3 ++- backend/core/databases.go | 11 ++++++----- backend/go.mod | 2 ++ backend/routes/indexer.go | 1 + 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/backend/core/backend.go b/backend/core/backend.go index 9625ded8..d0072b9f 100644 --- a/backend/core/backend.go +++ b/backend/core/backend.go @@ -10,7 +10,8 @@ import ( ) type Backend struct { - Databases *Databases + Databases *Databases + // TODO: Is this thread safe? WSConnections []*websocket.Conn CanvasConfig *config.CanvasConfig diff --git a/backend/core/databases.go b/backend/core/databases.go index a53d59b0..5f248dea 100644 --- a/backend/core/databases.go +++ b/backend/core/databases.go @@ -5,7 +5,7 @@ import ( "os" "strconv" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/redis/go-redis/v9" "github.com/keep-starknet-strange/art-peace/backend/config" @@ -15,7 +15,7 @@ type Databases struct { DatabaseConfig *config.DatabaseConfig Redis *redis.Client - Postgres *pgx.Conn + Postgres *pgxpool.Pool } func NewDatabases(databaseConfig *config.DatabaseConfig) *Databases { @@ -31,16 +31,17 @@ func NewDatabases(databaseConfig *config.DatabaseConfig) *Databases { // Connect to Postgres postgresConnString := "postgresql://" + databaseConfig.Postgres.User + ":" + os.Getenv("POSTGRES_PASSWORD") + "@" + databaseConfig.Postgres.Host + ":" + strconv.Itoa(databaseConfig.Postgres.Port) + "/" + databaseConfig.Postgres.Database - pgConn, err := pgx.Connect(context.Background(), postgresConnString) + // TODO: crd_audit?sslmode=disable + pgPool, err := pgxpool.New(context.Background(), postgresConnString) if err != nil { panic(err) } - d.Postgres = pgConn + d.Postgres = pgPool return d } func (d *Databases) Close() { d.Redis.Close() - d.Postgres.Close(context.Background()) + d.Postgres.Close() } diff --git a/backend/go.mod b/backend/go.mod index b25549f6..475dc2cc 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -20,10 +20,12 @@ require ( github.com/fxamacker/cbor/v2 v2.5.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/x448/float16 v0.8.4 // indirect golang.org/x/crypto v0.18.0 // indirect golang.org/x/net v0.20.0 // indirect + golang.org/x/sync v0.6.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect rsc.io/tmplfunc v0.0.3 // indirect diff --git a/backend/routes/indexer.go b/backend/routes/indexer.go index a137412f..5012075a 100644 --- a/backend/routes/indexer.go +++ b/backend/routes/indexer.go @@ -56,6 +56,7 @@ func InitIndexerRoutes() { */ // TODO: User might miss some messages between loading canvas and connecting to websocket? +// TODO: Check thread safety of these things func consumeIndexerMsg(w http.ResponseWriter, r *http.Request) { requestBody, err := io.ReadAll(r.Body) if err != nil {