Skip to content

feat: event-driven tenant discovery in MultiTenantConsumer#398

Merged
jeffersonrodrigues92 merged 1 commit intodevelopfrom
feat/event-driven-tenant-discovery
Mar 25, 2026
Merged

feat: event-driven tenant discovery in MultiTenantConsumer#398
jeffersonrodrigues92 merged 1 commit intodevelopfrom
feat/event-driven-tenant-discovery

Conversation

@jeffersonrodrigues92
Copy link
Contributor

Summary

  • Replace polling-based tenant discovery with Redis Pub/Sub + lazy-load
  • Consumer pods boot with empty tenant map, subscribe to tenant-events:*, lazy-load on first request
  • All state changes arrive via Redis Pub/Sub events — polling eliminated entirely
  • 12 event types handled (tenant lifecycle + service association + credentials + connections)

Changes

New package: commons/tenant-manager/event/

New components in consumer/

  • TTL cache (multi_tenant_cache.go) — 12h default, lazy eviction, thread-safe
  • Lazy-load (multi_tenant_lazy.go) — on-demand tenant fetch with per-tenant mutex
  • Event dispatch (multi_tenant_events.go + _handlers.go) — 12 handlers with service filtering, jitter, full TenantConfig population

Breaking changes

  • NewMultiTenantConsumerWithError() now requires redisClient parameter
  • WithRedisClient() option removed (now constructor param)
  • Deleted all polling code: syncActiveTenants(), discoverTenants(), etc.
  • Deleted multi_tenant_sync.go, multi_tenant_revalidate.go

Fixes (from review feedback)

  • Aligned JSON payload structs with publisher contract (modules as []string, secret_paths as nested map, messaging_config with flat rabbitmq_secret_path)
  • handleServiceAssociated / handleServiceReactivated now populate full TenantConfig (ConnectionSettings, Databases, Messaging)
  • Close() stops event listener outside c.mu lock to prevent deadlock

Test plan

  • All 15 tenant-manager packages pass with -race
  • ~60+ new test cases covering event types, listener, cache, lazy-load, dispatch, Run/Close
  • Lint: 0 issues
  • No panic() in production code
  • Integration test with real Redis Pub/Sub (future PR)

Closes #396

🤖 Generated with Claude Code

Close() held c.mu while calling eventListener.Stop(), which waits for
the listener goroutine to finish. That goroutine may be executing an
event handler that acquires c.mu, causing deadlock.

Fix: snapshot the listener reference under the lock, release the lock,
stop the listener, then re-acquire for the rest of cleanup.

X-Lerian-Ref: 0x1
@coderabbitai
Copy link

coderabbitai bot commented Mar 25, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 6569082d-fd69-400d-8568-035f8f5b90b2

📥 Commits

Reviewing files that changed from the base of the PR and between 7f9fac0 and 14cd0e6.

📒 Files selected for processing (1)
  • commons/tenant-manager/consumer/multi_tenant.go

Walkthrough

The MultiTenantConsumer.Close() method's synchronization pattern has been refactored to reduce lock contention. Previously, the entire method held the c.mu mutex via defer unlock. Now, the mutex is acquired and released in two separate blocks: first to atomically set c.closed=true and snapshot c.eventListener, then released before calling listener.Stop(). After the listener stops, the mutex is re-acquired with defer unlock to perform tenant-context cancellation, clear internal maps (c.tenants, c.knownTenants), and optionally close pmClient. This change eliminates holding the lock during the potentially blocking listener.Stop() call while maintaining correctness for subsequent cleanup operations.


Comment @coderabbitai help to get the list of available commands and usage tips.

@jeffersonrodrigues92 jeffersonrodrigues92 merged commit 46b841e into develop Mar 25, 2026
1 of 3 checks passed
@jeffersonrodrigues92 jeffersonrodrigues92 deleted the feat/event-driven-tenant-discovery branch March 25, 2026 15:16
jeffersonrodrigues92 added a commit that referenced this pull request Mar 25, 2026
…enant-discovery"

This reverts commit 46b841e, reversing
changes made to 7f9fac0.
jeffersonrodrigues92 added a commit that referenced this pull request Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant