MCPcopy Index your code
hub / github.com/PatchMon/PatchMon / NewPool

Function NewPool

server-source-code/internal/database/database.go:20–42  ·  view source on GitHub ↗

NewPool creates a connection pool with retry logic.

(ctx context.Context, cfg *config.Config)

Source from the content-addressed store, hash-verified

18
19// NewPool creates a connection pool with retry logic.
20func 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.
45func (p *Pool) waitForDB(ctx context.Context) error {

Callers

nothing calls this directly

Calls 3

waitForDBMethod · 0.95
setUTCTimeZoneFunction · 0.85
CloseMethod · 0.45

Tested by

no test coverage detected