Skip to content

Commit

Permalink
[ENH]: use pgpool for log services (#2079)
Browse files Browse the repository at this point in the history
## Description of changes
pg.Connect is returning only one connection, which is not thread safe
and cause an error. When running in grpc, each request run on a
different go routine, and in parallel. pgpool will create and manage a
set of connection.
*Summarize the changes made by this PR.*
 - Improvements & Bug fixes
	 - Use pg pool to use threadsafe connection.
  • Loading branch information
nicolasgere authored Apr 29, 2024
1 parent f42567e commit 81d97dd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions go/pkg/log/repository/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"errors"
log "github.com/chroma-core/chroma/go/database/log/db"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"time"
)

type LogRepository struct {
conn *pgx.Conn
conn *pgxpool.Pool
queries *log.Queries
}

Expand Down Expand Up @@ -95,7 +96,7 @@ func (r *LogRepository) PurgeRecords(ctx context.Context) (err error) {
return
}

func NewLogRepository(conn *pgx.Conn) *LogRepository {
func NewLogRepository(conn *pgxpool.Pool) *LogRepository {
return &LogRepository{
conn: conn,
queries: log.New(conn),
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/log/server/property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/chroma-core/chroma/go/pkg/proto/logservicepb"
"github.com/chroma-core/chroma/go/pkg/types"
libs2 "github.com/chroma-core/chroma/go/shared/libs"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"pgregory.net/rapid"
Expand Down Expand Up @@ -37,7 +37,7 @@ func (suite *LogServerTestSuite) SetupSuite() {
connectionString, err := libs2.StartPgContainer(ctx)
config.DATABASE_URL = connectionString
assert.NoError(suite.t, err, "Failed to start pg container")
var conn *pgx.Conn
var conn *pgxpool.Pool
conn, err = libs2.NewPgConnection(ctx, config)
assert.NoError(suite.t, err, "Failed to create new pg connection")
err = libs2.RunMigration(ctx, connectionString)
Expand Down
6 changes: 3 additions & 3 deletions go/shared/libs/db_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package libs
import (
"context"
"github.com/chroma-core/chroma/go/pkg/log/configuration"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)

func NewPgConnection(ctx context.Context, config *configuration.LogServiceConfiguration) (conn *pgx.Conn, err error) {
conn, err = pgx.Connect(ctx, config.DATABASE_URL)
func NewPgConnection(ctx context.Context, config *configuration.LogServiceConfiguration) (conn *pgxpool.Pool, err error) {
conn, err = pgxpool.New(ctx, config.DATABASE_URL)
return
}

0 comments on commit 81d97dd

Please sign in to comment.