Skip to content

Commit

Permalink
bug: Backend Postgres parallel access (#43)
Browse files Browse the repository at this point in the history
* Fix bug with concurrent access to postgres by switching to a pool

* Go fmt
  • Loading branch information
b-j-roberts authored Apr 16, 2024
1 parent d21aa1c commit 3795e97
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion backend/core/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

type Backend struct {
Databases *Databases
Databases *Databases
// TODO: Is this thread safe?
WSConnections []*websocket.Conn

CanvasConfig *config.CanvasConfig
Expand Down
11 changes: 6 additions & 5 deletions backend/core/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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()
}
2 changes: 2 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions backend/routes/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3795e97

Please sign in to comment.