Alternera.Plugin.BodyObserver behaviour (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

What sees a response body as it leaves, read-only: the module a holding step's leave/3 names in {:observe_body, {module, state}, summary} at its Close (Plugin.CallAdapter).

The door calls open/2 with the upstream's head as it arrived, before the response steps changed it (its header names in their own case), then chunk/2 for each piece the client is sent, then exactly one of finish/2, with the trailers, on a clean end, or abort/2, with why, on any other: a client gone, an upstream that broke off, an exchange that ended before the door opened the body ("abandoned"). For an answer made on the request side the body is fed whole, after open/2 with that answer's head as it reached the Close. Nothing the client gets depends on an observer: one that raises is logged and dropped.

The end is called at most once, from whichever process ends the exchange, so an observer that holds something outside its state (a single flight, a file) gives it back in either.

Summary

Types

A response head: its status and its headers, in order.

Callbacks

The body did not end cleanly, for reason.

A piece of the body as the client is sent it.

The body ended cleanly; trailers are the upstream's.

The upstream's head as it arrived (or a request-side answer's at the Close), before any piece of the body.

Types

head()

@type head() :: %{status: 100..599, headers: [{String.t(), String.t()}]}

A response head: its status and its headers, in order.

Callbacks

abort(reason, state)

@callback abort(reason :: String.t(), state :: term()) :: term()

The body did not end cleanly, for reason.

chunk(binary, state)

@callback chunk(binary(), state :: term()) :: {:ok, term()}

A piece of the body as the client is sent it.

finish(trailers, state)

@callback finish(trailers :: [{String.t(), String.t()}], state :: term()) :: term()

The body ended cleanly; trailers are the upstream's.

open(head, state)

(optional)
@callback open(head(), state :: term()) :: {:ok, term()}

The upstream's head as it arrived (or a request-side answer's at the Close), before any piece of the body.