Files

108 lines
3.7 KiB
Go

package postgres
import (
"context"
"errors"
"fmt"
"time"
"github.com/jackc/pgx/v5"
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
"lincodex/backend/internal/port/outbound"
)
type idempotencyRepository struct {
queries *sqlc.Queries
}
func (repository idempotencyRepository) Reserve(
ctx context.Context,
record outbound.IdempotencyRecord,
) (outbound.IdempotencyRecord, bool, error) {
const location = "idempotencyRepository.Reserve"
if err := requireTime(location, record.CreatedAt, outbound.ErrIdempotencyRecordNotFound); err != nil {
return outbound.IdempotencyRecord{}, false, err
}
if !record.ExpiresAt.After(record.CreatedAt) {
return outbound.IdempotencyRecord{}, false, errors.New(location + " - expiry must be after creation")
}
row, err := repository.queries.ReserveIdempotencyRecord(ctx, sqlc.ReserveIdempotencyRecordParams{
ScopeHash: record.Scope[:],
Fingerprint: record.Fingerprint[:],
CreatedAt: pgTime(record.CreatedAt),
ExpiresAt: pgTime(record.ExpiresAt),
})
if err == nil {
return idempotencyFromRow(location, row.ScopeHash, row.Fingerprint, row.CreatedAt.Time, row.ExpiresAt.Time, true)
}
if !isNoRows(err) {
return outbound.IdempotencyRecord{}, false, fmt.Errorf("%s - store record: %w", location, mapIdentityError(err))
}
// A live record with the scope exists and was left as it is. It is
// visible now: a concurrent writer committed before this statement
// returned.
existing, err := repository.Find(ctx, record.Scope, record.CreatedAt)
if err != nil {
return outbound.IdempotencyRecord{}, false, fmt.Errorf("%s - read live record: %w", location, err)
}
return existing, false, nil
}
func (repository idempotencyRepository) Find(
ctx context.Context,
scope [outbound.IdempotencyDigestSize]byte,
at time.Time,
) (outbound.IdempotencyRecord, error) {
const location = "idempotencyRepository.Find"
if err := requireTime(location, at, outbound.ErrIdempotencyRecordNotFound); err != nil {
return outbound.IdempotencyRecord{}, err
}
row, err := repository.queries.GetLiveIdempotencyRecord(ctx, sqlc.GetLiveIdempotencyRecordParams{
ScopeHash: scope[:],
At: pgTime(at),
})
if err != nil {
return outbound.IdempotencyRecord{}, fmt.Errorf("%s - select record: %w", location, mapNoRows(err, outbound.ErrIdempotencyRecordNotFound))
}
record, _, err := idempotencyFromRow(location, row.ScopeHash, row.Fingerprint, row.CreatedAt.Time, row.ExpiresAt.Time, false)
return record, err
}
func (repository idempotencyRepository) PurgeExpired(ctx context.Context, at time.Time, limit int32) (int64, error) {
const location = "idempotencyRepository.PurgeExpired"
if err := requireTime(location, at, outbound.ErrIdempotencyRecordNotFound); err != nil {
return 0, err
}
if limit <= 0 {
return 0, errors.New(location + " - limit must be positive")
}
deleted, err := repository.queries.PurgeExpiredIdempotencyRecords(ctx, sqlc.PurgeExpiredIdempotencyRecordsParams{
At: pgTime(at),
BatchLimit: limit,
})
if err != nil {
return 0, fmt.Errorf("%s - delete records: %w", location, mapIdentityError(err))
}
return deleted, nil
}
func idempotencyFromRow(
location string,
scope, fingerprint []byte,
createdAt, expiresAt time.Time,
created bool,
) (outbound.IdempotencyRecord, bool, error) {
record := outbound.IdempotencyRecord{CreatedAt: createdAt, ExpiresAt: expiresAt}
if len(scope) != outbound.IdempotencyDigestSize || len(fingerprint) != outbound.IdempotencyDigestSize {
return outbound.IdempotencyRecord{}, false, errors.New(location + " - stored digest has an unexpected size")
}
copy(record.Scope[:], scope)
copy(record.Fingerprint[:], fingerprint)
return record, created, nil
}
func isNoRows(err error) bool {
return errors.Is(err, pgx.ErrNoRows)
}