Karta. Messaging. Router
(Karta v1.0.0)
Decides what happens to a send request, step by step. Only Karta decides:
a plug or an agent reports facts (Karta.Messaging.Errors), the router
chooses. A request names channels in order; each is a step.
Moving on is decided on facts
| What happened at the step | What the router does | Twice-sent risk |
|---|---|---|
| The connector has no live agent | waits up to policy.wait_for_connector_s (default 120; 0 does not wait), then moves on (offline) | none, nothing was sent |
The address is not on the channel (a cached check says no, or the send answered unknown_recipient) | moves on at once | none |
not_linked, permanent, unsupported, too_large | moves on | none |
retry, rate_limited, expired (nothing was sent) | tries the same channel again, three attempts in all, then moves on (retry_exhausted) | none |
unknown_outcome (nobody can tell if it went out) | stops: the request is unknown, the application decides | would be real |
Sent, but no delivery receipt after policy.fallback_after_s (300 or more) | only if the request sets it: sends on the next channel, flags both attempts duplicate_possible | real, and flagged |
Sent, no receipt, fallback_after_s not set | nothing | |
| The provider reports a failure after it accepted the message | moves on (it is a fact); never retried on the same channel |
A step is also skipped, with the reason recorded (skipped in the
request), when no connector serves the channel, when the channel costs
money (SMS) and the server, the request or the organisation's daily cap
does not allow it, or when a cached check says the address is not on it.
Each attempt is a message of its own, in the conversation of its own
connector, with fallback_from naming the channel before it; the answer
and the messaging.message.status events always say the channel used and,
where known, the cost.
Waiting
Quiet hours (the organisation's, not for sign-in codes), the connector's
send limits (agents only) and a connector that is offline put the request in
waiting with an Oban job that calls advance/2 later. advance/2 is
safe to call any number of times: only a request that is queued or
waiting moves, under a row lock.
decide/1 is the pure part (what follows an attempt's end, given the facts
about the request) and is property-tested.
Summary
Functions
Makes the next attempt of a request, or waits, or ends it. Options:
now (tests). Safe to call any number of times.
Called after an attempt changed state: moves the request along.
What follows an attempt that ended, from facts only. facts:
state (:failed, :sent, :unknown), class, attempts (made on this
channel), no_retry (the provider reported it after accepting), next?
(a later step is viable), retry_after. Returns {:retry, seconds},
{:fallback, reason}, :stop (nobody can tell: the application decides) or
:finish.
Whether a failed attempt ends the request (no retry, no later step).
The attempts a channel gets for a failure that sent nothing.
The first step with a connector and an allowance: {:ok, {channel, connector}} or {:error, reason} (:no_connector,
:paid_channel_not_allowed). Used at the door, before a request is stored.
fallback_after_s passed with no delivery receipt: if the attempt is still
only sent and a later step is viable, sends on the next channel and flags
both attempts duplicate_possible (the first may still arrive).
Starts a stored request: makes its first attempt, or puts it in waiting.
Returns the first message, or nil when the request waits or ended.
Moves requests that were left behind: a request queued or sending with no
attempt, or queued or waiting with nobody looking, for more than two
minutes (the node died between storing the request and making its attempt,
or between choosing a step and making it, or a job was lost). Returns how many.
Safe on any number of replicas: advance/2 only moves a request that is
queued or waiting, under a row lock, and its jobs are unique.
Connectors of the server on the channel that can take a message, best first.
Functions
@spec advance(Ecto.UUID.t(), keyword()) :: :ok
Makes the next attempt of a request, or waits, or ends it. Options:
now (tests). Safe to call any number of times.
@spec attempt_changed(Karta.Messaging.Message.t(), map()) :: :ok
Called after an attempt changed state: moves the request along.
@spec decide(map()) :: {:retry, non_neg_integer()} | {:fallback, String.t()} | :stop | :finish
What follows an attempt that ended, from facts only. facts:
state (:failed, :sent, :unknown), class, attempts (made on this
channel), no_retry (the provider reported it after accepting), next?
(a later step is viable), retry_after. Returns {:retry, seconds},
{:fallback, reason}, :stop (nobody can tell: the application decides) or
:finish.
@spec final_failure?( Karta.Messaging.Message.t(), String.t(), non_neg_integer() | nil, boolean() ) :: boolean()
Whether a failed attempt ends the request (no retry, no later step).
The attempts a channel gets for a failure that sent nothing.
@spec plan(Ecto.UUID.t(), [String.t()], map()) :: {:ok, {String.t(), Karta.Messaging.Connector.t()}} | {:error, :no_connector | :paid_channel_not_allowed}
The first step with a connector and an allowance: {:ok, {channel, connector}} or {:error, reason} (:no_connector,
:paid_channel_not_allowed). Used at the door, before a request is stored.
@spec receipt_check(Ecto.UUID.t()) :: :ok
fallback_after_s passed with no delivery receipt: if the attempt is still
only sent and a later step is viable, sends on the next channel and flags
both attempts duplicate_possible (the first may still arrive).
@spec start(Karta.Messaging.SendRequest.t(), keyword()) :: {:ok, Karta.Messaging.Message.t() | nil}
Starts a stored request: makes its first attempt, or puts it in waiting.
Returns the first message, or nil when the request waits or ended.
@spec sweep(DateTime.t()) :: non_neg_integer()
Moves requests that were left behind: a request queued or sending with no
attempt, or queued or waiting with nobody looking, for more than two
minutes (the node died between storing the request and making its attempt,
or between choosing a step and making it, or a job was lost). Returns how many.
Safe on any number of replicas: advance/2 only moves a request that is
queued or waiting, under a row lock, and its jobs are unique.
@spec usable(Ecto.UUID.t(), String.t()) :: [Karta.Messaging.Connector.t()]
Connectors of the server on the channel that can take a message, best first.