|
| 1 | +/* |
| 2 | +SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +
|
| 16 | +SPDX-License-Identifier: Apache-2.0 |
| 17 | +*/ |
| 18 | + |
| 19 | +package postgres |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "log/slog" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/jackc/pgx/v5/pgxpool" |
| 28 | +) |
| 29 | + |
| 30 | +// PostgresConfig holds PostgreSQL connection configuration |
| 31 | +type PostgresConfig struct { |
| 32 | + Host string |
| 33 | + Port int |
| 34 | + Database string |
| 35 | + User string |
| 36 | + Password string |
| 37 | + MaxConns int32 |
| 38 | + MinConns int32 |
| 39 | + MaxConnLifetime time.Duration |
| 40 | + SSLMode string |
| 41 | +} |
| 42 | + |
| 43 | +// PostgresClient handles PostgreSQL database operations |
| 44 | +type PostgresClient struct { |
| 45 | + pool *pgxpool.Pool |
| 46 | + logger *slog.Logger |
| 47 | +} |
| 48 | + |
| 49 | +// NewPostgresClientFromParams creates a new PostgreSQL client with the given connection parameters. |
| 50 | +// This is a convenience function that constructs a PostgresConfig and calls NewPostgresClient. |
| 51 | +func CreatePostgresClient( |
| 52 | + ctx context.Context, |
| 53 | + logger *slog.Logger, |
| 54 | + host string, |
| 55 | + port int, |
| 56 | + database string, |
| 57 | + user string, |
| 58 | + password string, |
| 59 | + maxConns int32, |
| 60 | + minConns int32, |
| 61 | + maxConnLifetime time.Duration, |
| 62 | + sslMode string, |
| 63 | +) (*PostgresClient, error) { |
| 64 | + config := PostgresConfig{ |
| 65 | + Host: host, |
| 66 | + Port: port, |
| 67 | + Database: database, |
| 68 | + User: user, |
| 69 | + Password: password, |
| 70 | + MaxConns: maxConns, |
| 71 | + MinConns: minConns, |
| 72 | + MaxConnLifetime: maxConnLifetime, |
| 73 | + SSLMode: sslMode, |
| 74 | + } |
| 75 | + |
| 76 | + client, err := NewPostgresClient(ctx, config, logger) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + logger.Info("postgres client initialized", |
| 82 | + slog.String("host", host), |
| 83 | + slog.Int("port", port), |
| 84 | + slog.String("database", database), |
| 85 | + ) |
| 86 | + |
| 87 | + return client, nil |
| 88 | +} |
| 89 | + |
| 90 | +// NewPostgresClient creates a new PostgreSQL client with connection pooling |
| 91 | +func NewPostgresClient(ctx context.Context, config PostgresConfig, logger *slog.Logger) (*PostgresClient, error) { |
| 92 | + // Build connection URL |
| 93 | + connURL := fmt.Sprintf( |
| 94 | + "postgres://%s:%s@%s:%d/%s?sslmode=%s", |
| 95 | + config.User, |
| 96 | + config.Password, |
| 97 | + config.Host, |
| 98 | + config.Port, |
| 99 | + config.Database, |
| 100 | + config.SSLMode, |
| 101 | + ) |
| 102 | + |
| 103 | + // Parse config to get a pgxpool.Config |
| 104 | + poolConfig, err := pgxpool.ParseConfig(connURL) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("failed to parse connection config: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + // Configure connection pool settings |
| 110 | + poolConfig.MaxConns = config.MaxConns |
| 111 | + poolConfig.MinConns = config.MinConns |
| 112 | + poolConfig.MaxConnLifetime = config.MaxConnLifetime |
| 113 | + |
| 114 | + // Create connection pool |
| 115 | + pool, err := pgxpool.NewWithConfig(ctx, poolConfig) |
| 116 | + if err != nil { |
| 117 | + return nil, fmt.Errorf("failed to create connection pool: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + // Ping to verify connection |
| 121 | + pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 122 | + defer cancel() |
| 123 | + |
| 124 | + if err := pool.Ping(pingCtx); err != nil { |
| 125 | + pool.Close() |
| 126 | + return nil, fmt.Errorf("failed to ping database: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + logger.Info("postgres client connected successfully") |
| 130 | + |
| 131 | + return &PostgresClient{ |
| 132 | + pool: pool, |
| 133 | + logger: logger, |
| 134 | + }, nil |
| 135 | +} |
| 136 | + |
| 137 | +// Close closes the database connection pool |
| 138 | +func (c *PostgresClient) Close() { |
| 139 | + c.logger.Info("closing postgres client") |
| 140 | + c.pool.Close() |
| 141 | +} |
| 142 | + |
| 143 | +// Pool returns the underlying pgxpool.Pool for direct database access |
| 144 | +func (c *PostgresClient) Pool() *pgxpool.Pool { |
| 145 | + return c.pool |
| 146 | +} |
| 147 | + |
| 148 | +// Ping verifies the database connection is still alive |
| 149 | +func (c *PostgresClient) Ping(ctx context.Context) error { |
| 150 | + return c.pool.Ping(ctx) |
| 151 | +} |
0 commit comments