Alternera.Plugin.Provisional.Hold (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

Provisional: added for the Throttle and Circuit breaker steps, and a change to it needs a changelog line but no new major (docs/USER_PLUGINS_DESIGN.md).

A step in Before that holds something for the rest of the exchange: a turn of a throttle, a pass of a circuit breaker, anything the plugin wants back when the exchange ends. The plugin defines leave/3, and its call/3 returns {:hold, held} or {:hold, held, summary} (Alternera.Plugin.call/3); the runner keeps held, opaque to it, under the step's pipeline and id, and gives it back to the plugin's Alternera.Plugin.leave/3 exactly once (Plugin.CallAdapter), on whichever path ends the exchange first:

  • the step's Close in After ("plugin" => "close", settings: %{of: step id}, a node of the pipeline language the runner runs) runs: {:response, status, headers}, the response head as it reached After, whatever a step above the Close made of it;
  • the upstream failed before After ran: {:error, type};
  • the exchange ended without After, or the request side unwound past the step's pipeline: :not_sent.

leave/3 runs in the process that ends the exchange, which for the last path is the door's, so it must be short and must not block. A raise in it is logged with the step and the exchange goes on: nothing the client gets depends on the release. A summary it returns goes on the Close's outcome beside released, as what the release did.

A plugin never needs a Close of its own: the Close is the generic "give back what step of holds", which the runner carries out itself, and the runner knows which plugin's hold that is. A save check pairs each holding step with one Close in After (Pipelines.FlowCheck).

A step that holds nothing in a dry run or a pipeline test (its traffic is not the partner's) still returns a hold, so its Close closes it as on traffic, and leave/3 is not called (previews: :skip, the default for a holding plugin; Plugin.CallAdapter).

The three reads below are keys the host puts in the context of a holding step that Alternera.Plugin.context/0 does not declare; a plugin reads them through this module, so that where the host keeps them can change without the plugin noticing.

Summary

Types

How the exchange ended for a hold: the response head as it reached After (headers as the exchange carries them, a map or a list of pairs), an upstream error by its type, or no response at all.

Functions

The process the exchange belongs to, which outlives the task a step may run in: what a hold on a process (a monitor, a lock) should name as its owner, so that a connection that closes gives the hold back on its exit. The calling process when the host set none.

In a preview (context.dry_run?) of an exchange this step refused, the refusal as the execution recorded it, with string keys ("type", "message", "retry_after_ms"), so the step refuses again and the preview shows what the exchange went through. Nil otherwise.

Whether the step runs in a pipeline test (Pipelines.TestRun), whose traffic is not the partner's: a step takes no real hold then, as in a dry run (context.dry_run?).

Types

outcome()

@type outcome() ::
  {:response, non_neg_integer() | nil, map() | [{String.t(), String.t()}]}
  | {:error, String.t()}
  | :not_sent

How the exchange ended for a hold: the response head as it reached After (headers as the exchange carries them, a map or a list of pairs), an upstream error by its type, or no response at all.

Functions

owner(context)

@spec owner(Alternera.Plugin.context()) :: pid()

The process the exchange belongs to, which outlives the task a step may run in: what a hold on a process (a monitor, a lock) should name as its owner, so that a connection that closes gives the hold back on its exit. The calling process when the host set none.

recorded_refusal(context)

@spec recorded_refusal(Alternera.Plugin.context()) :: map() | nil

In a preview (context.dry_run?) of an exchange this step refused, the refusal as the execution recorded it, with string keys ("type", "message", "retry_after_ms"), so the step refuses again and the preview shows what the exchange went through. Nil otherwise.

test_run?(context)

@spec test_run?(Alternera.Plugin.context()) :: boolean()

Whether the step runs in a pipeline test (Pipelines.TestRun), whose traffic is not the partner's: a step takes no real hold then, as in a dry run (context.dry_run?).