简体   繁体   English

Amazon SES:如何加快此PHP示例的卷曲速度?

[英]Amazon SES: How to speed-up curl for this PHP example?

My Amazon Simple Email Service account can theoretically process 14 emails/second but my code is only achieving 1 or 2 per second. 我的Amazon Simple Email Service帐户理论上每秒可以处理14封电子邮件,但是我的代码每秒只能达到1或2。 Any help on how to increase the rate of HTTP-POSTs is appreciated. 感谢您提供有关如何提高HTTP-POST速率的任何帮助。 I'm pasting my code below to solicit ideas. 我在下面粘贴我的代码以征求想法。 If possible, post your code ideas rather than links. 如果可能,请发布您的代码提示,而不要发布链接。 Thanks! 谢谢!

NOTE: The code below is contained in a foreach loop that references a database of emails. 注意:以下代码包含在引用电子邮件数据库的foreach循环中。 Each email I'm sending via HTTP-POST is personalized as seen in the $MAIL string and sent to via the curl code block below. 我通过HTTP-POST发送的每封电子邮件都是个性化的,如$ MAIL字符串中所示,并通过下面的curl代码块发送至。 I should also mention that I do not care about the HTTP-POST response. 我还应该提到,我不在乎HTTP-POST响应。

$DATE = gmdate('D, d M Y H:i:s e');
$HASH = hash_hmac('sha1', $DATE, $AWSPRI, true);
$KEYS = base64_encode($HASH);
$headers = array();
$headers[] = "Host: email.us-east-1.amazonaws.com";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Date: ".$DATE;
  $auth = "AWS3-HTTPS AWSAccessKeyId=".$AWSPUB;
  $auth .= ",Algorithm=HmacSHA1,Signature=".$KEYS;
$headers[] = "X-Amzn-Authorization: ".$auth;
$url = "https://email.us-east-1.amazonaws.com/";
$MAIL =  "Action=SendEmail&Source=".$FROM_ENCODE."&ReturnPath=".$BOUNCE_ENCODE."&Destination.ToAddresses.member.1=".$SUBSCRIBER_ENCODE."&Message.Subject.Data=".$SUBJECT_ENCODE."&Message.Body.Html.Data=".$EMAIL_BODY_ENCODE;
$aws = curl_init();
curl_setopt($aws, CURLOPT_POSTFIELDS, $MAIL);
curl_setopt($aws, CURLOPT_HTTPHEADER, $headers);
curl_setopt($aws, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($aws, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($aws, CURLOPT_HEADER, false);
curl_setopt($aws, CURLOPT_URL, $url);
curl_setopt($aws, CURLOPT_RETURNTRANSFER, true);
curl_exec($aws);
curl_close($aws);

Firstly, you could put some benchmarking code in to find out the bottlenecks. 首先,您可以放入一些基准测试代码来找出瓶颈。

Secondly, a shot in the dark, you could reuse http connections. 其次,在黑暗中拍摄,您可以重用http连接。

Thirdly, and probably the right answer, get a beefed up dedicated server sitting in a high bandwidth thru-put international network center - James Bond style. 第三,也许是正确的答案,让一台功能强大的专用服务器位于高带宽直通国际网络中心-James Bond风格。

Use batches . 使用批处理 Configure 14 emails at once, and collect each CURL hangle by returning it, instead of immediate processing - see returnCurlHandle option. 一次配置14封电子邮件,然后通过返回而不是立即处理的方式收集每个CURL邮件-请参阅returnCurlHandle选项。

Thus there will be 14 concurrent CURL sessions, and all batch will take about 2-4 seconds to process. 因此,将有14个并发的CURL会话,并且所有批处理将花费大约2-4秒的时间。

I have not tested this yet, but I'm going to split the array of emails into 5 chunks, and pass each one simultaneously through the HTTP-POST routine. 我还没有对此进行测试,但是我将把电子邮件数组分成5个大块,然后将每个大块同时通过HTTP-POST例程传递。 In principle, that should cut the send-rate by 5 times. 原则上,应该将发送速率降低5倍。

With my previous experience I found reusing your cURL handle reduces overhead significantly. 根据我以前的经验,我发现重用cURL句柄可以显着减少开销。 Simply only do curl_init() once and only set the options that change each time. 只需只执行一次curl_init(),并仅设置每次更改的选项。

The other thing you could do is use mutli curl which allows curl to handle making it multithreaded. 您可以做的另一件事是使用mutli curl,它允许curl处理使其成为多线程。

http://php.net/manual/en/function.curl-multi-init.php http://php.net/manual/zh/function.curl-multi-init.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM