Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Backend Postgres parallel access #43

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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