[Issue #20] Add wildcard namespace subscriptions #52
Reference in New Issue
Block a user
Delete Branch "issue-20-wildcard-namespace-subscriptions"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Add support for NATS-style wildcard patterns in namespace subscriptions, enabling cross-namespace event handling for logging, monitoring, and auditing use cases.
Changes
pattern.gowithMatchNamespacePatternandIsWildcardPatternfunctionsEventBusto handle wildcard subscribers separately from exact-match subscribersNATSEventBusto leverage NATS native wildcard support for cross-node broadcastingpattern_test.go) and EventBus wildcards (eventbus_test.go)Pattern Syntax
Following NATS subject matching conventions:
*matches exactly one token (dot-separated)>matches one or more tokens (only at the end of a pattern)Examples:
prod.*matchesprod.tenant,prod.ordersprod.>matchesprod.tenant,prod.tenant.orders>matches all namespaces (catch-all)Security Considerations
Wildcard subscriptions bypass namespace isolation. This is documented with warnings in:
EventBroadcasterinterfaceEventBusstruct andSubscribemethodNATSEventBusstruct andSubscribemethodMatchNamespacePatternfunctionCloses #20
Support NATS-style wildcard patterns ("*" and ">") for subscribing to events across multiple namespaces. This enables cross-cutting concerns like logging, monitoring, and auditing without requiring separate subscriptions for each namespace. - Add pattern.go with MatchNamespacePattern and IsWildcardPattern - Update EventBus to track wildcard subscribers separately - Update NATSEventBus to use NATS native wildcard support - Add comprehensive tests for pattern matching and EventBus wildcards - Document security implications in all relevant code comments Closes #20 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>AI Code Review
Summary
This PR adds NATS-style wildcard pattern support for namespace subscriptions, enabling cross-namespace event handling for logging, monitoring, and auditing. The implementation is well-structured with comprehensive test coverage and appropriate security documentation.
Findings
Code Quality
subscriptionstruct and split betweenexactSubscribersandwildcardSubscribersis clean and maintainablematchTokensfunction inpattern.gois easy to follownats_eventbus.goline ~799, thedeliverToWildcardSubscribersfunction directly accessesneb.EventBus.wildcardSubscribers. While this works with embedding, it creates tight coupling. Consider adding a method to EventBus for this, though not blocking.Potential Bugs
Unsubscribe, but if the same channel is passed twice, the second call will panic on double-close. Current usage appears safe, but worth noting.>placement: The pattern matching allows>anywhere in the pattern string during parsing (e.g.,prod.>.tenantwould be split into tokens). However, the matching logic only handles>correctly when it's the last pattern token. Consider adding validation inSubscribeto reject invalid patterns likeprod.>.tenantearly.Security Concerns
Style Notes
Test Coverage
NATSEventBuswildcard behavior across nodes. The local EventBus tests are thorough, but validating NATS subject wildcard integration would strengthen confidence.Verdict
LGTM with minor suggestions
This is a solid implementation with excellent test coverage and thorough documentation. The pattern matching is correct, the EventBus refactoring maintains backward compatibility, and the security implications are well-documented. The minor observations about
>validation and cross-node integration testing are suggestions for future improvement, not blockers.The code is ready to merge.