forked from mtusi2026/mtusi2026lab2
41 lines
718 B
Go
41 lines
718 B
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type PostgreSQLStore struct {
|
|
pool *pgxpool.Pool
|
|
queryTimeout time.Duration
|
|
}
|
|
|
|
func NewPostgreSQLStore(
|
|
pool *pgxpool.Pool,
|
|
queryTimeout time.Duration,
|
|
) *PostgreSQLStore {
|
|
return &PostgreSQLStore{
|
|
pool: pool,
|
|
queryTimeout: queryTimeout,
|
|
}
|
|
}
|
|
|
|
func (s *PostgreSQLStore) Ping(ctx context.Context) error {
|
|
queryContext, cancel := s.withTimeout(ctx)
|
|
defer cancel()
|
|
|
|
return s.pool.Ping(queryContext)
|
|
}
|
|
|
|
func (s *PostgreSQLStore) Close() {
|
|
s.pool.Close()
|
|
}
|
|
|
|
func (s *PostgreSQLStore) withTimeout(
|
|
ctx context.Context,
|
|
) (context.Context, context.CancelFunc) {
|
|
return context.WithTimeout(ctx, s.queryTimeout)
|
|
}
|