494 lines
13 KiB
Go
494 lines
13 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: mail_quota.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const addMailQuotaUsage = `-- name: AddMailQuotaUsage :execrows
|
|
UPDATE mail_quota_usage
|
|
SET
|
|
used_recipients = mail_quota_usage.used_recipients
|
|
+ $1::bigint,
|
|
updated_at = $2
|
|
WHERE
|
|
provider = $3
|
|
AND provider_account = $4
|
|
AND (
|
|
(period_kind = 'day' AND period_start = $5)
|
|
OR (period_kind = 'month' AND period_start = $6)
|
|
)
|
|
`
|
|
|
|
type AddMailQuotaUsageParams struct {
|
|
Recipients int64
|
|
UpdatedAt pgtype.Timestamptz
|
|
Provider string
|
|
ProviderAccount string
|
|
DayStart pgtype.Date
|
|
MonthStart pgtype.Date
|
|
}
|
|
|
|
// Adds recipients to the day and the month row. The caller holds their
|
|
// locks from LockMailQuotaRows and has checked both limits.
|
|
func (q *Queries) AddMailQuotaUsage(ctx context.Context, arg AddMailQuotaUsageParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, addMailQuotaUsage,
|
|
arg.Recipients,
|
|
arg.UpdatedAt,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.DayStart,
|
|
arg.MonthStart,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const ensureMailQuotaRows = `-- name: EnsureMailQuotaRows :exec
|
|
|
|
INSERT INTO mail_quota_usage (
|
|
provider,
|
|
provider_account,
|
|
period_kind,
|
|
period_start,
|
|
used_recipients,
|
|
updated_at
|
|
)
|
|
SELECT
|
|
$1::text,
|
|
$2::text,
|
|
split_part(window_key, '/', 1),
|
|
split_part(window_key, '/', 2)::date,
|
|
0,
|
|
$3::timestamptz
|
|
FROM unnest($4::text []) AS window_key
|
|
ORDER BY split_part(window_key, '/', 1), split_part(window_key, '/', 2)::date
|
|
ON CONFLICT (
|
|
provider, provider_account, period_kind, period_start
|
|
) DO NOTHING
|
|
`
|
|
|
|
type EnsureMailQuotaRowsParams struct {
|
|
Provider string
|
|
ProviderAccount string
|
|
UpdatedAt pgtype.Timestamptz
|
|
WindowKeys []string
|
|
}
|
|
|
|
// Lock order. Every transaction that writes mail_quota_usage first creates
|
|
// and locks all the usage rows it will touch with EnsureMailQuotaRows and
|
|
// LockMailQuotaRows, which both work in (period_kind, period_start) order,
|
|
// and only then writes them or a mail_quota_reservations row. Later UPDATE
|
|
// statements of the same transaction find their rows locked already, so the
|
|
// scan order of an UPDATE never decides the lock order.
|
|
// Creates the listed window rows of a provider account with no usage when
|
|
// they do not exist yet, in (period_kind, period_start) order. A window key
|
|
// is "<period_kind>/<YYYY-MM-DD>". A concurrent insert of the same row waits
|
|
// for the first transaction and then inserts nothing. Existing rows are
|
|
// neither changed nor locked here.
|
|
func (q *Queries) EnsureMailQuotaRows(ctx context.Context, arg EnsureMailQuotaRowsParams) error {
|
|
_, err := q.db.Exec(ctx, ensureMailQuotaRows,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.UpdatedAt,
|
|
arg.WindowKeys,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const findOpenMailQuotaReservation = `-- name: FindOpenMailQuotaReservation :one
|
|
SELECT
|
|
id,
|
|
day_start,
|
|
month_start,
|
|
recipients,
|
|
reserved_at
|
|
FROM mail_quota_reservations
|
|
WHERE
|
|
delivery_id = $1
|
|
AND provider = $2
|
|
AND provider_account = $3
|
|
AND released_at IS NULL
|
|
AND reserved_at >= $4
|
|
ORDER BY reserved_at DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
type FindOpenMailQuotaReservationParams struct {
|
|
DeliveryID pgtype.UUID
|
|
Provider string
|
|
ProviderAccount string
|
|
ReservedSince pgtype.Timestamptz
|
|
}
|
|
|
|
type FindOpenMailQuotaReservationRow struct {
|
|
ID pgtype.UUID
|
|
DayStart pgtype.Date
|
|
MonthStart pgtype.Date
|
|
Recipients int64
|
|
ReservedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// Returns the newest open reservation of a delivery under a provider account
|
|
// made at or after reserved_since. A retry of the delivery inside the
|
|
// provider's idempotency window reuses it instead of counting again.
|
|
func (q *Queries) FindOpenMailQuotaReservation(ctx context.Context, arg FindOpenMailQuotaReservationParams) (FindOpenMailQuotaReservationRow, error) {
|
|
row := q.db.QueryRow(ctx, findOpenMailQuotaReservation,
|
|
arg.DeliveryID,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.ReservedSince,
|
|
)
|
|
var i FindOpenMailQuotaReservationRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.DayStart,
|
|
&i.MonthStart,
|
|
&i.Recipients,
|
|
&i.ReservedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getMailQuotaUsage = `-- name: GetMailQuotaUsage :many
|
|
SELECT
|
|
period_kind,
|
|
used_recipients,
|
|
provider_exhausted_at
|
|
FROM mail_quota_usage
|
|
WHERE
|
|
provider = $1
|
|
AND provider_account = $2
|
|
AND (
|
|
(period_kind = 'day' AND period_start = $3)
|
|
OR (period_kind = 'month' AND period_start = $4)
|
|
)
|
|
`
|
|
|
|
type GetMailQuotaUsageParams struct {
|
|
Provider string
|
|
ProviderAccount string
|
|
DayStart pgtype.Date
|
|
MonthStart pgtype.Date
|
|
}
|
|
|
|
type GetMailQuotaUsageRow struct {
|
|
PeriodKind string
|
|
UsedRecipients int64
|
|
ProviderExhaustedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// Reads the usage of the day and the month window without locking.
|
|
func (q *Queries) GetMailQuotaUsage(ctx context.Context, arg GetMailQuotaUsageParams) ([]GetMailQuotaUsageRow, error) {
|
|
rows, err := q.db.Query(ctx, getMailQuotaUsage,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.DayStart,
|
|
arg.MonthStart,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetMailQuotaUsageRow
|
|
for rows.Next() {
|
|
var i GetMailQuotaUsageRow
|
|
if err := rows.Scan(&i.PeriodKind, &i.UsedRecipients, &i.ProviderExhaustedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const insertMailQuotaReservation = `-- name: InsertMailQuotaReservation :exec
|
|
INSERT INTO mail_quota_reservations (
|
|
id,
|
|
delivery_id,
|
|
provider,
|
|
provider_account,
|
|
day_start,
|
|
month_start,
|
|
recipients,
|
|
reserved_at
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8
|
|
)
|
|
`
|
|
|
|
type InsertMailQuotaReservationParams struct {
|
|
ID pgtype.UUID
|
|
DeliveryID pgtype.UUID
|
|
Provider string
|
|
ProviderAccount string
|
|
DayStart pgtype.Date
|
|
MonthStart pgtype.Date
|
|
Recipients int64
|
|
ReservedAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) InsertMailQuotaReservation(ctx context.Context, arg InsertMailQuotaReservationParams) error {
|
|
_, err := q.db.Exec(ctx, insertMailQuotaReservation,
|
|
arg.ID,
|
|
arg.DeliveryID,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.DayStart,
|
|
arg.MonthStart,
|
|
arg.Recipients,
|
|
arg.ReservedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listOtherMailQuotaAccounts = `-- name: ListOtherMailQuotaAccounts :many
|
|
SELECT DISTINCT provider_account
|
|
FROM mail_quota_usage
|
|
WHERE
|
|
provider = $1
|
|
AND provider_account <> $2
|
|
AND period_kind = 'month'
|
|
AND period_start = $3
|
|
AND used_recipients > 0
|
|
ORDER BY provider_account
|
|
`
|
|
|
|
type ListOtherMailQuotaAccountsParams struct {
|
|
Provider string
|
|
ProviderAccount string
|
|
MonthStart pgtype.Date
|
|
}
|
|
|
|
// Lists the other accounts of a provider that have usage in a month, so
|
|
// startup can warn about a renamed EMAIL_ACCOUNT.
|
|
func (q *Queries) ListOtherMailQuotaAccounts(ctx context.Context, arg ListOtherMailQuotaAccountsParams) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, listOtherMailQuotaAccounts, arg.Provider, arg.ProviderAccount, arg.MonthStart)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []string
|
|
for rows.Next() {
|
|
var provider_account string
|
|
if err := rows.Scan(&provider_account); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, provider_account)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockMailQuotaRows = `-- name: LockMailQuotaRows :many
|
|
SELECT
|
|
period_kind,
|
|
period_start,
|
|
used_recipients
|
|
FROM mail_quota_usage
|
|
WHERE
|
|
provider = $1
|
|
AND provider_account = $2
|
|
AND period_start = any($3::date [])
|
|
AND period_kind || '/' || to_char(period_start, 'YYYY-MM-DD')
|
|
= any($4::text [])
|
|
ORDER BY period_kind, period_start
|
|
FOR UPDATE
|
|
`
|
|
|
|
type LockMailQuotaRowsParams struct {
|
|
Provider string
|
|
ProviderAccount string
|
|
PeriodStarts []pgtype.Date
|
|
WindowKeys []string
|
|
}
|
|
|
|
type LockMailQuotaRowsRow struct {
|
|
PeriodKind string
|
|
PeriodStart pgtype.Date
|
|
UsedRecipients int64
|
|
}
|
|
|
|
// Locks the listed window rows until the transaction ends, in
|
|
// (period_kind, period_start) order, the same order for every caller, and
|
|
// returns their usage. Under READ COMMITTED a waiting caller reads the usage
|
|
// the first caller committed. period_starts narrows the index scan;
|
|
// window_keys selects the exact rows.
|
|
func (q *Queries) LockMailQuotaRows(ctx context.Context, arg LockMailQuotaRowsParams) ([]LockMailQuotaRowsRow, error) {
|
|
rows, err := q.db.Query(ctx, lockMailQuotaRows,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.PeriodStarts,
|
|
arg.WindowKeys,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []LockMailQuotaRowsRow
|
|
for rows.Next() {
|
|
var i LockMailQuotaRowsRow
|
|
if err := rows.Scan(&i.PeriodKind, &i.PeriodStart, &i.UsedRecipients); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const markMailQuotaExhausted = `-- name: MarkMailQuotaExhausted :execrows
|
|
UPDATE mail_quota_usage
|
|
SET
|
|
used_recipients = greatest(
|
|
mail_quota_usage.used_recipients, $1::bigint
|
|
),
|
|
provider_exhausted_at = $2,
|
|
updated_at = $2
|
|
WHERE
|
|
provider = $3
|
|
AND provider_account = $4
|
|
AND period_kind = $5
|
|
AND period_start = $6
|
|
`
|
|
|
|
type MarkMailQuotaExhaustedParams struct {
|
|
LimitRecipients int64
|
|
ExhaustedAt pgtype.Timestamptz
|
|
Provider string
|
|
ProviderAccount string
|
|
PeriodKind string
|
|
PeriodStart pgtype.Date
|
|
}
|
|
|
|
// Records that the provider refused a send because the quota of a window is
|
|
// used up: the usage rises to at least the configured limit, so the local
|
|
// gate stops admitting sends until the window resets or an operator raises
|
|
// the limit. The caller holds the row lock.
|
|
func (q *Queries) MarkMailQuotaExhausted(ctx context.Context, arg MarkMailQuotaExhaustedParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, markMailQuotaExhausted,
|
|
arg.LimitRecipients,
|
|
arg.ExhaustedAt,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.PeriodKind,
|
|
arg.PeriodStart,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const recordMailQuotaOutcome = `-- name: RecordMailQuotaOutcome :execrows
|
|
UPDATE mail_quota_reservations
|
|
SET
|
|
settled_outcome = $1,
|
|
settled_at = $2
|
|
WHERE id = $3
|
|
`
|
|
|
|
type RecordMailQuotaOutcomeParams struct {
|
|
SettledOutcome pgtype.Text
|
|
SettledAt pgtype.Timestamptz
|
|
ID pgtype.UUID
|
|
}
|
|
|
|
// Records the latest settled outcome of a send under the reservation.
|
|
func (q *Queries) RecordMailQuotaOutcome(ctx context.Context, arg RecordMailQuotaOutcomeParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, recordMailQuotaOutcome, arg.SettledOutcome, arg.SettledAt, arg.ID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const releaseMailQuotaReservation = `-- name: ReleaseMailQuotaReservation :execrows
|
|
UPDATE mail_quota_reservations
|
|
SET released_at = $1
|
|
WHERE
|
|
id = $2
|
|
AND released_at IS NULL
|
|
`
|
|
|
|
type ReleaseMailQuotaReservationParams struct {
|
|
ReleasedAt pgtype.Timestamptz
|
|
ID pgtype.UUID
|
|
}
|
|
|
|
// Marks a reservation released. It matches only an open reservation, so a
|
|
// repeated release changes nothing and returns 0 rows.
|
|
func (q *Queries) ReleaseMailQuotaReservation(ctx context.Context, arg ReleaseMailQuotaReservationParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, releaseMailQuotaReservation, arg.ReleasedAt, arg.ID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const releaseMailQuotaUsage = `-- name: ReleaseMailQuotaUsage :execrows
|
|
UPDATE mail_quota_usage
|
|
SET
|
|
used_recipients = greatest(
|
|
mail_quota_usage.used_recipients - $1::bigint, 0
|
|
),
|
|
updated_at = $2
|
|
WHERE
|
|
provider = $3
|
|
AND provider_account = $4
|
|
AND provider_exhausted_at IS NULL
|
|
AND (
|
|
(period_kind = 'day' AND period_start = $5)
|
|
OR (period_kind = 'month' AND period_start = $6)
|
|
)
|
|
`
|
|
|
|
type ReleaseMailQuotaUsageParams struct {
|
|
Recipients int64
|
|
UpdatedAt pgtype.Timestamptz
|
|
Provider string
|
|
ProviderAccount string
|
|
DayStart pgtype.Date
|
|
MonthStart pgtype.Date
|
|
}
|
|
|
|
// Gives back recipients of a released reservation. The caller holds the row
|
|
// locks. A window that the provider reported exhausted keeps its usage: the
|
|
// provider's answer outranks the local count until the window resets. The
|
|
// count never drops below zero.
|
|
func (q *Queries) ReleaseMailQuotaUsage(ctx context.Context, arg ReleaseMailQuotaUsageParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, releaseMailQuotaUsage,
|
|
arg.Recipients,
|
|
arg.UpdatedAt,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.DayStart,
|
|
arg.MonthStart,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|