Karta.Messaging.Sessions (Karta v1.0.0)

The rules of an agent's session, apart from the WebSocket that carries it (KartaWeb.Messaging.ConnectSocket): who the agent is, which session is the current one, what welcome says, and what happens when the connection ends.

One session per connector. open/3 makes the new session the connector's (session_id, session_node, session_seen_at, in the database) and announces it (Karta.Relay.messaging/3); the connection that held the old one closes with 4004, on this replica or another. A connection also looks at the row every few seconds (current?/2), so a lost announcement only delays. This is what makes several replicas correct without an Erlang cluster, and KARTA_CLUSTER makes it quicker, nothing more.

A new journal. When the agent's journal_id differs from the one stored, its journal was lost or replaced: Karta answers acked_event_seq: 0 and relies on the de-duplication of Karta.Messaging.Inbound; the reset is written to the connector's history.

Summary

Functions

Checks a hello: the connector exists, has not been revoked, and the proof is the agent's signature of the challenge. Errors are the close codes: 4002 (unknown, revoked or not enrolled), 4001 (bad proof).

The connection ended. If it was still the connector's session the connector is offline and the session is cleared; a connection that was already replaced changes nothing.

True while this is still the connector's session (and the connector is not revoked).

The seconds between the agent's pings (25; config :karta, Karta.Messaging, heartbeat_s: for tests).

Makes the connection the connector's session. Returns the connector as stored, the session id and the position Karta answers in welcome, or {:error, 4002} when the connector was revoked meanwhile.

Says the session is alive (the agent pinged).

The settings Karta sends an agent (welcome and the settings command).

A settings command with a fresh session token (sent when the limits change).

Marks connectors whose session has been silent for grace seconds as offline (the replica that held the connection died). Runs on any replica; returns how many.

True when the agent's version is at least the minimum Karta requires.

The welcome frame for a new session.

Functions

authenticate(hello, nonce, host)

@spec authenticate(map(), String.t(), String.t()) ::
  {:ok, Karta.Messaging.Connector.t()} | {:error, 4001 | 4002}

Checks a hello: the connector exists, has not been revoked, and the proof is the agent's signature of the challenge. Errors are the close codes: 4002 (unknown, revoked or not enrolled), 4001 (bad proof).

close(connector_id, session_id, reason)

@spec close(Ecto.UUID.t(), String.t(), String.t()) :: :ok

The connection ended. If it was still the connector's session the connector is offline and the session is cleared; a connection that was already replaced changes nothing.

current?(connector_id, session_id)

@spec current?(Ecto.UUID.t(), String.t()) :: :ok | {:error, :replaced | :revoked}

True while this is still the connector's session (and the connector is not revoked).

heartbeat_s()

The seconds between the agent's pings (25; config :karta, Karta.Messaging, heartbeat_s: for tests).

open(connector_id, hello, contracts)

@spec open(Ecto.UUID.t(), map(), map()) ::
  {:ok, Karta.Messaging.Connector.t(), String.t(), non_neg_integer()}
  | {:error, 4002}

Makes the connection the connector's session. Returns the connector as stored, the session id and the position Karta answers in welcome, or {:error, 4002} when the connector was revoked meanwhile.

seen(connector_id, session_id)

@spec seen(Ecto.UUID.t(), String.t()) :: :ok

Says the session is alive (the agent pinged).

settings(connector)

@spec settings(Karta.Messaging.Connector.t()) :: map()

The settings Karta sends an agent (welcome and the settings command).

settings_command(connector)

@spec settings_command(Karta.Messaging.Connector.t()) ::
  {:ok, Karta.Messaging.Command.t()}

A settings command with a fresh session token (sent when the limits change).

sweep(grace \\ 25 * 4, now \\ DateTime.utc_now())

@spec sweep(pos_integer(), DateTime.t()) :: non_neg_integer()

Marks connectors whose session has been silent for grace seconds as offline (the replica that held the connection died). Runs on any replica; returns how many.

version_ok?(agent_version)

@spec version_ok?(String.t() | nil) :: boolean()

True when the agent's version is at least the minimum Karta requires.

welcome(connector, session_id, contracts, acked_event_seq)

The welcome frame for a new session.