| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"sync" |
| 6 |
"time" |
| 7 |
|
| 8 |
"heckel.io/ntfy/v2/log" |
| 9 |
"heckel.io/ntfy/v2/model" |
| 10 |
"heckel.io/ntfy/v2/util" |
| 11 |
) |
| 12 |
|
| 13 |
const ( |
| 14 |
// topicExpungeAfter defines how long a topic is active before it is removed from memory. |
| 15 |
// This must be larger than matrixRejectPushKeyForUnifiedPushTopicWithoutRateVisitorAfter to give |
| 16 |
// time for more requests to come in, so that we can send a {"rejected":["<pushkey>"]} response back. |
| 17 |
topicExpungeAfter = 16 * time.Hour |
| 18 |
) |
| 19 |
|
| 20 |
// topic represents a channel to which subscribers can subscribe, and publishers |
| 21 |
// can publish a message |
| 22 |
type topic struct { |
| 23 |
ID string |
| 24 |
subscribers map[int]*topicSubscriber |
| 25 |
rateVisitor *visitor |
| 26 |
lastAccess time.Time |
| 27 |
mu sync.RWMutex |
| 28 |
} |
| 29 |
|
| 30 |
type topicSubscriber struct { |
| 31 |
userID string // User ID associated with this subscription, may be empty |
| 32 |
subscriber subscriber |
| 33 |
cancel func() |
| 34 |
} |
| 35 |
|
| 36 |
// subscriber is a function that is called for every new message on a topic |
| 37 |
type subscriber func(v *visitor, msg *model.Message) error |
| 38 |
|
| 39 |
// newTopic creates a new topic |
| 40 |
func newTopic(id string) *topic { |
| 41 |
return &topic{ |
| 42 |
ID: id, |
| 43 |
subscribers: make(map[int]*topicSubscriber), |
| 44 |
lastAccess: time.Now(), |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// Subscribe subscribes to this topic |
| 49 |
func (t *topic) Subscribe(s subscriber, userID string, cancel func()) (subscriberID int) { |
| 50 |
t.mu.Lock() |
| 51 |
defer t.mu.Unlock() |
| 52 |
for i := 0; i < 5; i++ { // Best effort retry |
| 53 |
subscriberID = rand.Int() |
| 54 |
_, exists := t.subscribers[subscriberID] |
| 55 |
if !exists { |
| 56 |
break |
| 57 |
} |
| 58 |
} |
| 59 |
t.subscribers[subscriberID] = &topicSubscriber{ |
| 60 |
userID: userID, // May be empty |
| 61 |
subscriber: s, |
| 62 |
cancel: cancel, |
| 63 |
} |
| 64 |
t.lastAccess = time.Now() |
| 65 |
return subscriberID |
| 66 |
} |
| 67 |
|
| 68 |
func (t *topic) Stale() bool { |
| 69 |
t.mu.Lock() |
| 70 |
defer t.mu.Unlock() |
| 71 |
if t.rateVisitor != nil && !t.rateVisitor.Stale() { |
| 72 |
return false |
| 73 |
} |
| 74 |
return len(t.subscribers) == 0 && time.Since(t.lastAccess) > topicExpungeAfter |
| 75 |
} |
| 76 |
|
| 77 |
func (t *topic) LastAccess() time.Time { |
| 78 |
t.mu.RLock() |
| 79 |
defer t.mu.RUnlock() |
| 80 |
return t.lastAccess |
| 81 |
} |
| 82 |
|
| 83 |
func (t *topic) SetRateVisitor(v *visitor) { |
| 84 |
t.mu.Lock() |
| 85 |
defer t.mu.Unlock() |
| 86 |
t.rateVisitor = v |
| 87 |
t.lastAccess = time.Now() |
| 88 |
} |
| 89 |
|
| 90 |
func (t *topic) RateVisitor() *visitor { |
| 91 |
t.mu.Lock() |
| 92 |
defer t.mu.Unlock() |
| 93 |
if t.rateVisitor != nil && t.rateVisitor.Stale() { |
| 94 |
t.rateVisitor = nil |
| 95 |
} |
| 96 |
return t.rateVisitor |
| 97 |
} |
| 98 |
|
| 99 |
// Unsubscribe removes the subscription from the list of subscribers |
| 100 |
func (t *topic) Unsubscribe(id int) { |
| 101 |
t.mu.Lock() |
| 102 |
defer t.mu.Unlock() |
| 103 |
delete(t.subscribers, id) |
| 104 |
} |
| 105 |
|
| 106 |
// Publish asynchronously publishes to all subscribers |
| 107 |
func (t *topic) Publish(v *visitor, m *model.Message) error { |
| 108 |
go func() { |
| 109 |
// We want to lock the topic as short as possible, so we make a shallow copy of the |
| 110 |
// subscribers map here. Actually sending out the messages then doesn't have to lock. |
| 111 |
subscribers := t.subscribersCopy() |
| 112 |
if len(subscribers) > 0 { |
| 113 |
logvm(v, m).Tag(tagPublish).Debug("Forwarding to %d subscriber(s)", len(subscribers)) |
| 114 |
for _, s := range subscribers { |
| 115 |
// We call the subscriber functions in their own Go routines because they are blocking, and |
| 116 |
// we don't want individual slow subscribers to be able to block others. |
| 117 |
go func(s subscriber) { |
| 118 |
if err := s(v, m); err != nil { |
| 119 |
logvm(v, m).Tag(tagPublish).Err(err).Warn("Error forwarding to subscriber") |
| 120 |
} |
| 121 |
}(s.subscriber) |
| 122 |
} |
| 123 |
} else { |
| 124 |
logvm(v, m).Tag(tagPublish).Trace("No stream or WebSocket subscribers, not forwarding") |
| 125 |
} |
| 126 |
t.Keepalive() |
| 127 |
}() |
| 128 |
return nil |
| 129 |
} |
| 130 |
|
| 131 |
// Stats returns the number of subscribers and last access to this topic |
| 132 |
func (t *topic) Stats() (int, time.Time) { |
| 133 |
t.mu.RLock() |
| 134 |
defer t.mu.RUnlock() |
| 135 |
return len(t.subscribers), t.lastAccess |
| 136 |
} |
| 137 |
|
| 138 |
// Keepalive sets the last access time and ensures that Stale does not return true |
| 139 |
func (t *topic) Keepalive() { |
| 140 |
t.mu.Lock() |
| 141 |
defer t.mu.Unlock() |
| 142 |
t.lastAccess = time.Now() |
| 143 |
} |
| 144 |
|
| 145 |
// CancelSubscribersExceptUser calls the cancel function for all subscribers, forcing |
| 146 |
func (t *topic) CancelSubscribersExceptUser(exceptUserID string) { |
| 147 |
t.mu.Lock() |
| 148 |
defer t.mu.Unlock() |
| 149 |
for _, s := range t.subscribers { |
| 150 |
if s.userID != exceptUserID { |
| 151 |
t.cancelUserSubscriber(s) |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// CancelSubscriberUser kills the subscriber with the given user ID |
| 157 |
func (t *topic) CancelSubscriberUser(userID string) { |
| 158 |
t.mu.RLock() |
| 159 |
defer t.mu.RUnlock() |
| 160 |
for _, s := range t.subscribers { |
| 161 |
if s.userID == userID { |
| 162 |
t.cancelUserSubscriber(s) |
| 163 |
return |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func (t *topic) cancelUserSubscriber(s *topicSubscriber) { |
| 169 |
log. |
| 170 |
Tag(tagSubscribe). |
| 171 |
With(t). |
| 172 |
Fields(log.Context{ |
| 173 |
"user_id": s.userID, |
| 174 |
}). |
| 175 |
Debug("Canceling subscriber with user ID %s", s.userID) |
| 176 |
s.cancel() |
| 177 |
} |
| 178 |
|
| 179 |
func (t *topic) Context() log.Context { |
| 180 |
t.mu.RLock() |
| 181 |
defer t.mu.RUnlock() |
| 182 |
fields := map[string]any{ |
| 183 |
"topic": t.ID, |
| 184 |
"topic_subscribers": len(t.subscribers), |
| 185 |
"topic_last_access": util.FormatTime(t.lastAccess), |
| 186 |
} |
| 187 |
if t.rateVisitor != nil { |
| 188 |
for k, v := range t.rateVisitor.Context() { |
| 189 |
fields["topic_rate_"+k] = v |
| 190 |
} |
| 191 |
} |
| 192 |
return fields |
| 193 |
} |
| 194 |
|
| 195 |
// subscribersCopy returns a shallow copy of the subscribers map |
| 196 |
func (t *topic) subscribersCopy() map[int]*topicSubscriber { |
| 197 |
t.mu.Lock() |
| 198 |
defer t.mu.Unlock() |
| 199 |
subscribers := make(map[int]*topicSubscriber) |
| 200 |
for k, sub := range t.subscribers { |
| 201 |
subscribers[k] = &topicSubscriber{ |
| 202 |
userID: sub.userID, |
| 203 |
subscriber: sub.subscriber, |
| 204 |
cancel: sub.cancel, |
| 205 |
} |
| 206 |
} |
| 207 |
return subscribers |
| 208 |
} |