This commit is contained in:
Vladislav Kunetskiy
2026-11-05 20:33:31 +03:00
parent edc2a405ee
commit b303db2d62
194 changed files with 10047 additions and 3831 deletions
+30 -2
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"mtusi-messenger/internal/config"
@@ -24,7 +25,25 @@ type Store struct {
pipeline *PipelineRepository
}
func New(ctx context.Context, cfg config.Postgres, idempotencyTTL time.Duration) (*Store, error) {
type options struct {
queryTracer pgx.QueryTracer
}
// Option customizes the PostgreSQL store without leaking telemetry into the
// storage package's public API.
type Option func(*options)
// WithQueryTracer installs a pgx tracer (nil means no tracing).
func WithQueryTracer(tracer pgx.QueryTracer) Option {
return func(o *options) { o.queryTracer = tracer }
}
func New(ctx context.Context, cfg config.Postgres, idempotencyTTL time.Duration, opts ...Option) (*Store, error) {
options := options{}
for _, opt := range opts {
opt(&options)
}
cfg, err := normalize(cfg)
if err != nil {
return nil, fmt.Errorf("normalize database config: %w", err)
@@ -40,6 +59,9 @@ func New(ctx context.Context, cfg config.Postgres, idempotencyTTL time.Duration)
poolCfg.MaxConnLifetime = cfg.MaxConnLifetime
poolCfg.MaxConnIdleTime = cfg.MaxConnIdleTime
poolCfg.ConnConfig.ConnectTimeout = cfg.ConnectTimeout
if options.queryTracer != nil {
poolCfg.ConnConfig.Tracer = options.queryTracer
}
pool, err := pgxpool.NewWithConfig(ctx, poolCfg)
if err != nil {
@@ -82,13 +104,19 @@ func (s *Store) Sessions() *SessionRepository { return s.sessions }
func (s *Store) Pipeline() *PipelineRepository { return s.pipeline }
func (s *Store) Ping(ctx context.Context) error {
if err := s.pool.Ping(ctx); err != nil {
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
if err := s.pool.Ping(pingCtx); err != nil {
return fmt.Errorf("ping database: %w", err)
}
return nil
}
// PoolStats exposes a point-in-time pgxpool snapshot for telemetry callbacks.
func (s *Store) PoolStats() *pgxpool.Stat { return s.pool.Stat() }
func (s *Store) Close() { s.pool.Close() }
// sweepLockKey is a process-independent constant: every replica contends for the