From fb2fa4c478778ab0d09154b5d274ae37caa6a4b6 Mon Sep 17 00:00:00 2001 From: Karmanyaah Malhotra Date: Tue, 14 Feb 2023 14:00:43 -0600 Subject: [PATCH] Fix m.Expires and prune stale topics based on lastVisitorExpires --- server/server.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/server/server.go b/server/server.go index a22ed7ba..619e30cb 100644 --- a/server/server.go +++ b/server/server.go @@ -622,7 +622,7 @@ func (s *Server) handlePublishWithoutResponse(r *http.Request, v *visitor) (*mes } m.Sender = v.IP() m.User = v.MaybeUserID() - m.Expires = time.Now().Add(v.Limits().MessageExpiryDuration).Unix() + m.Expires = time.Unix(m.Time, 0).Add(v.Limits().MessageExpiryDuration).Unix() if err := s.handlePublishBody(r, v, m, body, unifiedpush); err != nil { return nil, err } @@ -666,6 +666,8 @@ func (s *Server) handlePublishWithoutResponse(r *http.Request, v *visitor) (*mes if err := s.messageCache.AddMessage(m); err != nil { return nil, err } + } else { + m.Expires = m.Time } u := v.User() if s.userManager != nil && u != nil && u.Tier != nil { @@ -1404,9 +1406,10 @@ func (s *Server) execManager() { defer s.mu.Unlock() for _, t := range s.topics { subs := t.SubscribersCount() - log.Tag(tagManager).Trace("- topic %s: %d subscribers", t.ID, subs) + expiryTime := time.Until(t.lastVisitorExpires) + log.Tag(tagManager).Trace("- topic %s: %d subscribers, expires in %s", t.ID, subs, expiryTime) msgs, exists := messageCounts[t.ID] - if subs == 0 && (!exists || msgs == 0) { + if t.Stale() && (!exists || msgs == 0) { log.Tag(tagManager).Trace("Deleting empty topic %s", t.ID) emptyTopics++ delete(s.topics, t.ID)