Frequent sending via Mandrill – curl connection not closed problem
-
Hi Yehuda
We have noticed a problem with Post SMTP plugin when sending large volumes of email via Post SMTP to Mandrill. The plugin does not close curl connections quickly or reliably enough, and can lead to problems with too many connections open on the server.
We send around 1500 emails per hour in a Jobs email alert to Mandrill via Post SMTP. We find that, after the first ~40 emails are sent, the connection to Mandrill starts to throw errors, and emails are skipped until the plugin resets curl connections.
In post-smtp/Postman/Postman-Mail/PostmanMandrillMailEngine.php line 170, Postman creates the Mandrill object which triggers the constructor. In that constructor the curl is initialized.
In order to have the curl connection closed it has to trigger the destructor. Here is the problem: it is not triggered. The destructor is a magic function and if used correctly it should do the job. But in our case it doesn’t work. The destructor is called when the object is unset, assigned null, at the end of the script and on reassigning a value to the same object var (which is what we are doing). From tests we’ve done this is not consistent, and sometimes it takes longer until the destructor is called.
The main issue with the current approach is that because the object gets cloned in the plugin’s own constructor (post-smtp/Postman/Postman-Mail/mailchimp-mandrill-api-php-da3adc10042e/src/Mandrill.php line 88), once the object var is overidden with a new object it is not destroyed because it was already cloned.
From testing, this is currently the constructor/destructor how happens:
2020-12-03 07:03:28 - CONSTRUCT 2020-12-03 07:03:29 - CONSTRUCT 2020-12-03 07:03:30 - CONSTRUCT 2020-12-03 07:03:31 - CONSTRUCT 2020-12-03 07:03:32 - CONSTRUCT 2020-12-03 07:03:33 - CONSTRUCT 2020-12-03 07:03:34 - CONSTRUCT 2020-12-03 07:03:35 - CONSTRUCT 2020-12-03 07:03:36 - CONSTRUCT 2020-12-03 07:03:37 - CONSTRUCT 2020-12-03 07:03:37 - CONSTRUCT 2020-12-03 07:03:38 - CONSTRUCT 2020-12-03 07:03:39 - CONSTRUCT 2020-12-03 07:03:40 - CONSTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT 2020-12-03 07:03:41 - DESTRUCT
Because the destruction happens with a delay, we end up having lots of connections open at the same time – too many for the server, which then starts blocking connections and emails sending starts to fail.
The workaround we’ve implemented in the plugin is to remove curl_close from the Post SMTP destructor and place it immediately after “curl_exec” happens in the “call” method of “Postman_Mandrill” class.
If you don’t see any problems with this approach, perhaps you would be able to implement this patch within your plugin, to prevent these issues of too many connections at times of high volume email sending?
Thanks, Yehuda
Marcus
- The topic ‘Frequent sending via Mandrill – curl connection not closed problem’ is closed to new replies.