Stacking Delays On Tasks

One of my favorite task queue tricks is to add a stacking delay whenever multiple tasks need to be added. Here’s a sample piece of code::

TaskOptions task = TaskOptions.Builder.withUrl("/example");
task.param(parameter_name, parameter_value);
//Stacking delay here. Every subsequent task is delayed 
//an extra 5 minutes.
task.countdownMillis((1000 * 5) + (i * 5 * 60 * 1000));
Queue queue = QueueFactory.getDefaultQueue();
queue.add(task);

Notice the i variable in the countdownMillis function. The countdown function tells the task to delay a certain amount of milliseconds.

Placing this code within a for loop (and with i as the loop counter) causes each added task to be delayed for 5 seconds, plus an additional 5 minutes over the previous task. To see how it works, consider how the i variable is processed: the first task (when i = 0) will execute in 5 seconds (due to the 1000 times 5 part – 5,000 milliseconds translates into 5 seconds). The second task (when i = 1) will execute in 5 minutes & 5 seconds (1000 * 5 + 1 * 5 * 60 * 1000 turns into 5,000 + 1 * 300,000 which is 305,000 milliseconds, which is also 5 minutes, 5 seconds). The third task will wait for 605,000 milliseconds, which is 10 minutes plus 5 seconds, and so forth.

Staggering tasks like this can make an application run smoother and with fewer spikes in resource usage.

CORS Preflight Request Testing In cURL

When browsers send AJAX-JSONP requests, they often send a “preflight request” before the JSONP call. This request is a HTTP OPTIONS call asking the server whether it supports the cross origin resource sharing specification (in other words, JSONP requests).

To test a server’s support for cross origin resource sharing (CORS), you can use the cURL utility to emulate a HTTP OPTIONS request. A server that supports CORS will return a number of Access-Control headers specifying the requests it supports. Here’s an example cURL command:

curl -H "Origin: http://www.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: X-Requested-With" \
  -X OPTIONS --verbose \
  http://ip.jsontest.com/

Here’s an example of a proper CORS preflight response:

Access-Control-Allow-Origin is set to a wildcard, which means that all domains are permitted to make requests to it. Access-Control-Max-Age means that the results of this preflight request can be saved for 86,400 seconds (1 day). Access-Control-Allow-Methods means that GET and POST requests are supported.

Cloudflare Error

Many App Engine applications use Cloudflare for caching, SSL, and other services. However if Cloudflare can’t reach your website (for example, if the request is taking too long to finish) the above screen will be shown to the user.

App Engine Logos

Badge logos (sometimes called button logos) are used to advertise a website’s server software or platform. Here’s a few examples:

Apache Badge Logo

Apple Xserve:

PHP:

Personally, I always like seeing badge logos because they give an interesting insight into that website: which stack is powering the website, the preferred coding language of the webmaster, etc.

Here’s logos for App Engine:

For reference, these are the equivalent badge logos for Microsoft Azure and AWS:

 

Retrieving An Array From Memcache In Golang

Here’s a short code example showing how to extract an array from memcache.

CustomStruct is the name of the struct the app uses, and “memcache_key” is the key that the memcache entry is stored under. C represents an appengine.Context reference. The array is extracted to the variable item_list which can then be manipulated and accessed using regular slice semantics.

//Declares the variable to unmarshal the 
//memcache item into. We need to declare 
//it first so the JSON unmarshaller 
//understands what to extract.
var item_list []CustomStruct
_, err = memcache.JSON.Get(c, "memcache_key", &item_list)
if err != nil {
    //Memcache failed to extract the list.
    c.Infof("Unable to retrieve list from Memcache: %v", err)
}