Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #786 from matrix-org/matthew/email_notifs_tuning
Browse files Browse the repository at this point in the history
tune email notifs, fix CSS a bit, and add debugging details
  • Loading branch information
ara4n committed May 17, 2016
2 parents 1ed3378 + e501e9e commit 43e1e04
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
5 changes: 5 additions & 0 deletions res/templates/mail.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ body {
margin: 0px;
}

pre, code {
word-break: break-word;
white-space: pre-wrap;
}

#page {
font-family: 'Open Sans', Helvetica, Arial, Sans-Serif;
font-color: #454545;
Expand Down
11 changes: 11 additions & 0 deletions res/templates/notif_mail.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
{% include 'room.html' with context %}
{% endfor %}
<div class="footer">
<small>
Sending email at {{ reason.now|format_ts("%c") }} due to activity in room '{{ reason.room_name }}' because:<br/>
1. An event was received at {{ reason.received_at|format_ts("%c") }}
which is more than {{ "%.1f"|format(reason.delay_before_mail_ms / (60*1000)) }} (delay_before_mail_ms) mins ago.<br/>
{% if reason.last_sent_ts %}
2. The last time we sent a mail for this room was {{ reason.last_sent_ts|format_ts("%c") }},
which is more than {{ "%.1f"|format(reason.throttle_ms / (60*1000)) }} (current throttle_ms) mins ago.
{% else %}
2. We can't remember the last time we sent a mail for this room.
{% endif %}
</small>
<a href="{{ unsubscribe_link }}">Unsubscribe</a>
</div>
</td>
Expand Down
32 changes: 24 additions & 8 deletions synapse/push/emailpusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@

# The amount of time we always wait before ever emailing about a notification
# (to give the user a chance to respond to other push or notice the window)
DELAY_BEFORE_MAIL_MS = 2 * 60 * 1000
DELAY_BEFORE_MAIL_MS = 10 * 60 * 1000

THROTTLE_START_MS = 2 * 60 * 1000
THROTTLE_MAX_MS = (2 * 60 * 1000) * (2 ** 11) # ~3 days
THROTTLE_MULTIPLIER = 2
# THROTTLE is the minimum time between mail notifications sent for a given room.
# Each room maintains its own throttle counter, but each new mail notification
# sends the pending notifications for all rooms.
THROTTLE_START_MS = 10 * 60 * 1000
THROTTLE_MAX_MS = 24 * 60 * 60 * 1000 # (2 * 60 * 1000) * (2 ** 11) # ~3 days
THROTTLE_MULTIPLIER = 6 # 10 mins, 1 hour, 6 hours, 24 hours

# If no event triggers a notification for this long after the previous,
# the throttle is released.
Expand Down Expand Up @@ -146,7 +149,18 @@ def _unsafe_process(self):
# *one* email updating the user on their notifications,
# we then consider all previously outstanding notifications
# to be delivered.
yield self.send_notification(unprocessed)

# debugging:
reason = {
'room_id': push_action['room_id'],
'now': self.clock.time_msec(),
'received_at': received_at,
'delay_before_mail_ms': DELAY_BEFORE_MAIL_MS,
'last_sent_ts': self.get_room_last_sent_ts(push_action['room_id']),
'throttle_ms': self.get_room_throttle_ms(push_action['room_id']),
}

yield self.send_notification(unprocessed, reason)

yield self.save_last_stream_ordering_and_success(max([
ea['stream_ordering'] for ea in unprocessed
Expand Down Expand Up @@ -195,7 +209,8 @@ def room_ready_to_notify_at(self, room_id):
"""
Determines whether throttling should prevent us from sending an email
for the given room
Returns: True if we should send, False if we should not
Returns: The timestamp when we are next allowed to send an email notif
for this room
"""
last_sent_ts = self.get_room_last_sent_ts(room_id)
throttle_ms = self.get_room_throttle_ms(room_id)
Expand Down Expand Up @@ -244,8 +259,9 @@ def sent_notif_update_throttle(self, room_id, notified_push_action):
)

@defer.inlineCallbacks
def send_notification(self, push_actions):
def send_notification(self, push_actions, reason):
logger.info("Sending notif email for user %r", self.user_id)

yield self.mailer.send_notification_mail(
self.user_id, self.email, push_actions
self.user_id, self.email, push_actions, reason
)
7 changes: 6 additions & 1 deletion synapse/push/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __init__(self, hs):
)

@defer.inlineCallbacks
def send_notification_mail(self, user_id, email_address, push_actions):
def send_notification_mail(self, user_id, email_address, push_actions, reason):
raw_from = email.utils.parseaddr(self.hs.config.email_notif_from)[1]
raw_to = email.utils.parseaddr(email_address)[1]

Expand Down Expand Up @@ -143,12 +143,17 @@ def _fetch_room_state(room_id):
notifs_by_room, state_by_room, notif_events, user_id
)

reason['room_name'] = calculate_room_name(
state_by_room[reason['room_id']], user_id, fallback_to_members=False
)

template_vars = {
"user_display_name": user_display_name,
"unsubscribe_link": self.make_unsubscribe_link(),
"summary_text": summary_text,
"app_name": self.app_name,
"rooms": rooms,
"reason": reason,
}

html_text = self.notif_template_html.render(**template_vars)
Expand Down

0 comments on commit 43e1e04

Please sign in to comment.