Kantoor.Quota (Kantoor v1.0.0)

Virtual quotas, computed from the index (Karta holds the bytes).

Effective quota = mailbox override || domain default || nil (unlimited). Usage = sum of size_bytes of VISIBLE messages in the mailbox. Held (over_quota_held) mail is counted separately and does not count towards usage. Held mail has no UID until it is released (PLAN 2b point 1).

Summary

Functions

The effective quota in bytes, or nil for unlimited.

True when adding size to usage stays within quota (nil = unlimited).

Count and bytes of held messages, for the admin/user warning.

Should a new message of size be held? True when any older message is already held (to preserve arrival order) or when it would push usage past the quota. Call with the mailbox row locked.

Plans a release. held is [{id, size}] oldest first. Takes messages in order while they fit and stops at the first that does not (strict order: a smaller newer message never overtakes an older one).

Releases held messages oldest first until the quota is full again, assigning UIDs and modseqs at that moment. Returns {:ok, released_messages}. Safe to call at any time (e.g. after expunging mail or raising a quota).

Bytes used by visible messages.

Functions

effective_quota(mailbox)

@spec effective_quota(Kantoor.Mailboxes.Mailbox.t()) :: non_neg_integer() | nil

The effective quota in bytes, or nil for unlimited.

fits?(quota, usage, size)

@spec fits?(non_neg_integer() | nil, non_neg_integer(), non_neg_integer()) ::
  boolean()

True when adding size to usage stays within quota (nil = unlimited).

held_summary(mailbox)

@spec held_summary(Kantoor.Mailboxes.Mailbox.t()) :: %{
  count: non_neg_integer(),
  bytes: non_neg_integer()
}

Count and bytes of held messages, for the admin/user warning.

hold?(mailbox, size)

Should a new message of size be held? True when any older message is already held (to preserve arrival order) or when it would push usage past the quota. Call with the mailbox row locked.

plan_release(quota, usage, held)

@spec plan_release(non_neg_integer() | nil, non_neg_integer(), [
  {term(), non_neg_integer()}
]) :: [
  term()
]

Plans a release. held is [{id, size}] oldest first. Takes messages in order while they fit and stops at the first that does not (strict order: a smaller newer message never overtakes an older one).

release_held(mailbox)

@spec release_held(Kantoor.Mailboxes.Mailbox.t()) ::
  {:ok, [Kantoor.Mailboxes.Message.t()]}

Releases held messages oldest first until the quota is full again, assigning UIDs and modseqs at that moment. Returns {:ok, released_messages}. Safe to call at any time (e.g. after expunging mail or raising a quota).

usage(mailbox)

Bytes used by visible messages.