keep track of topics that have never been subscribed to

This commit is contained in:
Karmanyaah Malhotra 2023-12-17 16:58:06 -06:00
parent 072520eacc
commit 8f5213ab1a

View file

@ -19,11 +19,12 @@ const (
// topic represents a channel to which subscribers can subscribe, and publishers
// can publish a message
type topic struct {
ID string
subscribers map[int]*topicSubscriber
rateVisitor *visitor
lastAccess time.Time
mu sync.RWMutex
ID string
subscribers map[int]*topicSubscriber
rateVisitor *visitor
lastAccess time.Time
neverSubscribed bool
mu sync.RWMutex
}
type topicSubscriber struct {
@ -41,6 +42,7 @@ func newTopic(id string) *topic {
ID: id,
subscribers: make(map[int]*topicSubscriber),
lastAccess: time.Now(),
neverSubscribed: true,
}
}
@ -61,6 +63,7 @@ func (t *topic) Subscribe(s subscriber, userID string, cancel func()) (subscribe
cancel: cancel,
}
t.lastAccess = time.Now()
t.neverSubscribed = false
return subscriberID
}
@ -79,6 +82,12 @@ func (t *topic) LastAccess() time.Time {
return t.lastAccess
}
func (t *topic) NeverSubscribed() bool {
t.mu.RLock()
defer t.mu.RUnlock()
return t.neverSubscribed
}
func (t *topic) SetRateVisitor(v *visitor) {
t.mu.Lock()
defer t.mu.Unlock()