Skip to content

Commit

Permalink
feat: PubMatcher returns true if message should be sent to sub
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Aug 31, 2024
1 parent 4e7bff1 commit 7c98275
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type PubSub interface {
Sub(l *Subscriber) error
UnSub(l *Subscriber) error
Pub(msg *Msg) error
// return true if message should be sent to this subscriber
PubMatcher(msg *Msg, sub *Subscriber) bool
}

type PubSubMulticast struct {
Expand Down Expand Up @@ -64,14 +66,18 @@ func (b *PubSubMulticast) UnSub(rm *Subscriber) error {
return nil
}

func (b *PubSubMulticast) PubMatcher(msg *Msg, sub *Subscriber) bool {
return msg.Name == sub.Name
}

func (b *PubSubMulticast) Pub(msg *Msg) error {
log := b.Logger.With("channel", msg.Name)
log.Info("pub")

matches := []*Subscriber{}
writers := []io.Writer{}
for _, sub := range b.subs {
if sub.Name == msg.Name {
if b.PubMatcher(msg, sub) {
matches = append(matches, sub)
writers = append(writers, sub.Session)
}
Expand Down

0 comments on commit 7c98275

Please sign in to comment.