topic.go 4.9 KB

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