[Issue #21] Add namespace event filtering #51

Closed
HugoNijhuis wants to merge 1 commits from issue-21-namespace-event-filtering into main
Owner

Summary

Add support for filtering events by type and actor pattern within namespace subscriptions.

Changes

  • Added SubscriptionFilter type with EventTypes and ActorPattern fields
  • Added SubscribeWithFilter method to EventBus for client-side filtering
  • Added SubscribeWithFilter method to NATSEventBus with server-side NATS subject filtering
  • Updated EventBroadcaster interface to include SubscribeWithFilter
  • Added comprehensive unit tests for filtering functionality
  • Documented filter syntax with examples in code comments

Implementation Notes

Event Type Filtering

Subscribers can specify which event types to receive:

filter := SubscriptionFilter{
    EventTypes: []string{"OrderPlaced", "OrderShipped"},
}
ch := bus.SubscribeWithFilter("namespace", filter)

Actor Pattern Filtering

Supports prefix matching with wildcard:

filter := SubscriptionFilter{
    ActorPattern: "order-*", // matches "order-123", "order-456", etc.
}

Server-Side Filtering

For NATSEventBus, single event type filters use NATS subject-based routing for efficiency. Multiple event types or actor patterns are filtered client-side.

AND Logic

When both filters are specified, events must match all criteria.

Closes #21

## Summary Add support for filtering events by type and actor pattern within namespace subscriptions. ## Changes - Added `SubscriptionFilter` type with `EventTypes` and `ActorPattern` fields - Added `SubscribeWithFilter` method to `EventBus` for client-side filtering - Added `SubscribeWithFilter` method to `NATSEventBus` with server-side NATS subject filtering - Updated `EventBroadcaster` interface to include `SubscribeWithFilter` - Added comprehensive unit tests for filtering functionality - Documented filter syntax with examples in code comments ## Implementation Notes ### Event Type Filtering Subscribers can specify which event types to receive: ```go filter := SubscriptionFilter{ EventTypes: []string{"OrderPlaced", "OrderShipped"}, } ch := bus.SubscribeWithFilter("namespace", filter) ``` ### Actor Pattern Filtering Supports prefix matching with wildcard: ```go filter := SubscriptionFilter{ ActorPattern: "order-*", // matches "order-123", "order-456", etc. } ``` ### Server-Side Filtering For NATSEventBus, single event type filters use NATS subject-based routing for efficiency. Multiple event types or actor patterns are filtered client-side. ### AND Logic When both filters are specified, events must match all criteria. Closes #21
HugoNijhuis added 1 commit 2026-01-10 18:24:38 +00:00
Add namespace event filtering support
All checks were successful
CI / build (pull_request) Successful in 21s
a8787877f0
Add SubscriptionFilter type and SubscribeWithFilter method to enable
filtering events by type and actor pattern within namespace subscriptions.

- SubscriptionFilter supports event type filtering (e.g., only "OrderPlaced")
- SubscriptionFilter supports actor ID prefix patterns (e.g., "order-*")
- Filters are combined with AND logic
- NATSEventBus uses NATS subjects for server-side filtering when possible
- Comprehensive test coverage for filtering functionality

Closes #21

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Author
Owner

AI Code Review

This is an automated review generated by the code-reviewer agent.

Summary

PR adds namespace event filtering with event type and actor pattern support. Implementation is well-structured with comprehensive tests. Server-side NATS filtering for single event types is a smart optimization. Found one critical bug in NATS subscription management and several areas for improvement.

Findings

Code Quality

  • Excellent documentation with clear examples
  • Well-structured filter logic
  • Comprehensive unit test coverage (948 lines)
  • Smart server-side filtering optimization

Issues:

  1. nats_eventbus.go:71-72 Subscription management tracks count per namespace but not which subjects subscribed. Different filters will cause missing events.
  2. eventbus.go:85-98 Wildcard check uses HasSuffix on every match. Consider pre-parsing patterns.

Potential Bugs

CRITICAL nats_eventbus.go:71-108 Subscription management broken. Example: First subscriber with EventTypes:[OrderPlaced] creates NATS sub. Second subscriber with EventTypes:[UserCreated] sees count>0, skips NATS subscription. Second subscriber NEVER receives events. Fix: Track subscriptions per subject not per namespace.

BREAKING CHANGE nats_eventbus.go:161 Subject format changed from aether.events.{ns} to aether.events.{ns}.{type}. Old subscribers incompatible. Needs migration path.

Test Coverage

Excellent unit tests but missing NATSEventBus integration tests and subscription management tests.

Verdict

BLOCKING ISSUES

Critical bug will cause lost events. Subject change breaks compatibility.

Recommendations: Fix subscription tracking with map[subject]*nats.Subscription. Add NATSEventBus integration tests. Document migration path.

## AI Code Review This is an automated review generated by the code-reviewer agent. ### Summary PR adds namespace event filtering with event type and actor pattern support. Implementation is well-structured with comprehensive tests. Server-side NATS filtering for single event types is a smart optimization. Found one critical bug in NATS subscription management and several areas for improvement. ### Findings #### Code Quality - Excellent documentation with clear examples - Well-structured filter logic - Comprehensive unit test coverage (948 lines) - Smart server-side filtering optimization Issues: 1. nats_eventbus.go:71-72 Subscription management tracks count per namespace but not which subjects subscribed. Different filters will cause missing events. 2. eventbus.go:85-98 Wildcard check uses HasSuffix on every match. Consider pre-parsing patterns. #### Potential Bugs CRITICAL nats_eventbus.go:71-108 Subscription management broken. Example: First subscriber with EventTypes:[OrderPlaced] creates NATS sub. Second subscriber with EventTypes:[UserCreated] sees count>0, skips NATS subscription. Second subscriber NEVER receives events. Fix: Track subscriptions per subject not per namespace. BREAKING CHANGE nats_eventbus.go:161 Subject format changed from aether.events.{ns} to aether.events.{ns}.{type}. Old subscribers incompatible. Needs migration path. #### Test Coverage Excellent unit tests but missing NATSEventBus integration tests and subscription management tests. ### Verdict BLOCKING ISSUES Critical bug will cause lost events. Subject change breaks compatibility. Recommendations: Fix subscription tracking with map[subject]*nats.Subscription. Add NATSEventBus integration tests. Document migration path.
Author
Owner

This PR's implementation conflicts with the wildcard subscription support merged in PR #52. The filtering feature needs to be redesigned to:

  1. Work with wildcard patterns (the current implementation removed wildcard support)
  2. Fix the NATS subscription tracking bug (tracks per namespace, not per subject)
  3. Integrate with the metrics system (PR #49)

Closing this PR. The filtering feature should be re-implemented in a new PR that builds on the current main branch with wildcards and metrics support.

This PR's implementation conflicts with the wildcard subscription support merged in PR #52. The filtering feature needs to be redesigned to: 1. Work with wildcard patterns (the current implementation removed wildcard support) 2. Fix the NATS subscription tracking bug (tracks per namespace, not per subject) 3. Integrate with the metrics system (PR #49) Closing this PR. The filtering feature should be re-implemented in a new PR that builds on the current main branch with wildcards and metrics support.
HugoNijhuis closed this pull request 2026-01-10 18:52:11 +00:00
All checks were successful
CI / build (pull_request) Successful in 21s

Pull request closed

Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: flowmade-one/aether#51