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 stepWhat the router doesTwice-sent risk
The connector has no live agentwaits 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 oncenone
not_linked, permanent, unsupported, too_largemoves onnone
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 decideswould 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_possiblereal, and flagged
Sent, no receipt, fallback_after_s not setnothing
The provider reports a failure after it accepted the messagemoves 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

advance(request_id, opts \\ [])

@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.

attempt_changed(message, opts \\ %{})

@spec attempt_changed(Karta.Messaging.Message.t(), map()) :: :ok

Called after an attempt changed state: moves the request along.

decide(facts)

@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.

final_failure?(message, class, retry_after, no_retry?)

@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).

max_attempts()

The attempts a channel gets for a failure that sent nothing.

plan(server_id, channels, policy \\ %{})

@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.

receipt_check(message_id)

@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).

start(send_request, opts \\ [])

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.

sweep(now \\ DateTime.utc_now())

@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.

usable(server_id, channel)

Connectors of the server on the channel that can take a message, best first.