From 0e4044b7477c7083556d6097ef02ae6436cc110f Mon Sep 17 00:00:00 2001 From: Karmanyaah Malhotra Date: Tue, 21 Feb 2023 20:18:04 -0600 Subject: [PATCH] rename lastVisitor to vRate --- server/server.go | 2 +- server/topic.go | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/server/server.go b/server/server.go index a5307964..e0be8d41 100644 --- a/server/server.go +++ b/server/server.go @@ -1417,7 +1417,7 @@ func (s *Server) execManager() { if ev.IsTrace() { expiryMessage := "" if subs == 0 { - expiryTime := time.Until(t.lastVisitorExpires) + expiryTime := time.Until(t.vRateExpires) expiryMessage = ", expires in " + expiryTime.String() } ev.Trace("- topic %s: %d subscribers%s", t.ID, subs, expiryMessage) diff --git a/server/topic.go b/server/topic.go index b168c70f..31545340 100644 --- a/server/topic.go +++ b/server/topic.go @@ -11,11 +11,11 @@ import ( // topic represents a channel to which subscribers can subscribe, and publishers // can publish a message type topic struct { - ID string - subscribers map[int]*topicSubscriber - lastVisitor *visitor - lastVisitorExpires time.Time - mu sync.Mutex + ID string + subscribers map[int]*topicSubscriber + vRate *visitor + vRateExpires time.Time + mu sync.Mutex } type topicSubscriber struct { @@ -49,9 +49,9 @@ func (t *topic) Subscribe(s subscriber, visitor *visitor, cancel func(), subscri } // if no subscriber is already handling the rate limit - if t.lastVisitor == nil && subscriberRateLimit { - t.lastVisitor = visitor - t.lastVisitorExpires = time.Time{} + if t.vRate == nil && subscriberRateLimit { + t.vRate = visitor + t.vRateExpires = time.Time{} } return subscriberID @@ -61,16 +61,16 @@ func (t *topic) Stale() bool { t.mu.Lock() defer t.mu.Unlock() // if Time is initialized (not the zero value) and the expiry time has passed - if !t.lastVisitorExpires.IsZero() && t.lastVisitorExpires.Before(time.Now()) { - t.lastVisitor = nil + if !t.vRateExpires.IsZero() && t.vRateExpires.Before(time.Now()) { + t.vRate = nil } - return len(t.subscribers) == 0 && t.lastVisitor == nil + return len(t.subscribers) == 0 && t.vRate == nil } func (t *topic) Billee() *visitor { t.mu.Lock() defer t.mu.Unlock() - return t.lastVisitor + return t.vRate } // Unsubscribe removes the subscription from the list of subscribers @@ -84,16 +84,16 @@ func (t *topic) Unsubscribe(id int) { // look for an active subscriber (in random order) that wants to handle the rate limit for _, v := range t.subscribers { if v.subscriberRateLimit { - t.lastVisitor = v.visitor - t.lastVisitorExpires = time.Time{} + t.vRate = v.visitor + t.vRateExpires = time.Time{} return } } // if no active subscriber is found, count it towards the leaving subscriber if deletingSub.subscriberRateLimit { - t.lastVisitor = deletingSub.visitor - t.lastVisitorExpires = time.Now().Add(subscriberBilledValidity) + t.vRate = deletingSub.visitor + t.vRateExpires = time.Now().Add(subscriberBilledValidity) } }