fix(database): correct metrics and improve error handling
- Fix metrics double-counting: track deltas for WaitCount/WaitDuration instead of adding cumulative values each tick - Replace fmt.Printf with structured logging in pool monitor - Add PoolOptions validation (MaxConns > 0, MinConns >= 0) - Warn when DATABASE_URI overrides non-default PoolOptions - Improve findAndParseConfig to report all tried files and errors - Remove dead code in pgdb/config.go (unreachable host default) - Fix errcheck lint issues for file.Close() calls - Add context parameter to OpenDBMonitor() (breaking change)
This commit is contained in:
@@ -2,12 +2,14 @@ package pgdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.ntppool.org/common/database"
|
||||
"go.ntppool.org/common/logger"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -61,6 +63,19 @@ func DefaultPoolOptions() PoolOptions {
|
||||
// can be specified in the URI query string and PoolOptions are ignored.
|
||||
// When using config files, PoolOptions are applied.
|
||||
func OpenPool(ctx context.Context, options PoolOptions) (*pgxpool.Pool, error) {
|
||||
log := logger.FromContext(ctx)
|
||||
|
||||
// Validate PoolOptions
|
||||
if options.MaxConns <= 0 {
|
||||
return nil, fmt.Errorf("pgdb: MaxConns must be positive, got: %d", options.MaxConns)
|
||||
}
|
||||
if options.MinConns < 0 {
|
||||
return nil, fmt.Errorf("pgdb: MinConns must be non-negative, got: %d", options.MinConns)
|
||||
}
|
||||
if options.MinConns > options.MaxConns {
|
||||
return nil, fmt.Errorf("pgdb: MinConns (%d) cannot exceed MaxConns (%d)", options.MinConns, options.MaxConns)
|
||||
}
|
||||
|
||||
var poolConfig *pgxpool.Config
|
||||
var err error
|
||||
|
||||
@@ -70,7 +85,15 @@ func OpenPool(ctx context.Context, options PoolOptions) (*pgxpool.Pool, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse DATABASE_URI: %w", err)
|
||||
}
|
||||
// Pool settings from URI are used; PoolOptions ignored
|
||||
|
||||
// Log when PoolOptions differ from defaults (they will be ignored)
|
||||
defaults := DefaultPoolOptions()
|
||||
if options.MaxConns != defaults.MaxConns || options.MinConns != defaults.MinConns {
|
||||
log.WarnContext(ctx, "DATABASE_URI is set; PoolOptions are ignored (use URI query parameters for pool settings)",
|
||||
"ignored_max_conns", options.MaxConns,
|
||||
"ignored_min_conns", options.MinConns,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// Fall back to config file approach
|
||||
pgCfg, err := findAndParseConfig(options.ConfigFiles)
|
||||
@@ -99,7 +122,7 @@ func OpenPool(ctx context.Context, options PoolOptions) (*pgxpool.Pool, error) {
|
||||
|
||||
// Test the connection
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
pool.Close() // pgxpool.Pool.Close() doesn't return an error
|
||||
return nil, fmt.Errorf("failed to ping database: %w", err)
|
||||
}
|
||||
|
||||
@@ -116,35 +139,33 @@ func OpenPoolWithConfigFile(ctx context.Context, configFile string) (*pgxpool.Po
|
||||
|
||||
// findAndParseConfig searches for and parses the first existing config file
|
||||
func findAndParseConfig(configFiles []string) (*database.PostgresConfig, error) {
|
||||
var firstErr error
|
||||
var errs []error
|
||||
var triedFiles []string
|
||||
|
||||
for _, configFile := range configFiles {
|
||||
if configFile == "" {
|
||||
continue
|
||||
}
|
||||
triedFiles = append(triedFiles, configFile)
|
||||
|
||||
// Check if file exists
|
||||
if _, err := os.Stat(configFile); err != nil {
|
||||
if firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
errs = append(errs, fmt.Errorf("%s: %w", configFile, err))
|
||||
continue
|
||||
}
|
||||
|
||||
// Try to read and parse the file
|
||||
pgCfg, err := parseConfigFile(configFile)
|
||||
if err != nil {
|
||||
if firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
errs = append(errs, fmt.Errorf("%s: %w", configFile, err))
|
||||
continue
|
||||
}
|
||||
|
||||
return pgCfg, nil
|
||||
}
|
||||
|
||||
if firstErr != nil {
|
||||
return nil, fmt.Errorf("no config file found: %w", firstErr)
|
||||
if len(errs) > 0 {
|
||||
return nil, fmt.Errorf("no valid config file found (tried: %v): %w", triedFiles, errors.Join(errs...))
|
||||
}
|
||||
return nil, fmt.Errorf("no valid config files provided")
|
||||
}
|
||||
@@ -155,7 +176,7 @@ func parseConfigFile(configFile string) (*database.PostgresConfig, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open config file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
dec := yaml.NewDecoder(file)
|
||||
cfg := database.Config{}
|
||||
|
||||
Reference in New Issue
Block a user