From b58bd9794f146ee8801415bbf7958dcc95fc5643 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Fri, 7 Jul 2023 16:55:43 +0200 Subject: [PATCH] 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. --- doc/lua-api/index.rst | 6 ++++++ examples/lua/mailers.lua | 9 ++++++++- src/hlua_fcn.c | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index c635741ab..d2720d22c 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -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. diff --git a/examples/lua/mailers.lua b/examples/lua/mailers.lua index c7ffa0376..0d146ff17 100644 --- a/examples/lua/mailers.lua +++ b/examples/lua/mailers.lua @@ -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 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, diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c index a92daa3b0..91c5155d2 100644 --- a/src/hlua_fcn.c +++ b/src/hlua_fcn.c @@ -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);