31 lines
750 B
Go
31 lines
750 B
Go
package platform
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func OpenDatabase(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
|
|
poolConfig, err := pgxpool.ParseConfig(databaseURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse Bell database URL: %w", err)
|
|
}
|
|
poolConfig.MaxConns = 8
|
|
poolConfig.MinConns = 1
|
|
poolConfig.MaxConnLifetime = 30 * time.Minute
|
|
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open Bell database: %w", err)
|
|
}
|
|
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
if err := pool.Ping(pingCtx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("ping Bell database: %w", err)
|
|
}
|
|
return pool, nil
|
|
}
|