Skip to content

feat(plugins): add plugin key-value store #550

Closed

Description

@uniqueg

Part of #545.

Problem

Plugins have no storage beyond the current process (see #545), so caches aren't shared and counters (e.g. per-user API budgets) can't be enforced across workers or replicas.

Proposal

Add a small key-value store to the plugin context as ctx.store:

  • API: get(key), set(key, value, *, ttl=None), delete(key), and an atomic increment(key, by=1, *, ttl=None, limit=None) that returns the new value, or None without incrementing if it would exceed limit. On increment, the TTL is set only when the counter is created (fixed window).
  • Keys: strings of 1 to 256 characters; values: I-JSON, size checked as compact JSON.
  • Backend: one shared plugin_store collection in the app's MongoDB, _id = namespace + key, TTL index on expires_at; reads also filter out expired entries.
  • Namespace: the normalized (PEP 503) distribution name of the plugin; built-in plugins use foca.
  • In-memory fallback (per process) when no database is configured, and when the operator turns storage off for a plugin.
  • Operator config in plugins.defaults.store with per-plugin overrides in plugins.available.<id>.store: enabled, max_entry_size, max_ttl (entries without a TTL get it).
  • Calls bounded by the hook deadline; failures raise a new StoreError, which the engine treats like any other hook failure.
  • In-memory store for plugin tests in foca.plugins.testing.
  • Docs: plugin guide (including hashing principals before using them as keys) and configuration.

No namespace size limit in this issue (see the follow-up child issue).

Done when

  • ctx.store is available to plugins with both backends, configured and documented;
  • unit and integration tests (real MongoDB for TTL and atomic increments), 100% coverage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: lowtype: featureworkload: days

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions