From 2524c8ab98c660e2654652a6ad67e2b76333cb86 Mon Sep 17 00:00:00 2001 From: Son Date: Tue, 12 Oct 2021 14:53:30 +0200 Subject: [PATCH] sleep for 60s when rate limit is hit for the first time --- email_handler.py | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/email_handler.py b/email_handler.py index a29798ff..bf9ecb29 100644 --- a/email_handler.py +++ b/email_handler.py @@ -1858,24 +1858,37 @@ async def handle(envelope: Envelope) -> str: return status.E523 if rate_limited(mail_from, rcpt_tos): - LOG.w("Rate Limiting applied for mail_from:%s rcpt_tos:%s", mail_from, rcpt_tos) + LOG.w( + "Rate Limiting applied for mail_from:%s rcpt_tos:%s, retry in 60s", + mail_from, + rcpt_tos, + ) + # slow down the rate a bit + await asyncio.sleep(60) - # add more logging info. TODO: remove - if len(rcpt_tos) == 1: - alias = Alias.get_by(email=rcpt_tos[0]) - if alias: - LOG.w( - "total number email log on %s, %s is %s, %s", - alias, - alias.user, - EmailLog.filter(EmailLog.alias_id == alias.id).count(), - EmailLog.filter(EmailLog.user_id == alias.user_id).count(), - ) + # rate limit is still applied + if rate_limited(mail_from, rcpt_tos): + LOG.w( + "Rate Limiting (no retry) applied for mail_from:%s rcpt_tos:%s", + mail_from, + rcpt_tos, + ) + # add more logging info. TODO: remove + if len(rcpt_tos) == 1: + alias = Alias.get_by(email=rcpt_tos[0]) + if alias: + LOG.w( + "total number email log on %s, %s is %s, %s", + alias, + alias.user, + EmailLog.filter(EmailLog.alias_id == alias.id).count(), + EmailLog.filter(EmailLog.user_id == alias.user_id).count(), + ) - if should_ignore_bounce(envelope.mail_from): - return status.E207 - else: - return status.E522 + if should_ignore_bounce(envelope.mail_from): + return status.E207 + else: + return status.E522 # Handle "out of office" auto notice. An automatic response is sent for every forwarded email # todo: remove logging