Skip to content
Prev Previous commit
Next Next commit
format
  • Loading branch information
Georgi Dimitrov committed Dec 2, 2025
commit 4617a05e1336b72d5961e194d4cc1173d1beb246
65 changes: 32 additions & 33 deletions retriever/postgresqlretriever/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,42 @@
)

var (
pool *pgxpool.Pool
mu sync.Mutex
refCount int
pool *pgxpool.Pool
mu sync.Mutex
refCount int
)

func GetPool(ctx context.Context, uri string) (*pgxpool.Pool, error) {

Check warning on line 16 in retriever/postgresqlretriever/database.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the 'Get' prefix from this function name.

See more on https://sonarcloud.io/project/issues?id=thomaspoignant_go-feature-flag&issues=AZrf8MN0sn8GrSqJf98R&open=AZrf8MN0sn8GrSqJf98R&pullRequest=4393
mu.Lock()
defer mu.Unlock()

if pool == nil {
p, err := pgxpool.New(ctx, uri)
if err != nil {
return nil, err
}
if err := p.Ping(ctx); err != nil {
p.Close()
return nil, err
}

pool = p
}

refCount++
return pool, nil
mu.Lock()
defer mu.Unlock()

if pool == nil {
p, err := pgxpool.New(ctx, uri)
if err != nil {
return nil, err
}
if err := p.Ping(ctx); err != nil {
p.Close()
return nil, err
}

pool = p
}

refCount++
return pool, nil
}

func ReleasePool() {
mu.Lock()
defer mu.Unlock()

refCount--
if refCount <= 0 {
if pool != nil {
pool.Close()
pool = nil
}
refCount = 0
}
mu.Lock()
defer mu.Unlock()

refCount--
if refCount <= 0 {
if pool != nil {
pool.Close()
pool = nil
}
refCount = 0
}
}

Loading