Store.APIUsage (fnord v0.9.29)
View SourceModule for recording and checking API usage data. Cross OS process coordination is handled with regard to file reads/writes. It is up to the caller to ensure that requests are internally ordered for consistency.
Summary
Functions
Checks if a request can be made for the given model based on the last
recorded usage data for the model. If a request can be made, returns :ok.
Otherwise, returns {:wait, milliseconds} indicating how long to wait before
attempting another request.
Records API usage for a provided model. If model is nil, returns http_result unchanged. For 2xx {:ok, %{headers: headers, status: code}} results and a binary model, records usage data and updates the store. Other responses or errors are returned unchanged.
Returns the path to the usage store file.
Types
@type model_usage() :: %{ updated_at: non_neg_integer(), requests_max: non_neg_integer(), requests_left: non_neg_integer(), requests_reset: non_neg_integer(), tokens_max: non_neg_integer(), tokens_left: non_neg_integer(), tokens_reset: non_neg_integer() }
@type status() :: non_neg_integer()
@type usage() :: %{optional(binary()) => model_usage()}
Functions
@spec check(binary()) :: :ok | {:wait, non_neg_integer()} | {:error, term()}
Checks if a request can be made for the given model based on the last
recorded usage data for the model. If a request can be made, returns :ok.
Otherwise, returns {:wait, milliseconds} indicating how long to wait before
attempting another request.
@spec record_for_model(binary() | nil, http_result()) :: http_result()
Records API usage for a provided model. If model is nil, returns http_result unchanged. For 2xx {:ok, %{headers: headers, status: code}} results and a binary model, records usage data and updates the store. Other responses or errors are returned unchanged.
@spec store_path() :: binary()
Returns the path to the usage store file.