Skip to content

Plugin host: per-plugin WASM instance pool with TTL + memory-pressure recycling #9

@tayebmokni

Description

@tayebmokni

Summary

Implement the per-plugin WASM instance pool. Pool size formula: min(maxInstances, currentConcurrency * 2), capped at 16 per plugin (tunable). Each instance is single-threaded; hook dispatch acquires, runs, returns it. Instances are recycled on shutdown, on a 1-hour TTL, or when memory hits 80% of cap.

Design reference

  • docs/02-plugin-system.md S4.6, S4.7, Appendix B (InstancePool)

Acceptance criteria

  • InstancePool struct with idle list, in-flight counter, sync.Mutex
  • Get(ctx) (*Instance, error) pops an available instance or instantiates a new one if len < max; blocks with ctx cancellation if at max
  • Put(inst) returns to available pool, OR destroys instance if its memory >80% of cap, OR if older than TTL
  • One CompiledModule per (slug, version) is reused across the entire pool
  • State inside an instance is allowed to persist across Get/Put cycles (in-instance caches survive)
  • Cold-start instantiation completes within 20ms for a small (<1MB) plugin in benchmark
  • Warm-dispatch overhead (Get -> empty hook_handler call -> Put) under 200µs in benchmark
  • Unit tests: concurrent Get from N goroutines never exceeds maxInstances; instances are returned to pool on success path; instances destroyed on memory threshold
  • Per-plugin metric: plugin_pool_size{slug}, plugin_pool_in_flight{slug}

Dependencies

#6 (plugin host scaffolding)

Complexity

L

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions