forked from mtusi2026/mtusi2026lab2
This commit is contained in:
@@ -2,6 +2,7 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -10,6 +11,18 @@ import (
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/migrations"
|
||||
)
|
||||
|
||||
var (
|
||||
errURLRequired = errors.New("database URL is required")
|
||||
errMinConnections = errors.New("minimum database connections must not be negative")
|
||||
errMaxConnections = errors.New("maximum database connections must be positive")
|
||||
errConnectionRange = errors.New("minimum database connections exceed maximum")
|
||||
errMaxConnectionAge = errors.New("maximum database connection age must be positive")
|
||||
errMaxConnectionIdle = errors.New("maximum database connection idle time must be positive")
|
||||
errHealthCheckPeriod = errors.New("database health check period must be positive")
|
||||
errConnectionTimeout = errors.New("database connection timeout must be positive")
|
||||
errMigrationTimeout = errors.New("database migration timeout must be positive")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
URL string
|
||||
MinConnections int32
|
||||
@@ -58,23 +71,23 @@ func Open(ctx context.Context, config Config) (*pgxpool.Pool, error) {
|
||||
func validateConfig(config Config) error {
|
||||
switch {
|
||||
case config.URL == "":
|
||||
return fmt.Errorf("database URL is required")
|
||||
return errURLRequired
|
||||
case config.MinConnections < 0:
|
||||
return fmt.Errorf("minimum database connections must not be negative")
|
||||
return errMinConnections
|
||||
case config.MaxConnections < 1:
|
||||
return fmt.Errorf("maximum database connections must be positive")
|
||||
return errMaxConnections
|
||||
case config.MinConnections > config.MaxConnections:
|
||||
return fmt.Errorf("minimum database connections exceed maximum")
|
||||
return errConnectionRange
|
||||
case config.MaxConnectionAge <= 0:
|
||||
return fmt.Errorf("maximum database connection age must be positive")
|
||||
return errMaxConnectionAge
|
||||
case config.MaxConnectionIdle <= 0:
|
||||
return fmt.Errorf("maximum database connection idle time must be positive")
|
||||
return errMaxConnectionIdle
|
||||
case config.HealthCheckPeriod <= 0:
|
||||
return fmt.Errorf("database health check period must be positive")
|
||||
return errHealthCheckPeriod
|
||||
case config.ConnectTimeout <= 0:
|
||||
return fmt.Errorf("database connection timeout must be positive")
|
||||
return errConnectionTimeout
|
||||
case config.MigrationTimeout <= 0:
|
||||
return fmt.Errorf("database migration timeout must be positive")
|
||||
return errMigrationTimeout
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user