emailpusher.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. # Copyright 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from typing import TYPE_CHECKING, Dict, List, Optional
  16. from twisted.internet.error import AlreadyCalled, AlreadyCancelled
  17. from twisted.internet.interfaces import IDelayedCall
  18. from synapse.metrics.background_process_metrics import run_as_background_process
  19. from synapse.push import Pusher, PusherConfig, PusherConfigException, ThrottleParams
  20. from synapse.push.mailer import Mailer
  21. from synapse.push.push_types import EmailReason
  22. from synapse.storage.databases.main.event_push_actions import EmailPushAction
  23. from synapse.util.threepids import validate_email
  24. if TYPE_CHECKING:
  25. from synapse.server import HomeServer
  26. logger = logging.getLogger(__name__)
  27. # The amount of time we always wait before ever emailing about a notification
  28. # (to give the user a chance to respond to other push or notice the window)
  29. DELAY_BEFORE_MAIL_MS = 10 * 60 * 1000
  30. # THROTTLE is the minimum time between mail notifications sent for a given room.
  31. # Each room maintains its own throttle counter, but each new mail notification
  32. # sends the pending notifications for all rooms.
  33. THROTTLE_START_MS = 10 * 60 * 1000
  34. THROTTLE_MAX_MS = 24 * 60 * 60 * 1000 # 24h
  35. # THROTTLE_MULTIPLIER = 6 # 10 mins, 1 hour, 6 hours, 24 hours
  36. THROTTLE_MULTIPLIER = 144 # 10 mins, 24 hours - i.e. jump straight to 1 day
  37. # If no event triggers a notification for this long after the previous,
  38. # the throttle is released.
  39. # 12 hours - a gap of 12 hours in conversation is surely enough to merit a new
  40. # notification when things get going again...
  41. THROTTLE_RESET_AFTER_MS = 12 * 60 * 60 * 1000
  42. # does each email include all unread notifs, or just the ones which have happened
  43. # since the last mail?
  44. # XXX: this is currently broken as it includes ones from parted rooms(!)
  45. INCLUDE_ALL_UNREAD_NOTIFS = False
  46. class EmailPusher(Pusher):
  47. """
  48. A pusher that sends email notifications about events (approximately)
  49. when they happen.
  50. This shares quite a bit of code with httpusher: it would be good to
  51. factor out the common parts
  52. """
  53. def __init__(self, hs: "HomeServer", pusher_config: PusherConfig, mailer: Mailer):
  54. super().__init__(hs, pusher_config)
  55. self.mailer = mailer
  56. self.store = self.hs.get_datastores().main
  57. self.email = pusher_config.pushkey
  58. self.timed_call: Optional[IDelayedCall] = None
  59. self.throttle_params: Dict[str, ThrottleParams] = {}
  60. self._inited = False
  61. self._is_processing = False
  62. # Make sure that the email is valid.
  63. try:
  64. validate_email(self.email)
  65. except ValueError:
  66. raise PusherConfigException("Invalid email")
  67. def on_started(self, should_check_for_notifs: bool) -> None:
  68. """Called when this pusher has been started.
  69. Args:
  70. should_check_for_notifs: Whether we should immediately
  71. check for push to send. Set to False only if it's known there
  72. is nothing to send
  73. """
  74. if should_check_for_notifs and self.mailer is not None:
  75. self._start_processing()
  76. def on_stop(self) -> None:
  77. if self.timed_call:
  78. try:
  79. self.timed_call.cancel()
  80. except (AlreadyCalled, AlreadyCancelled):
  81. pass
  82. self.timed_call = None
  83. def on_new_receipts(self, min_stream_id: int, max_stream_id: int) -> None:
  84. # We could wake up and cancel the timer but there tend to be quite a
  85. # lot of read receipts so it's probably less work to just let the
  86. # timer fire
  87. pass
  88. def on_timer(self) -> None:
  89. self.timed_call = None
  90. self._start_processing()
  91. def _start_processing(self) -> None:
  92. if self._is_processing:
  93. return
  94. run_as_background_process("emailpush.process", self._process)
  95. def _pause_processing(self) -> None:
  96. """Used by tests to temporarily pause processing of events.
  97. Asserts that its not currently processing.
  98. """
  99. assert not self._is_processing
  100. self._is_processing = True
  101. def _resume_processing(self) -> None:
  102. """Used by tests to resume processing of events after pausing."""
  103. assert self._is_processing
  104. self._is_processing = False
  105. self._start_processing()
  106. async def _process(self) -> None:
  107. # we should never get here if we are already processing
  108. assert not self._is_processing
  109. try:
  110. self._is_processing = True
  111. if not self._inited:
  112. # this is our first loop: load up the throttle params
  113. assert self.pusher_id is not None
  114. self.throttle_params = await self.store.get_throttle_params_by_room(
  115. self.pusher_id
  116. )
  117. self._inited = True
  118. # if the max ordering changes while we're running _unsafe_process,
  119. # call it again, and so on until we've caught up.
  120. while True:
  121. starting_max_ordering = self.max_stream_ordering
  122. try:
  123. await self._unsafe_process()
  124. except Exception:
  125. logger.exception("Exception processing notifs")
  126. if self.max_stream_ordering == starting_max_ordering:
  127. break
  128. finally:
  129. self._is_processing = False
  130. async def _unsafe_process(self) -> None:
  131. """
  132. Main logic of the push loop without the wrapper function that sets
  133. up logging, measures and guards against multiple instances of it
  134. being run.
  135. """
  136. start = 0 if INCLUDE_ALL_UNREAD_NOTIFS else self.last_stream_ordering
  137. unprocessed = (
  138. await self.store.get_unread_push_actions_for_user_in_range_for_email(
  139. self.user_id, start, self.max_stream_ordering
  140. )
  141. )
  142. soonest_due_at: Optional[int] = None
  143. if not unprocessed:
  144. await self.save_last_stream_ordering_and_success(self.max_stream_ordering)
  145. return
  146. for push_action in unprocessed:
  147. received_at = push_action.received_ts
  148. if received_at is None:
  149. received_at = 0
  150. notif_ready_at = received_at + DELAY_BEFORE_MAIL_MS
  151. room_ready_at = self.room_ready_to_notify_at(push_action.room_id)
  152. should_notify_at = max(notif_ready_at, room_ready_at)
  153. if should_notify_at <= self.clock.time_msec():
  154. # one of our notifications is ready for sending, so we send
  155. # *one* email updating the user on their notifications,
  156. # we then consider all previously outstanding notifications
  157. # to be delivered.
  158. reason: EmailReason = {
  159. "room_id": push_action.room_id,
  160. "now": self.clock.time_msec(),
  161. "received_at": received_at,
  162. "delay_before_mail_ms": DELAY_BEFORE_MAIL_MS,
  163. "last_sent_ts": self.get_room_last_sent_ts(push_action.room_id),
  164. "throttle_ms": self.get_room_throttle_ms(push_action.room_id),
  165. }
  166. await self.send_notification(unprocessed, reason)
  167. await self.save_last_stream_ordering_and_success(
  168. max(ea.stream_ordering for ea in unprocessed)
  169. )
  170. # we update the throttle on all the possible unprocessed push actions
  171. for ea in unprocessed:
  172. await self.sent_notif_update_throttle(ea.room_id, ea)
  173. break
  174. else:
  175. if soonest_due_at is None or should_notify_at < soonest_due_at:
  176. soonest_due_at = should_notify_at
  177. if self.timed_call is not None:
  178. try:
  179. self.timed_call.cancel()
  180. except (AlreadyCalled, AlreadyCancelled):
  181. pass
  182. self.timed_call = None
  183. if soonest_due_at is not None:
  184. self.timed_call = self.hs.get_reactor().callLater(
  185. self.seconds_until(soonest_due_at), self.on_timer
  186. )
  187. async def save_last_stream_ordering_and_success(
  188. self, last_stream_ordering: int
  189. ) -> None:
  190. self.last_stream_ordering = last_stream_ordering
  191. pusher_still_exists = (
  192. await self.store.update_pusher_last_stream_ordering_and_success(
  193. self.app_id,
  194. self.email,
  195. self.user_id,
  196. last_stream_ordering,
  197. self.clock.time_msec(),
  198. )
  199. )
  200. if not pusher_still_exists:
  201. # The pusher has been deleted while we were processing, so
  202. # lets just stop and return.
  203. self.on_stop()
  204. def seconds_until(self, ts_msec: int) -> float:
  205. secs = (ts_msec - self.clock.time_msec()) / 1000
  206. return max(secs, 0)
  207. def get_room_throttle_ms(self, room_id: str) -> int:
  208. if room_id in self.throttle_params:
  209. return self.throttle_params[room_id].throttle_ms
  210. else:
  211. return 0
  212. def get_room_last_sent_ts(self, room_id: str) -> int:
  213. if room_id in self.throttle_params:
  214. return self.throttle_params[room_id].last_sent_ts
  215. else:
  216. return 0
  217. def room_ready_to_notify_at(self, room_id: str) -> int:
  218. """
  219. Determines whether throttling should prevent us from sending an email
  220. for the given room
  221. Returns:
  222. The timestamp when we are next allowed to send an email notif
  223. for this room
  224. """
  225. last_sent_ts = self.get_room_last_sent_ts(room_id)
  226. throttle_ms = self.get_room_throttle_ms(room_id)
  227. may_send_at = last_sent_ts + throttle_ms
  228. return may_send_at
  229. async def sent_notif_update_throttle(
  230. self, room_id: str, notified_push_action: EmailPushAction
  231. ) -> None:
  232. # We have sent a notification, so update the throttle accordingly.
  233. # If the event that triggered the notif happened more than
  234. # THROTTLE_RESET_AFTER_MS after the previous one that triggered a
  235. # notif, we release the throttle. Otherwise, the throttle is increased.
  236. time_of_previous_notifs = await self.store.get_time_of_last_push_action_before(
  237. notified_push_action.stream_ordering
  238. )
  239. time_of_this_notifs = notified_push_action.received_ts
  240. if time_of_previous_notifs is not None and time_of_this_notifs is not None:
  241. gap = time_of_this_notifs - time_of_previous_notifs
  242. else:
  243. # if we don't know the arrival time of one of the notifs (it was not
  244. # stored prior to email notification code) then assume a gap of
  245. # zero which will just not reset the throttle
  246. gap = 0
  247. current_throttle_ms = self.get_room_throttle_ms(room_id)
  248. if gap > THROTTLE_RESET_AFTER_MS:
  249. new_throttle_ms = THROTTLE_START_MS
  250. else:
  251. if current_throttle_ms == 0:
  252. new_throttle_ms = THROTTLE_START_MS
  253. else:
  254. new_throttle_ms = min(
  255. current_throttle_ms * THROTTLE_MULTIPLIER, THROTTLE_MAX_MS
  256. )
  257. self.throttle_params[room_id] = ThrottleParams(
  258. self.clock.time_msec(),
  259. new_throttle_ms,
  260. )
  261. assert self.pusher_id is not None
  262. await self.store.set_throttle_params(
  263. self.pusher_id, room_id, self.throttle_params[room_id]
  264. )
  265. async def send_notification(
  266. self, push_actions: List[EmailPushAction], reason: EmailReason
  267. ) -> None:
  268. logger.info("Sending notif email for user %r", self.user_id)
  269. await self.mailer.send_notification_mail(
  270. self.app_id, self.user_id, self.email, push_actions, reason
  271. )