Malachi.ConnectionLimiter (malachi v0.22.7)

View Source

Tracks active connections per IP address and globally. Rejects new connections when limits exceeded.

Schema

Uses three ETS tables:

  • :malachi_conn_limits_ip - {ip, count} per-IP counters
  • :malachi_conn_limits_global - {:total, count} global counter
  • :malachi_conn_pids - {pid, ip, monitor_ref} process tracking

Configuration

  • connection_limit_enabled - Enable/disable connection limiting (default: true)
  • max_connections_per_ip - Max connections per IP (default: 100)
  • max_total_connections - Max total connections (default: 10000)

Summary

Functions

Returns a specification to start this module under a supervisor.

Get connection count for specific IP.

Get statistics about connections.

Get total connection count.

List all connections by IP.

Register a new connection.

Clears all tracked connections: the per-IP and global counters and the process monitors. This does not change limiting behavior; it only resets the tracked state, so it is primarily for tests that need a known-clean baseline (the tracker is process-global, shared with the live server and every other test).

Starts the connection limiter GenServer.

Unregister a connection manually.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_connection_count(ip)

Get connection count for specific IP.

get_stats()

Get statistics about connections.

get_total_connections()

Get total connection count.

list_connections()

List all connections by IP.

Returns map of %{ip => count}.

register_connection(pid, ip)

Register a new connection.

Returns :ok if allowed, or error tuple if limit exceeded.

Examples

register_connection(self(), "192.168.1.1")
#=> :ok

register_connection(self(), "10.0.0.1")
#=> {:error, :connection_limit_exceeded}

reset()

Clears all tracked connections: the per-IP and global counters and the process monitors. This does not change limiting behavior; it only resets the tracked state, so it is primarily for tests that need a known-clean baseline (the tracker is process-global, shared with the live server and every other test).

start_link(opts)

Starts the connection limiter GenServer.

unregister_connection(pid)

Unregister a connection manually.

Usually not needed as process monitoring handles cleanup automatically.