Karta.Outbound.Throttle (Karta v1.0.0)

Limits per destination: how many connections Karta has open at once to a group of receiving mail servers, and how many recipients it sends to there per minute.

Rules (parse/1) are text, entries separated by commas:

*.mx.example.com connections=5 per_minute=300, *.mx.example.org connections=10, * connections=20

An entry is a pattern of MX host names followed by its limits, either or both of connections (open at once) and per_minute (recipients in any 60 seconds). *.mx.example.com matches every host under mx.example.com, and all of them share the limits: a provider's many mail servers count as one destination. A host name matches that host only. * matches every host, each counted on its own. The first entry that matches a host decides (match/2); an entry without limits exempts its hosts.

Counting happens in this process, on each node, per key (with the replicas connected in a cluster, each counts its share of a rule's limits, share/2): the delivery passes its IP pool and the destination (acquire/5). Receiving servers limit what comes from one sending address, and a pool's addresses are used only on the node that holds them, so a pool's limits hold wherever it sends from; without a pool, each node counts for its own address. A delivery holds one connection slot for its whole run (its transactions follow one another) and gives it back at the end with the recipients it did not send to (release/3); the slot also comes back when the delivery's process dies. Counters start empty when a node starts.

Summary

Functions

Takes one connection slot of key and up to wanted recipients of its rate under rule, for the calling process: {:ok, granted, ref}, or {:wait, seconds, reason} when all slots are taken (:connections; a few seconds) or the minute's recipients are sent (:rate; until the oldest leaves the minute). :now in opts (milliseconds) replaces the clock in tests.

Returns a specification to start this module under a supervisor.

The destination of an MX host under rules: {group, rule}, the group being the pattern that matched (the host itself for *), or nil when no rule limits the host.

Reads rules (see the module doc): {:ok, rules} in their order, or {:error, problem} in words. nil and blank text are no rules.

As parse/1, but raises ArgumentError for text it cannot read.

Gives back the slot of ref, and the unused recipients of its rate.

Starts the counters; name: (default this module).

Types

rule()

@type rule() :: %{
  pattern: String.t(),
  connections: pos_integer() | nil,
  per_minute: pos_integer() | nil
}

Functions

acquire(server, key, rule, wanted, opts \\ [])

@spec acquire(GenServer.server(), term(), rule(), pos_integer(), keyword()) ::
  {:ok, pos_integer(), reference()}
  | {:wait, pos_integer(), :connections | :rate}

Takes one connection slot of key and up to wanted recipients of its rate under rule, for the calling process: {:ok, granted, ref}, or {:wait, seconds, reason} when all slots are taken (:connections; a few seconds) or the minute's recipients are sent (:rate; until the oldest leaves the minute). :now in opts (milliseconds) replaces the clock in tests.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

match(rules, host)

@spec match([rule()], String.t()) :: {String.t(), rule()} | nil

The destination of an MX host under rules: {group, rule}, the group being the pattern that matched (the host itself for *), or nil when no rule limits the host.

parse(text)

@spec parse(String.t() | nil) :: {:ok, [rule()]} | {:error, String.t()}

Reads rules (see the module doc): {:ok, rules} in their order, or {:error, problem} in words. nil and blank text are no rules.

parse!(text)

@spec parse!(String.t() | nil) :: [rule()]

As parse/1, but raises ArgumentError for text it cannot read.

release(server, ref, unused)

@spec release(GenServer.server(), reference(), non_neg_integer()) :: :ok

Gives back the slot of ref, and the unused recipients of its rate.

start_link(opts)

Starts the counters; name: (default this module).