Alternera.Plugin.Body (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

A body a pipeline holds, as the codecs hand it to the steps: in memory ({:inline, binary}) or spooled to a file in the body storage directory ({:file, path, size}, Proxy.BodySpool).

The plugin callbacks keep taking and returning binaries: the runner reads a file-backed body inside the step's own process (Pipelines.Runner), so the exchange's process never holds it. A body no step changed goes on as the handle it came as.

Whoever spooled a body releases it (release/1) once the exchange no longer needs it.

Summary

Functions

Whether body is spooled to a file.

The handle for a Proxy.BodyTee.finalize/1 result. A failed file write is {:error, :spool_failed}; a capture that kept only part of the body (truncated or discarded) is {:error, :incomplete}; a capped file is removed with it.

Whether term is a body handle (not a bare binary).

A binary or a body handle: what wrap/1 takes.

Refreshes the modification time of a held file a reader is still sending, at most once a minute: TempFileCleanup takes a held file by its age, so a body written long ago and sent slowly (a client reading a large response at its own pace) must not look orphaned. touched_at is the monotonic time (ms) this returned last, nil before the first read, which touches at once. Returns the time of the last touch.

A second name for a file body (a hard link beside it, same filesystem, no copy), so a reader that outlives the body's owner keeps the bytes after the owner's release/1: the recording of a rewritten body (Streaming.Recording). An inline body is returned as it is.

The whole body as a binary. Raises File.Error when a file body cannot be read: a spooled body that is gone is a bug in its owner's release. The caller bounds the read by checking size/1 against its cap first.

At most max bytes from the start of the body.

Removes a file body's file; a no-op for an inline body, nil and a file already gone. Any other failure is logged and :ok all the same: the TempFileCleanup sweep is the safety net.

The body's size in bytes.

The body in chunks of chunk_bytes (64 KiB by default), the last one shorter; nothing for an empty body. A file is read as the stream is consumed, and kept fresh while it is (keep_fresh/2).

body as a handle: a binary becomes inline; a handle stays as it is.

Types

t()

@type t() :: {:inline, binary()} | {:file, Path.t(), non_neg_integer()}

Functions

file?(arg1)

@spec file?(term()) :: boolean()

Whether body is spooled to a file.

from_tee(arg)

@spec from_tee(
  {:inline, binary(), non_neg_integer(), boolean()}
  | {:file, String.t(), non_neg_integer()}
  | {:file_truncated, String.t(), non_neg_integer()}
  | {:discarded, non_neg_integer()}
  | {:capture_failed, non_neg_integer()}
) :: {:ok, t()} | {:error, :spool_failed | :incomplete}

The handle for a Proxy.BodyTee.finalize/1 result. A failed file write is {:error, :spool_failed}; a capture that kept only part of the body (truncated or discarded) is {:error, :incomplete}; a capped file is removed with it.

handle?(arg1)

@spec handle?(term()) :: boolean()

Whether term is a body handle (not a bare binary).

is_body(term)

(macro)

A binary or a body handle: what wrap/1 takes.

keep_fresh(path, touched_at)

@spec keep_fresh(Path.t(), integer() | nil) :: integer()

Refreshes the modification time of a held file a reader is still sending, at most once a minute: TempFileCleanup takes a held file by its age, so a body written long ago and sent slowly (a client reading a large response at its own pace) must not look orphaned. touched_at is the monotonic time (ms) this returned last, nil before the first read, which touches at once. Returns the time of the last touch.

link(body)

@spec link(t()) :: {:ok, t()} | {:error, File.posix()}

A second name for a file body (a hard link beside it, same filesystem, no copy), so a reader that outlives the body's owner keeps the bytes after the owner's release/1: the recording of a rewritten body (Streaming.Recording). An inline body is returned as it is.

read(arg)

@spec read(t()) :: binary()

The whole body as a binary. Raises File.Error when a file body cannot be read: a spooled body that is gone is a bug in its owner's release. The caller bounds the read by checking size/1 against its cap first.

read_prefix(arg, max)

@spec read_prefix(t(), non_neg_integer()) :: binary()

At most max bytes from the start of the body.

release(arg1)

@spec release(t() | nil) :: :ok

Removes a file body's file; a no-op for an inline body, nil and a file already gone. Any other failure is logged and :ok all the same: the TempFileCleanup sweep is the safety net.

size(arg)

@spec size(t()) :: non_neg_integer()

The body's size in bytes.

stream(body, chunk_bytes \\ 65536)

@spec stream(t(), pos_integer()) :: Enumerable.t()

The body in chunks of chunk_bytes (64 KiB by default), the last one shorter; nothing for an empty body. A file is read as the stream is consumed, and kept fresh while it is (keep_fresh/2).

wrap(body)

@spec wrap(binary() | t()) :: t()

body as a handle: a binary becomes inline; a handle stays as it is.