add core lib
This commit is contained in:
68
dbpool/postgres.go
Normal file
68
dbpool/postgres.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package dbpool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type PoolConfig struct {
|
||||
MaxOpenConns int
|
||||
MinIdleConns int
|
||||
MaxConnLifetime time.Duration
|
||||
MaxConnIdleTime time.Duration
|
||||
HealthCheckPeriod time.Duration
|
||||
ConnectionAcquireWait time.Duration
|
||||
}
|
||||
|
||||
func NewPostgresPool(ctx context.Context, databaseURL string, pool PoolConfig) (*pgxpool.Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(databaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse postgres pool config: %w", err)
|
||||
}
|
||||
|
||||
if pool.MaxOpenConns <= 0 {
|
||||
pool.MaxOpenConns = 25
|
||||
}
|
||||
if pool.MinIdleConns < 0 {
|
||||
pool.MinIdleConns = 0
|
||||
}
|
||||
if pool.MinIdleConns > pool.MaxOpenConns {
|
||||
pool.MinIdleConns = pool.MaxOpenConns
|
||||
}
|
||||
if pool.MaxConnLifetime <= 0 {
|
||||
pool.MaxConnLifetime = 30 * time.Minute
|
||||
}
|
||||
if pool.MaxConnIdleTime <= 0 {
|
||||
pool.MaxConnIdleTime = 5 * time.Minute
|
||||
}
|
||||
if pool.HealthCheckPeriod <= 0 {
|
||||
pool.HealthCheckPeriod = time.Minute
|
||||
}
|
||||
if pool.ConnectionAcquireWait <= 0 {
|
||||
pool.ConnectionAcquireWait = 10 * time.Second
|
||||
}
|
||||
|
||||
cfg.MaxConns = int32(pool.MaxOpenConns)
|
||||
cfg.MinConns = int32(pool.MinIdleConns)
|
||||
cfg.MaxConnLifetime = pool.MaxConnLifetime
|
||||
cfg.MaxConnIdleTime = pool.MaxConnIdleTime
|
||||
cfg.HealthCheckPeriod = pool.HealthCheckPeriod
|
||||
|
||||
pingCtx, cancel := context.WithTimeout(ctx, pool.ConnectionAcquireWait)
|
||||
defer cancel()
|
||||
|
||||
poolConn, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open postgres pool: %w", err)
|
||||
}
|
||||
|
||||
if err := poolConn.Ping(pingCtx); err != nil {
|
||||
poolConn.Close()
|
||||
return nil, fmt.Errorf("ping postgres: %w", err)
|
||||
}
|
||||
|
||||
return poolConn, nil
|
||||
}
|
||||
26
dbpool/postgres_test.go
Normal file
26
dbpool/postgres_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package dbpool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewPostgresPool_ParseConfigError(t *testing.T) {
|
||||
_, err := NewPostgresPool(context.Background(), "://invalid-url", PoolConfig{})
|
||||
if err == nil || !strings.Contains(err.Error(), "parse postgres pool config") {
|
||||
t.Fatalf("expected parse config error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPostgresPool_PingError(t *testing.T) {
|
||||
_, err := NewPostgresPool(
|
||||
context.Background(),
|
||||
"postgres://postgres:postgres@127.0.0.1:1/appdb?sslmode=disable",
|
||||
PoolConfig{ConnectionAcquireWait: 20 * time.Millisecond},
|
||||
)
|
||||
if err == nil || !strings.Contains(err.Error(), "ping postgres") {
|
||||
t.Fatalf("expected ping error, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user