neurofuzzy, flash game development, rich internet applications, free source code - *alt.neurotica.fuzzy*

neurofuzzy, flash game development, rich internet applications, free source code - *alt.neurotica.fuzzy*

12/2/2006

Amazon S3 Tip, Check Availability First

Filed under: PHP — geoff @ 9:32 pm

If you’re using Amazon S3 in PHP (whether you are using the neurofuzzy s3 class or your own solution) it is a good idea to make sure you can access the service before you attempt any operations on it. If for some reason your site cannot access S3, it’s possible your server could get seriously bogged down or even crash if you aren’t careful.

In my experience, the PEAR HTTPrequest class doesn’t timeout properly out of the box. This means that if you can’t reach S3, HTTP requests will remain active, keeping processes running and possibly MySQL connections open. If you are running scripts that access S3 on a cron, or actively on your site’s pages, your request queues can back up, overloading your server.

I can get even hairier than that. If you are running scripts that pipe images or other data from S3 through your site as a proxy (you may do this to mask the S3 urls or to get around Flash security restrictions), it’s possible that your users will have their browser’s HTTP requests to your site back up as well. Since browsers have limits on concurrent connections to a single domain, this will effectively cause your site to lock up for the user. Very bad indeed.

If you are running a backend script that works via HTTPrequest, or any script where you’ve turned off timeouts, it’s imperative to check to make sure you can get to S3. One solution is to upload a small, publicly available resource to a bucket, and then when you want to check S3, just try to grab the file.

Put something like this at the beginning of your script:

// make sure S3 is accessible
if (!@file_get_contents("http://s3.amazonaws.com/mybucket/…
… myresource.ext")) {
      die("Server can't see S3!");
}

If you are piping images through your site from S3 to your users, use cURL instead of readfile. cURL’s timeouts are more reliable in my experience:

$location = "http://s3.amazonaws.com/mybucket/myresource.ext";
$ch = curl_init();
$timeout = 5; // set timeout in seconds - zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $location);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

It’s also a good idea to close any MySQL connections you may have open before you transfer any large files.

These ideas are based on my own experiences, and I’m sure that there are others out there who may have better ideas. Feel free to post your ideas in the comments. Thanks!

 

Powered by WordPress