NewPool creates a connection pool with retry logic.
(ctx context.Context, cfg *config.Config)
| 18 | |
| 19 | // NewPool creates a connection pool with retry logic. |
| 20 | func NewPool(ctx context.Context, cfg *config.Config) (*Pool, error) { |
| 21 | poolCfg, err := pgxpool.ParseConfig(cfg.DatabaseURL) |
| 22 | if err != nil { |
| 23 | return nil, fmt.Errorf("parse database url: %w", err) |
| 24 | } |
| 25 | |
| 26 | poolCfg.MaxConns = safeconv.ClampToInt32(cfg.DBConnectionLimit) |
| 27 | poolCfg.ConnConfig.ConnectTimeout = time.Duration(cfg.DBConnectTimeout) * time.Second |
| 28 | setUTCTimeZone(poolCfg) |
| 29 | |
| 30 | pool, err := pgxpool.NewWithConfig(ctx, poolCfg) |
| 31 | if err != nil { |
| 32 | return nil, fmt.Errorf("create pool: %w", err) |
| 33 | } |
| 34 | |
| 35 | p := &Pool{Pool: pool, cfg: cfg} |
| 36 | if err := p.waitForDB(ctx); err != nil { |
| 37 | pool.Close() |
| 38 | return nil, err |
| 39 | } |
| 40 | |
| 41 | return p, nil |
| 42 | } |
| 43 | |
| 44 | // waitForDB retries connection until DB is available. |
| 45 | func (p *Pool) waitForDB(ctx context.Context) error { |
nothing calls this directly
no test coverage detected