Karta.Quotas (Karta v1.0.0)

What an organisation may send: a daily limit on recipients, a per-recipient limit on messages to one address a day, and a trust level that holds back free-form and bulk sending (organisations.trust_level, daily_send_limit, per_recipient_daily_limit; set through the provisioning API, see Karta.Provisioning.Limits).

  • :standard (the default of an organisation made on the admin pages) holds nothing back. The limits apply when they are set.
  • :restricted is for low-volume transactional mail: at most 5 recipients to a message (To, Cc and Bcc together), and no batches (Karta.Sending.send_batch/3, send_each/3, and the batch endpoints of the APIs take one message at most).

Days are UTC days. The limits count at acceptance, in the transaction that accepts a message (reserve/2), so every way in is covered the same: SMTP submission and every HTTP API, on every node. A message that would pass a limit is refused whole and counts for nothing; a rolled-back acceptance gives its counts back with it. Mail Karta writes itself (delivery status notifications, reports, forwards) does not count.

The refusals are {:quota, reason}: :daily_limit and :recipient_limit (try again after midnight UTC, seconds_to_midnight/1), :too_many_recipients and :batch_not_permitted (the organisation is restricted).

Summary

Types

Why a message is refused.

Functions

Whether the organisation may send count messages in one call (a batch): a restricted organisation sends one.

The organisation's trust level and limits.

Deletes the counts of days before day - 1; returns how many rows.

Counts the message message_id against its organisation's limits, for addresses (the envelope recipients). Call it inside the transaction that accepts the message. :ok, or {:error, {:quota, reason}} after which the caller must roll the transaction back.

The most recipients a restricted organisation may name in one message.

Seconds until the next UTC midnight: when a daily limit counts from zero again.

How many recipients the organisation has had accepted today (UTC); 0 without a daily limit.

Types

reason()

@type reason() ::
  :daily_limit | :recipient_limit | :too_many_recipients | :batch_not_permitted

Why a message is refused.

Functions

check_batch(organisation_id, count)

@spec check_batch(Ecto.UUID.t(), non_neg_integer()) ::
  :ok | {:error, {:quota, reason()}}

Whether the organisation may send count messages in one call (a batch): a restricted organisation sends one.

profile(organisation_id)

@spec profile(Ecto.UUID.t()) :: %{
  trust_level: :restricted | :standard,
  daily: pos_integer() | nil,
  per_recipient: pos_integer() | nil
}

The organisation's trust level and limits.

prune(today \\ Date.utc_today())

@spec prune(Date.t()) :: non_neg_integer()

Deletes the counts of days before day - 1; returns how many rows.

reserve(message_id, addresses, day \\ Date.utc_today())

@spec reserve(Ecto.UUID.t(), [String.t()], Date.t()) ::
  :ok | {:error, {:quota, reason()}}

Counts the message message_id against its organisation's limits, for addresses (the envelope recipients). Call it inside the transaction that accepts the message. :ok, or {:error, {:quota, reason}} after which the caller must roll the transaction back.

restricted_recipients()

@spec restricted_recipients() :: pos_integer()

The most recipients a restricted organisation may name in one message.

seconds_to_midnight(now \\ DateTime.utc_now())

@spec seconds_to_midnight(DateTime.t()) :: pos_integer()

Seconds until the next UTC midnight: when a daily limit counts from zero again.

used_today(organisation_id, day \\ Date.utc_today())

@spec used_today(Ecto.UUID.t(), Date.t()) :: non_neg_integer()

How many recipients the organisation has had accepted today (UTC); 0 without a daily limit.