Skip to content

Commit

Permalink
use pgx Pool to prevent connection parallel execution
Browse files Browse the repository at this point in the history
Signed-off-by: Rajiv Harlalka <[email protected]>
  • Loading branch information
rajivharlalka committed Sep 9, 2024
1 parent d7ca91e commit 6536685
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 2 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ require (
require (
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
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

Expand Down
10 changes: 5 additions & 5 deletions backend/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"log"
"sync"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/metakgp/iqps/backend/pkg/config"
)

var (
database *pgx.Conn
database *pgxpool.Pool
mu sync.Mutex
)

Expand All @@ -34,12 +34,12 @@ create index IF NOT EXISTS idx_course_name_trgm on iqps using gin (course_name g
`

func InitDB() *pgx.Conn {
func InitDB() *pgxpool.Pool {
var err error
dbConfig := config.Get().DB
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", dbConfig.Host, dbConfig.Port, dbConfig.Username, dbConfig.Password, dbConfig.DBname)
// database, err = sql.Open("postgres", psqlconn)
database, err = pgx.Connect(context.Background(), psqlconn)
database, err = pgxpool.New(context.Background(), psqlconn)
if err != nil {
panic("Invalid Database connection string")
}
Expand All @@ -58,7 +58,7 @@ func InitDB() *pgx.Conn {
return database
}

func GetDB() *pgx.Conn {
func GetDB() *pgxpool.Pool {
if database == nil {
mu.Lock()
defer mu.Unlock()
Expand Down
3 changes: 3 additions & 0 deletions backend/query/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ with fuzzy as (
from iqps
where (course_code || ' ' || course_name) %>> @query_text
order by rank_ix
limit 30
),
full_text as (
select
Expand All @@ -77,6 +78,7 @@ full_text as (
fts_course_details @@ websearch_to_tsquery(@query_text)
AND approve_status = true
order by rank_ix
limit 30
),
partial_search as (
select id,
Expand All @@ -88,6 +90,7 @@ partial_search as (
websearch_to_tsquery('simple', @query_text)::text || ':*'
)
AND approve_status = true
limit 30
), result as (
select
iqps.id,iqps.course_code, iqps.course_name, iqps.year, iqps.exam, iqps.filelink, iqps.from_library, iqps.upload_timestamp, iqps.approve_status
Expand Down

0 comments on commit 6536685

Please sign in to comment.