Skip to content
Prev Previous commit
Next Next commit
fix imports
  • Loading branch information
Georgi Dimitrov committed Dec 2, 2025
commit a0082b1f648eda8b61b9733bd4fa5b1d4918a48e
24 changes: 12 additions & 12 deletions retriever/postgresqlretriever/database.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
package postgresqlretriever

import (
"context"
"fmt"
"sync"
"context"

Check failure on line 4 in retriever/postgresqlretriever/database.go

View workflow job for this annotation

GitHub Actions / Lint

File is not properly formatted (gci)
"fmt"
"sync"

"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/pgxpool"
)

var poolInstance *pgxpool.Pool
var once sync.Once
var poolErr error
var errPool error

// refCount tracks how many retrievers are currently using the pool.
var refCount int
var refCountMutex sync.Mutex

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

Check warning on line 19 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
// The sync.Once ensures that the inner function is executed only once,
// even if called by multiple goroutines concurrently.
once.Do(func() {
poolInstance, poolErr = pgxpool.New(ctx, uri)
if poolErr != nil {
poolErr = fmt.Errorf("failed to create connection pool: %w", poolErr)
poolInstance, errPool = pgxpool.New(ctx, uri)
if errPool != nil {
errPool = fmt.Errorf("failed to create connection pool: %w", errPool)
return
}

// Check connection immediately
if err := poolInstance.Ping(ctx); err != nil {
poolErr = fmt.Errorf("failed to ping database with new pool: %w", err)
errPool = fmt.Errorf("failed to ping database with new pool: %w", err)
// Don't close here, the pool remains valid for a retry connection
}
})

if poolErr != nil {
return nil, poolErr
if errPool != nil {
return nil, errPool
}

refCountMutex.Lock()
Expand All @@ -58,6 +58,6 @@
poolInstance = nil
// Reset sync.Once to allow re-initialization if needed
once = sync.Once{}
poolErr = nil
errPool = nil
}
}
20 changes: 11 additions & 9 deletions retriever/postgresqlretriever/retriever.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package postgresqlretriever

import (
"context"
"encoding/json"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/thomaspoignant/go-feature-flag/retriever"
"github.com/thomaspoignant/go-feature-flag/utils"
"github.com/thomaspoignant/go-feature-flag/utils/fflog"
"log/slog"
"context"

Check failure on line 4 in retriever/postgresqlretriever/retriever.go

View workflow job for this annotation

GitHub Actions / Lint

File is not properly formatted (gci)
"encoding/json"
"fmt"
"log/slog"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"

"github.com/thomaspoignant/go-feature-flag/retriever"
"github.com/thomaspoignant/go-feature-flag/utils"
"github.com/thomaspoignant/go-feature-flag/utils/fflog"
)

var defaultColumns = map[string]string{
Expand Down Expand Up @@ -66,7 +68,7 @@
}

// Shutdown closes the database connection.
func (r *Retriever) Shutdown(ctx context.Context) error {

Check warning on line 71 in retriever/postgresqlretriever/retriever.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unused parameter 'ctx' should be removed.

See more on https://sonarcloud.io/project/issues?id=thomaspoignant_go-feature-flag&issues=AZrf8MI6sn8GrSqJf98Q&open=AZrf8MI6sn8GrSqJf98Q&pullRequest=4393
ReleasePool()
return nil
}
Expand Down
Loading