From 38bf117f29f2cb8a31ed49c506a93dced46844c0 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Mon, 17 Aug 2020 11:40:58 +0200 Subject: [PATCH] move the lock sync to _handle --- email_handler.py | 58 ++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/email_handler.py b/email_handler.py index 9ee3f24b..f5def68e 100644 --- a/email_handler.py +++ b/email_handler.py @@ -1235,40 +1235,39 @@ async def get_spam_score(message: Message) -> float: class MailHandler: - lock = asyncio.Lock() + def __init__(self, lock): + self.lock = lock async def handle_DATA(self, server, session, envelope: Envelope): - async with self.lock: - try: - ret = await self._handle(envelope) - return ret - except Exception: - LOG.exception( - "email handling fail %s -> %s", - envelope.mail_from, - envelope.rcpt_tos, - ) - return "421 SL Retry later" + try: + ret = await self._handle(envelope) + return ret + except Exception: + LOG.exception( + "email handling fail %s -> %s", envelope.mail_from, envelope.rcpt_tos, + ) + return "421 SL Retry later" async def _handle(self, envelope: Envelope): - start = time.time() - LOG.debug( - "===>> New message, mail from %s, rctp tos %s ", - envelope.mail_from, - envelope.rcpt_tos, - ) + async with self.lock: + start = time.time() + LOG.info( + "===>> New message, mail from %s, rctp tos %s ", + envelope.mail_from, + envelope.rcpt_tos, + ) - if POSTFIX_SUBMISSION_TLS: - smtp = SMTP(POSTFIX_SERVER, 587) - smtp.starttls() - else: - smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT or 25) + if POSTFIX_SUBMISSION_TLS: + smtp = SMTP(POSTFIX_SERVER, 587) + smtp.starttls() + else: + smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT or 25) - app = new_app() - with app.app_context(): - ret = await handle(envelope, smtp) - LOG.debug("takes %s seconds <<===", time.time() - start) - return ret + app = new_app() + with app.app_context(): + ret = await handle(envelope, smtp) + LOG.info("takes %s seconds <<===", time.time() - start) + return ret if __name__ == "__main__": @@ -1278,9 +1277,10 @@ if __name__ == "__main__": with app.app_context(): load_pgp_public_keys() - handler = MailHandler() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) + lock = asyncio.Lock() + handler = MailHandler(lock) def factory(): return aiosmtpd.smtp.SMTP(handler, enable_SMTPUTF8=True)