Karta.SMTP.Limiter (Karta v1.0.0)

Per-client connection limits for one listener:

  • at most max_per_ip simultaneous connections per client, and
  • at most rate new connections per client per rate_window milliseconds.

A client is an IPv4 address or an IPv6 /64 prefix (one end site usually holds a whole /64, so per-address limits would be trivial to evade). Addresses in exempt (CIDR strings) are counted but never refused.

The limiter monitors each connection process, so a crashed or killed connection frees its slot. It also counts all open connections of the listener; stressed?/1 is true from stress_threshold connections on, and the handler then uses its short stress-mode timeout.

The count belongs to the node the sockets are open on, so it is kept per node: several nodes each protect their own listener. While the limiter's process is restarting, its table is gone and the reads answer false and 0 rather than crash a connection.

Summary

Functions

Takes a connection slot for ip, owned by the calling process until it exits.

Returns a specification to start this module under a supervisor.

Open connections for a client key; for tests and diagnostics.

The client key: the IPv4 address, or the IPv6 /64 prefix (Karta.CIDR.client_key/1).

True when the listener has stress_threshold or more open connections.

Types

reason()

@type reason() :: :too_many_connections | :rate_limited

Functions

acquire(name, ip)

@spec acquire(GenServer.server(), :inet.ip_address()) :: :ok | {:error, reason()}

Takes a connection slot for ip, owned by the calling process until it exits.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

count(name, ip)

@spec count(atom(), :inet.ip_address()) :: non_neg_integer()

Open connections for a client key; for tests and diagnostics.

key(ip)

The client key: the IPv4 address, or the IPv6 /64 prefix (Karta.CIDR.client_key/1).

start_link(opts)

stressed?(name)

@spec stressed?(atom()) :: boolean()

True when the listener has stress_threshold or more open connections.