Skip to content
Snippets Groups Projects
Commit eace9bd8 authored by ale's avatar ale
Browse files

Resolve the SMTP hostname on each connection

Combined with randomization of multiple results, this allows Mailman
to run simple round-robin balancing of the available SMTP servers.
parent c903c923
No related branches found
No related tags found
No related merge requests found
Pipeline #90559 passed
...@@ -28,6 +28,7 @@ for a threaded implementation. ...@@ -28,6 +28,7 @@ for a threaded implementation.
import copy import copy
import time import time
import random
import socket import socket
import smtplib import smtplib
from base64 import b64encode from base64 import b64encode
...@@ -55,13 +56,29 @@ except NameError: ...@@ -55,13 +56,29 @@ except NameError:
# Resolve host/port, randomizing multiple IPs.
def _resolve(host, port):
results = [x[4] for x in socket.getaddrinfo(
host, port,
family=socket.AF_INET,
type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)]
return random.choice(results)
# SMTP class that resolves the target randomly on each connection.
class SMTPRR(smtplib.SMTP):
def _get_socket(self, host, port, timeout):
host, port = _resolve(host, port)
return socket.create_connection((host, port), timeout)
# Manage a connection to the SMTP server # Manage a connection to the SMTP server
class Connection: class Connection:
def __init__(self): def __init__(self):
self.__conn = None self.__conn = None
def __connect(self): def __connect(self):
self.__conn = smtplib.SMTP( self.__conn = SMTPRR(
local_hostname=mm_cfg.SMTP_HELO_HOST) local_hostname=mm_cfg.SMTP_HELO_HOST)
self.__conn.set_debuglevel(mm_cfg.SMTPLIB_DEBUG_LEVEL) self.__conn.set_debuglevel(mm_cfg.SMTPLIB_DEBUG_LEVEL)
self.__conn.connect(mm_cfg.SMTPHOST, mm_cfg.SMTPPORT) self.__conn.connect(mm_cfg.SMTPHOST, mm_cfg.SMTPPORT)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment