Alternera. Plugin. Provisional. ResponseCache behaviour
(alternera_plugin_sdk v0.9.0)
Copy Markdown
View Source
Provisional: added for the Response cache step, and a change to it needs a
changelog line but no new major (docs/USER_PLUGINS_DESIGN.md).
The host's response cache, for a holding step that caches the response of
the rest of the exchange (Plugins.ResponseCache): the one a cache around
Upstream is. The step holds; these are the capabilities it holds with,
each answered by the host (Alternera.Plugin.Host):
look_up/3, incall/3: whether the request is looked up, its key (the cache's own grammar, read against the whole exchange, so the step's signaturereads_any), the lookup and the single flight. A hit is an answer the step gives with{:respond, answer, [], summary}; a miss this exchange fills is afillthe step holds until its Close;keep/3, inleave/3at the Close: the response rules andstore_when, read against the exchange at the Close. A kept fill is a body observer (Plugin.BodyObserver, the host's) the step asks for with{:observe_body, observer, writes, summary}; it stores the response when its body has ended, or gives the fill up on any other end;give_up/1, inleave/3anywhere else: the exchange ended before the Close, and the flight's waiters run uncached.
A plugin is such a step when it exports cache_settings/1: the proxy reads
the step's cache from it (its key and store, for the key's warnings, a
Store step's invalidate and the preview's Clear), and treats the step as
a response cache wherever the pipeline has one.
look_up/3 reads these context keys besides the model's: cache_version
(the version of the pipeline's steps: a save that changes them leaves the
old entries behind), owner (the exchange's process, which leads a
flight), store_tenant (a pipeline test's private store),
test_run? and ignore_cache? (a preview that reads no entry).
Summary
Types
A response a hit answers with: its body a binary or a file (Plugin.Body).
What init/1 builds, the cache look_up/3 looks the request up in. key
is a template of the cache's own grammar (default_key/0 when the step
has none), skip_lookup_when and store_when are conditions as stored,
and limits keeps the keys the host knows (max_entries,
max_value_bytes, max_bytes, max_body_bytes,
spill_threshold_bytes). id, path and version are left as
new_config/1 sets them: look_up/3 names the cache by the step's
place and the pipeline's cache_version. key_template is key as
the host parsed it once, when the config was built
(Plugin.Host.response_cache_key_template/1), so a lookup does not
parse the key again.
What an outcome says of the cache: status ("hit", "miss" or
"bypass"), the key as shown ([secret] for a secret store), a hit's
age_s, a bypass's reason, skipped_write (why a miss wrote nothing),
ignored (a preview that read no entry), and at the Close kept.
Callbacks
The step's cache as a cache container's settings (string keys, as
stored: "key", "store", "ttl"…), from the step's stored settings.
Functions
The key a response cache uses without one of its own: the request's method, scheme, host, port, path and query.
Gives a fill up where the exchange ended before its Close: its waiters run uncached.
At the step's Close, with leave/3's context inputs (the exchange at
the Close) and the response head as it reached After (outcome's status
and headers): {:keep, observer, info}, a Plugin.BodyObserver that
stores the response when its body ends, or {:bypass, reason, info} when
the response rules or store_when refuse it (partial_content,
status, event_stream, set_cookie, store_when_false); the fill is
given up then.
The bare references a key may hold (${method}, ${query}…); body
only in a cache that waits for the body.
The kinds of dotted reference a key may hold, each followed by . and a
name or path: ${header.<name>}, ${var.<name>},
${steps.<id>.<output>}, ${input.<name>}, ${path.<name>} and
${body.<path>}.
Looks the request up in config, against the exchange inputs reads (a
step whose signature reads_any)
A config: fields merged over the defaults (key default_key/0, no
ttl, store, wait or limits, on_miss "run", and response rules that keep
none of authorized requests, Set-Cookie or the status header), with the
key parsed by the host (key_template).
Types
@type answer() :: %{ status: 100..599, headers: [{String.t(), String.t()}], body: binary() | Alternera.Plugin.Body.t(), trailers: [{String.t(), String.t()}], content_type: nil }
A response a hit answers with: its body a binary or a file (Plugin.Body).
@type config() :: %{ type: :cache, id: String.t() | nil, path: String.t() | nil, disabled: false, waits?: false, key: String.t(), key_template: term(), ttl: non_neg_integer() | nil, store: String.t() | nil, skip_lookup_when: [map()], store_when: [map()] | nil, wait_ms: non_neg_integer() | nil, limits: %{required(String.t()) => pos_integer()}, version: String.t(), dry_run?: boolean(), body_phase: nil, on_miss: String.t(), response: %{ statuses: [100..599], cache_authorized: boolean(), allow_set_cookie: boolean(), status_header: boolean() } }
What init/1 builds, the cache look_up/3 looks the request up in. key
is a template of the cache's own grammar (default_key/0 when the step
has none), skip_lookup_when and store_when are conditions as stored,
and limits keeps the keys the host knows (max_entries,
max_value_bytes, max_bytes, max_body_bytes,
spill_threshold_bytes). id, path and version are left as
new_config/1 sets them: look_up/3 names the cache by the step's
place and the pipeline's cache_version. key_template is key as
the host parsed it once, when the config was built
(Plugin.Host.response_cache_key_template/1), so a lookup does not
parse the key again.
What an outcome says of the cache: status ("hit", "miss" or
"bypass"), the key as shown ([secret] for a secret store), a hit's
age_s, a bypass's reason, skipped_write (why a miss wrote nothing),
ignored (a preview that read no entry), and at the Close kept.
Callbacks
Functions
@spec default_key() :: String.t()
The key a response cache uses without one of its own: the request's method, scheme, host, port, path and query.
@spec give_up(term()) :: :ok
Gives a fill up where the exchange ended before its Close: its waiters run uncached.
@spec keep(term(), Alternera.Plugin.Inputs.t(), {non_neg_integer() | nil, term()}) :: {:keep, {module(), term()}, info()} | {:bypass, String.t(), info()}
At the step's Close, with leave/3's context inputs (the exchange at
the Close) and the response head as it reached After (outcome's status
and headers): {:keep, observer, info}, a Plugin.BodyObserver that
stores the response when its body ends, or {:bypass, reason, info} when
the response rules or store_when refuse it (partial_content,
status, event_stream, set_cookie, store_when_false); the fill is
given up then.
@spec key_facts() :: [String.t()]
The bare references a key may hold (${method}, ${query}…); body
only in a cache that waits for the body.
@spec key_kinds() :: [String.t()]
The kinds of dotted reference a key may hold, each followed by . and a
name or path: ${header.<name>}, ${var.<name>},
${steps.<id>.<output>}, ${input.<name>}, ${path.<name>} and
${body.<path>}.
@spec look_up(config(), Alternera.Plugin.Inputs.t(), map()) :: {:hit, answer(), info()} | {:fill, term(), info()} | {:miss, String.t(), info()} | {:bypass, String.t(), info()}
Looks the request up in config, against the exchange inputs reads (a
step whose signature reads_any):
{:hit, answer, info}: a stored response, withAgeand, when the cache asks,X-Cache-Status: hit;{:fill, fill, info}: this exchange leads the flight for the key (or refills afterskip_lookup_when): holdfill, andkeep/3it at the Close orgive_up/1it;{:miss, reason, info}: nothing was found, and nothing fills: a preview, a HEAD,on_miss: "fail"(reason "miss"), or a flight that was abandoned or not waited for;{:bypass, reason, info}: not looked up (method,authorization,key_unresolved,test_run,unavailable,dry_runorheadafterskip_lookup_when).
A config: fields merged over the defaults (key default_key/0, no
ttl, store, wait or limits, on_miss "run", and response rules that keep
none of authorized requests, Set-Cookie or the status header), with the
key parsed by the host (key_template).