MINOR: hlua_fcn/mailers: handle timeout mail from mailers section

As the example/lua/mailers.lua script does its best to mimic the c-mailer
implementation, it should support the "timeout mail" directive as well.

This could be backported in 2.8.
This commit is contained in:
Aurelien DARRAGON 2023-07-07 16:55:43 +02:00 committed by Christopher Faulet
parent b2f7069479
commit b58bd9794f
3 changed files with 19 additions and 1 deletions

View File

@ -1198,6 +1198,12 @@ ProxyMailers class
Each array entry is a name:desc pair where desc represents the full server
address (including port) as described in haproxy's configuration file.
.. js:attribute:: ProxyMailers.mailservers_timeout
An integer representing the maximum time in milliseconds to wait for the
email to be sent. See "timeout mail" directive from "mailers" section in
haproxy configuration file.
.. js:attribute:: ProxyMailers.smtp_hostname
A string containing the hostname to use for the SMTP transaction.

View File

@ -30,7 +30,9 @@ local mailqueue = core.queue()
-- same format used in haproxy config file. It will be passed as it is to
-- tcp::connect() without explicit port argument. See Socket.connect()
-- manual for more information.
function smtp_send_email(server, domain, from, to, data)
--
-- The function will abort after <timeout> ms
function smtp_send_email(server, timeout, domain, from, to, data)
local ret
local reason
local tcp = core.tcp()
@ -55,6 +57,10 @@ function smtp_send_email(server, domain, from, to, data)
end
end
if timeout ~= 0 then
tcp:settimeout(timeout / 1000)
end
if tcp:connect(server) == nil then
return false, "Can't connect to \""..server.."\""
end
@ -406,6 +412,7 @@ core.register_task(function()
for name, mailsrv in pairs(job.mailconf.mailservers) do
-- finally, send email to server
local ret, reason = smtp_send_email(mailsrv,
job.mailconf.mailservers_timeout,
job.mailconf.smtp_hostname,
job.mailconf.smtp_from,
job.mailconf.smtp_to,

View File

@ -2134,6 +2134,11 @@ int hlua_proxy_get_mailers(lua_State *L)
}
lua_settable(L, -3);
/* mailers timeout (from mailers section) */
lua_pushstring(L, "mailservers_timeout");
lua_pushinteger(L, px->email_alert.mailers.m->timeout.mail);
lua_settable(L, -3);
/* email-alert myhostname */
lua_pushstring(L, "smtp_hostname");
lua_pushstring(L, px->email_alert.myhostname);