Skip to content

Refactor ateapi PostgreSQL pool ownership and OpenFGA dependency injection in main #1755

Closed

Description

Context

Follow-up to #1670 based on review feedback.

Currently, atepg.Connect dials and owns the primary PostgreSQL connection pool internally, and ateapi opens a separate dedicated *pgxpool.Pool (NewPool) for authz.NewServer because OpenFGA's postgres.Datastore.Close() closes the underlying pool. Additionally, authz.NewServer constructs stateful dependencies (datastore and fgaServer) inline rather than receiving them via dependency injection.

Goals

  1. Share a single primary PostgreSQL connection pool between atepg and OpenFGA:

    • Dial the *pgxpool.Pool in cmd/ateapi/main.go and manage its lifecycle at the top level (defer pool.Close()).
    • Inject the shared pool into both atepg and OpenFGA to avoid extra connection pool pressure on PostgreSQL.
    • Ensure shutdown ordering (defer LIFO in main.go) cleanly stops fgaServer and atepg background loops before closing the shared pool.
  2. Manage OpenFGA datastore and fgaServer lifecycles in main.go:

    • Expose initialization helpers in internal/authz (e.g., ApplyMigrations, store/model bootstrapping) rather than constructing and closing datastore and fgaServer inline inside authz.NewServer.
    • Use unconditional defer statements in cmd/ateapi/main.go for datastore.Close() and fgaServer.Close() so panics or startup errors cannot leak open resources.
  3. Simplify internal/authz wrapper struct:

    • Re-evaluate the authz.Server wrapper alongside control-plane API authorization call sites (Check, Write, Read) so it only encapsulates what callers need (storeID, modelID, and domain helpers).

Acceptance Criteria

  • cmd/ateapi/main.go dials the shared *pgxpool.Pool and injects it into atepg and OpenFGA.
  • atepg.Persistence.NewPool is removed in favor of the shared injected pool.
  • datastore and fgaServer lifecycles are managed via defer in main.go.
  • Unit and E2E tests pass with the shared pool configuration.

Metadata

Metadata

Labels

area/api-machinerykind/cleanupSmall fixes that are not bugs, for example a typo in a code comment

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions