Skip to content

Instantly share code, notes, and snippets.

@kakarukeys
Last active April 15, 2021 08:31
Show Gist options
  • Save kakarukeys/53bfe2f52ec1db0fd5ac2c817b943eaf to your computer and use it in GitHub Desktop.
Save kakarukeys/53bfe2f52ec1db0fd5ac2c817b943eaf to your computer and use it in GitHub Desktop.
from collections import defaultdict
from asyncio import Semaphore
MAX_CONNECTIONS_PER_MX = 1
leaky_bucket = defaultdict(lambda: Semaphore(MAX_CONNECTIONS_PER_MX))
async def ping_server_handler(request):
...
...
async with leaky_bucket[mx_domain]:
# only 1 await is allowed on smtp_handshake() at any time per every unique mx_domain
await smtp_handshake(mx_domain, email)
...
...
async def smtp_handshake(mx_domain, email):
...
await ..ehlo
await ..rcpt
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment