间歇性的Cloudfront CDN故障(监视) - CDN故障转移 [英] Intermittent Cloudfront CDN failures (monitoring) - CDN Failover

查看:510
本文介绍了间歇性的Cloudfront CDN故障(监视) - CDN故障转移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关在过去的2个月,我一直在经历亚马逊的Cloudfront间歇性故障(每周2-3次),从而使页面加载会从我的Web服务器,但全部来自CDN的资产将在时间尚待分钟块(我证实,壳牌卷曲从不同的数据中心的一些工作有些不依赖于边缘的位置 - 伦敦)。一旦挂起的申请成功,一切都恢复正常。 我们一直在报道这对亚马逊,但他们始终以不要指望从我们的答复。如果极大数人会抱怨只有这样,我们考虑寻找到这个之类的消息的回复。通常,恢复正常工作,我写完了支持请求之前。

For the past 2 months I have been experiencing Amazon Cloudfront intermittent failures (2-3 times a week) whereby the page would load from my web server but all the assets from the CDN would block in pending for minutes at the time (I confirmed that with shell curl from different datacenters some work some don't depending on the edge location - London?). Once the pending requests succeed all goes back to normal. We have been reporting this to amazon but they always reply with "Don't expect reply from us. If gazillion people will complain only then will we consider looking into this" kind of message. Often it resumes normal operation before I'm done writing the support request.

我得出一个结论,最好的方式进行,由于缺乏开发时间迁移到其他CDN是在HTML头,可以让我们知道,每当类似的事情发生添加脚本。所以说,在头试图从CDN如果请求花费的时间多于N毫秒然后调用根域(监测)内的任意URL下载一个很小的GIF。

I came to a conclusion that the best way to proceed due to lack of development time for migrating to other CDN is to add a script in the html header that will let us know whenever something similar happens. So say in the header try to download a tiny gif from the CDN if the request takes longer than N msec then call an arbitrary url within the root domain (for monitoring).

的问题: 一个人如何可靠,在所有流行的浏览器,请求与回调的超时文件。即:

The question: How does one reliably, across all popular browsers, request a file with callback on timeout. i.e.:

  • 要求从CDN使用AJAX的文件 - 不会因跨域限制的工作
  • 的setTimeout(callbackTimeout,2000年)callbackTimeout(){的getElementById()否则... HttpWebRequest的...} - 将是会阻止挂起的HttpWebRequest请求或将它的工作

怎么回事?

感谢。

推荐答案

这是短暂IE.7和放大器测试; 8,最新的FF在Windows和放大器; OSX以及Chrome浏览器。我建议你​​自己进行测试。缩小!如果你知道这样做,请提出你的改进的更好的方法。使用图像的即脚本,而不是该方法已经考虑和决定对可能主要是由于我的无知。

This has been briefly tested in IE.7&8, up to date FF on Windows & OSX as well as Chrome. I suggest you test it yourself. Minify! If you know better way of doing this please suggest your improvements. The way using i.e. script instead of an image has been considered and decided against probably mostly due to my ignorance.

下一个版本将写超时的cookie和将来的请求将在服务器端进行处理(利用相关资产路径)。 cookie将到期说,30分钟后。每个连续超时将更新该cookie。不知道我怎么会处理的第一个故障切换。可能是一个重定向(不是很优雅,但简单)。也许我会想出更聪明的方法(可能更优雅,但更复杂的太)。

The next version will write a cookie on timeout and the future requests will be handled on the server side (using relative asset path). The cookie will expire after say 30 minutes. Every consecutive timeout will renew that cookie. Not sure how I'll handle the first failover. Could be a redirect (not very elegant but simple). Perhaps I will figure out smarter way (possibly more elegant but more complex too).

<script type="text/javascript">
//<![CDATA[
// Absolute path to a picture on your CDN to be monitored
cdnImagePath        = "http://YOURCDNADDRESS.net/empty.gif";
//this is relative path (cross domain limitation) 
//will be followed by "timeout" or "other" as a reason i.e. /cdnMonitor.php?message=timeout
cdnMonitoringPath = "/cdnMonitor.php?message=";
// Recommended 3000 for 3 second(s) timeout
cdnTimeoutMilisec = 3000;
// Set to true to be notified after timeout (provides extra information)
cdnNotifyAfterTimeout = false;

// Handler methods
cdnOK = function(){
     if (!cdnTimer && cdnNotifyAfterTimeout) cdnNotify('success');
}

cdnFail = function(reason){
     if (reason != "timeout") {
          if (cdnTimer) clearTimeout(cdnTimer);
          message = "error"
     } else {
          message = reason;
     }
     cdnNotify(message);
}

cdnTimeout = function() {
     cdnTimer = false;
     if (cdnImage.complete == false) {
          cdnFail("timeout");
     }
}

cdnNotify = function(message) {
     if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", cdnMonitoringPath + message, true);
        xmlhttp.send();
     } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
}

// Load test image and define event handlers
cdnTimer = setTimeout("cdnTimeout()", cdnTimeoutMilisec);
cdnImage = new Image(); 
cdnImage.onload  = cdnOK;
cdnImage.onerror = cdnFail;
cdnImage.src = cdnImagePath + "?" + Math.floor(Math.random()*1000000);
//]]>
</script>

另外,这是我将使用什么特别监测服务器端cdnMonitor.php:

Also this is what I'll use for ad hoc monitoring on the server side cdnMonitor.php:

error_log(date('Y-m-d H:i:s.') .next(explode('.',microtime(1))). ' - '. $_GET['message'] . ' - '. $_SERVER['HTTP_X_REAL_IP']. ' - ' . $_SERVER['HTTP_USER_AGENT'] ."\n", 3, '/tmp/cdnMonitor.log');

您将需要更改HTTP_X_REAL_IP来REMOTE_ADDR或任何适合您的需要。我使用反向代理所以这就是我做的。

You will need to change the "HTTP_X_REAL_IP" to REMOTE_ADDR or whatever suits your needs. I use reverse proxy so that's what I do.

最后,我做了编辑后的一些最后一分钟的变化,并有可能打破东西。手指交叉。

Lastly I made some last minute changes in the post editor and might have broken something. Fingers crossed.

这篇关于间歇性的Cloudfront CDN故障(监视) - CDN故障转移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆