33 lines
695 B
Go
33 lines
695 B
Go
|
|
package postgres
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
|
|
||
|
|
"github.com/geo-platform/tenant-api/internal/shared/config"
|
||
|
|
)
|
||
|
|
|
||
|
|
func NewPool(ctx context.Context, cfg config.DatabaseConfig) (*pgxpool.Pool, error) {
|
||
|
|
poolCfg, err := pgxpool.ParseConfig(cfg.DSN())
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("parse pool config: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
poolCfg.MaxConns = int32(cfg.MaxOpenConns)
|
||
|
|
poolCfg.MinConns = int32(cfg.MaxIdleConns)
|
||
|
|
|
||
|
|
pool, err := pgxpool.NewWithConfig(ctx, poolCfg)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("create pool: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := pool.Ping(ctx); err != nil {
|
||
|
|
pool.Close()
|
||
|
|
return nil, fmt.Errorf("ping postgres: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return pool, nil
|
||
|
|
}
|