Skip to content

refactor: replace process-global runtime state with scoped dependencies #606

@abtreece

Description

@abtreece

Problem

Several important runtime dependencies are process-global mutable state rather than scoped dependencies.

Verified in:

  • pkg/log/log.go:22-25, where tag and logger are package globals.
  • pkg/log/log.go:112-179, where SetLevel and SetFormat mutate the global logger.
  • pkg/template/client_cache.go:14-17, where the per-resource backend client cache is a package-global map plus mutex.
  • pkg/template/client_cache.go:92-110, where shutdown closes all globally cached clients.

Why this is a problem

Global mutable state makes tests and runtime lifecycle harder to isolate. For logging, tests or components that mutate level/format affect the entire process. For backend clients, cache ownership is implicit: resource loading creates clients through a package-global cache, while shutdown later has to remember to call a package-global close function.

This is manageable today, but it becomes increasingly brittle as reload, per-resource backends, metrics wrapping, and tests grow.

Suggested refactor

Make runtime dependencies explicit:

  • Introduce a scoped logger or logging configuration object passed into runtime components where practical.
  • Introduce a ClientCache type owned by the top-level runtime/supervisor.
  • Pass the cache into template resource construction instead of calling package-global cache functions.
  • Keep package-level defaults only as thin compatibility shims if needed.

Acceptance criteria

  • Tests can create isolated logger/cache instances.
  • Per-resource backend clients are owned by a runtime-scoped cache.
  • Shutdown closes the runtime-owned cache instead of a package-global cache.
  • Existing behavior remains backward compatible for callers using default paths.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions