From f57d93ac7872d05fe84bf6bbf8d8d0e545e03e08 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 6 Sep 2024 19:37:34 -0300 Subject: [PATCH] after getting an EOSE we should stop checking since/until. --- filter.go | 33 +++++++++++++++++++++++++-------- relay.go | 1 + subscription.go | 2 ++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/filter.go b/filter.go index d9d86fe..e28ad5a 100644 --- a/filter.go +++ b/filter.go @@ -39,12 +39,37 @@ func (eff Filters) Match(event *Event) bool { return false } +func (eff Filters) MatchIgnoringTimestampConstraints(event *Event) bool { + for _, filter := range eff { + if filter.MatchesIgnoringTimestampConstraints(event) { + return true + } + } + return false +} + func (ef Filter) String() string { j, _ := easyjson.Marshal(ef) return string(j) } func (ef Filter) Matches(event *Event) bool { + if !ef.MatchesIgnoringTimestampConstraints(event) { + return false + } + + if ef.Since != nil && event.CreatedAt < *ef.Since { + return false + } + + if ef.Until != nil && event.CreatedAt > *ef.Until { + return false + } + + return true +} + +func (ef Filter) MatchesIgnoringTimestampConstraints(event *Event) bool { if event == nil { return false } @@ -67,14 +92,6 @@ func (ef Filter) Matches(event *Event) bool { } } - if ef.Since != nil && event.CreatedAt < *ef.Since { - return false - } - - if ef.Until != nil && event.CreatedAt > *ef.Until { - return false - } - return true } diff --git a/relay.go b/relay.go index 47b5bad..ed06a5f 100644 --- a/relay.go +++ b/relay.go @@ -397,6 +397,7 @@ func (r *Relay) PrepareSubscription(ctx context.Context, filters Filters, opts . EndOfStoredEvents: make(chan struct{}, 1), ClosedReason: make(chan string, 1), Filters: filters, + match: filters.Match, } for _, opt := range opts { diff --git a/subscription.go b/subscription.go index ec858d3..e1acc42 100644 --- a/subscription.go +++ b/subscription.go @@ -32,6 +32,7 @@ type Subscription struct { // Context will be .Done() when the subscription ends Context context.Context + match func(*Event) bool // this will be either Filters.Match or Filters.MatchIgnoringTimestampConstraints live atomic.Bool eosed atomic.Bool closed atomic.Bool @@ -104,6 +105,7 @@ func (sub *Subscription) dispatchEvent(evt *Event) { func (sub *Subscription) dispatchEose() { if sub.eosed.CompareAndSwap(false, true) { + sub.match = sub.Filters.MatchIgnoringTimestampConstraints go func() { sub.storedwg.Wait() sub.EndOfStoredEvents <- struct{}{}