EtfPort (EtfPort v2.0.0)

Copy Markdown View Source

Runs a program that speaks the beamports protocol as an Erlang port.

The protocol is one Erlang term per message, after a 4-byte length ({packet, 4}), each a map with "type". The program says what it is (hello: its operations and how many calls it takes at once), then that it runs (a state whose dimension is "life" and state "running"), and answers each call with a reply carrying the call's id and one of "ok" (the data), "fail" (a failure the program declares: %{"port" => ..., "details" => ...}) or "error" (a defect, or a call it refused). duckport, oxiport and tantiport speak it when started with --etf.

Each EtfPort process owns one program, which it starts at once. Calls wait until the program runs, and beyond the program's concurrency they wait their turn. A call whose time is up is cancelled; a program that takes one call at a time and doesn't answer within a grace (1 s) after that is killed and started again, and the call answers {:error, :uncertain_native_outcome}: it may or may not have taken effect. A program that exits is started again, at most three times in ten seconds; after that it is started again once the ten seconds have passed, or sooner by reset/2. Every state the program reports, and its starts and exits, go to :on_state.

{:ok, pid} =
  EtfPort.start_link(
    executable: "/usr/local/bin/duckport",
    args: ["--etf", "--database", "/var/db/app/analytics.duckdb"]
  )

{:ok, %{"rows" => [%{"answer" => 42}]}} =
  EtfPort.call(pid, "query", %{sql: "SELECT 42 AS answer", params: []})

Payloads are maps; their keys may be atoms or binaries, and they become binaries. Atoms other than true, false and nil become binaries, Date, Time, NaiveDateTime and DateTime ISO 8601 binaries, and a Decimal its text. Anything else that the protocol has no term for (a tuple, a pid, a function) refuses the call.

Summary

Types

Why a call didn't get the program's data.

Functions

Calls operation with payload and waits up to timeout milliseconds for the reply (and the grace after it, for a program that takes one call at a time). Returns {:ok, data} or {:error, reason} (see error/0).

Returns a specification to start this module under a supervisor.

What the program said it is (its hello), once it said it.

Stops the program once the calls made before have been answered, and starts it again. A barrier: nothing called before it is still running in the program afterwards.

Starts a process for one program, and the program.

Types

error()

@type error() ::
  {:fail, String.t(), term()}
  | String.t()
  | :timeout
  | :uncertain_native_outcome
  | {:native_exit, term()}
  | :missing_executable
  | {:spawn_failed, term()}
  | {:invalid_payload, String.t()}
  | :packet_limit

Why a call didn't get the program's data.

  • {:fail, port, details} - a failure the program declares.
  • a binary - the program's error: a defect, or a call it refused.
  • :timeout - the call's time was up before the program took it, or the program takes several calls at once and didn't answer in time.
  • :uncertain_native_outcome - the program didn't answer in time and was killed; the call may or may not have taken effect.
  • {:native_exit, status} - the program exited before it answered.
  • :missing_executable, {:spawn_failed, reason} - the program couldn't be started.
  • {:invalid_payload, message} - the payload has no term in the protocol.
  • :packet_limit - the call is larger than 16 MiB.

Functions

call(server, operation, payload \\ %{}, timeout \\ 30000)

@spec call(GenServer.server(), String.t(), map(), timeout()) ::
  {:ok, term()} | {:error, error()}

Calls operation with payload and waits up to timeout milliseconds for the reply (and the grace after it, for a program that takes one call at a time). Returns {:ok, data} or {:error, reason} (see error/0).

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

hello(server, timeout \\ 5000)

@spec hello(GenServer.server(), timeout()) :: {:ok, map()} | {:error, :not_started}

What the program said it is (its hello), once it said it.

reset(server, timeout \\ 30000)

@spec reset(GenServer.server(), timeout()) :: :ok

Stops the program once the calls made before have been answered, and starts it again. A barrier: nothing called before it is still running in the program afterwards.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts a process for one program, and the program.

Options:

  • :executable - absolute path of the program (required).
  • :args - its arguments, for example ["--etf", "--index", path].
  • :name - registers the process, as in GenServer.start_link/3.
  • :on_state - a function given each state as a map (%{"dimension" => "life", "state" => "running"}): the program's own, and "starting" when it is started, "failed" (with "reason") when it exited and isn't started again.
  • :restart - {count, milliseconds}: how many times an exited program is started again within that time (default {3, 10_000}).