View Source IdempotencyPlug.RequestTracker (TestServer v0.1.0)

GenServer that tracks processes to ensure requests, at most, are processed once.

storage

Storage

First-time tracked request will store {:processing, {node, pid}} request state with an expiration date for the the provided request ID. Once the request has completed, put_response/3 must be called to update the cached response. The response will be stored as {:ok, data} with an expiration date.

The process for a tracked request may halt unexpectedly (e.g. due to raised exception). This module will track the terminated process and store the value as {:halted, reason}.

All cached responses will be removed after 24 hours.

lookup

Lookup

For subsequent requests the state of the first-time tracked request will be returned in the format of {:cache, {:ok, data}, expires_at}.

If the request payload fingerprint differs, {:mismatch, {:fingerprint, fingerprint}, expires_at} is returned.

If first-time request hasn't yet completed, {:processing, {node, pid}, expires_at} is returned.

If the request unexpectedly terminated {:cache, {:halted, reason}, expires_at} is returned.

options

Options

  • :cache_ttl - the TTL in miliseconds for any objects in the cache store. Defaults to 24 hours.

  • :prune - the interval in miliseconds to prune the cache store for expired objects. Defaults to 60 seconds.

  • :store - the cache store module to use to store the cache objects. Defaults to {IdempotencyPlug.ETSStore, [table: Elixir.IdempotencyPlug.RequestTracker]}.

examples

Examples

children = [
  {
    IdempotencyPlug.RequestTracker,
      cache_ttl: :timer.hours(6),
      prune: :timer.minutes(1),
      store: {IdempotencyPlug.EctoStore, repo: MyApp.Repo}
  }
]

Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Updates the state for a given request ID.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

put_response(name_or_pid, request_id, response)

View Source
@spec put_response(atom() | pid(), binary(), any()) ::
  {:ok, DateTime.t()} | {:error, term()}

Updates the state for a given request ID.

Link to this function

track(name_or_pid, request_id, fingerprint)

View Source
@spec track(atom() | pid(), binary(), binary()) ::
  {:error, term()}
  | {:init, binary(), DateTime.t()}
  | {:mismatch, {:fingerprint, binary()}, DateTime.t()}
  | {:processing, {atom(), pid()}, DateTime.t()}
  | {:cache, {:ok, any()}, DateTime.t()}
  | {:cache, {:halted, term()}, DateTime.t()}

Tracks a request ID.

This function will return {:init, id, expires_at} for first-time request. Subsequent requests will return the request state. If the request payload fingerprint differs from what was stored, an error is returned.