Created
January 5, 2012 21:08
-
-
Save AlexJReid/1567288 to your computer and use it in GitHub Desktop.
parallel requests to SimpleDB in node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
simpledb = require('simpledb'), | |
Step = require('step'), | |
sdb = new simpledb.SimpleDB({ | |
keyid: 'your aws key', | |
secret: 'your aws secret' | |
}), | |
app = express.createServer(express.logger()), | |
REQUESTS = 50; | |
// Query SimpleDB | |
function sdb_request(callback) { | |
sdb.select("select field from INTERACTIONS", | |
function(error, result, meta) { | |
callback(error, result); | |
}); | |
} | |
// 50 calls guaranteed to return in order, when one completes another is made | |
app.get('/sync', | |
function(request, response) { | |
var start = new Date().getTime(); | |
function looper(incr) { | |
// We loop using this recursive function. | |
if (incr == REQUESTS) { | |
// Stopping condition | |
var end = new Date().getTime(); | |
var time = end - start; | |
// Exit here | |
response.send(REQUESTS + " synchronous calls to SimpleDB took: " + time); | |
} else { | |
sdb_request(function(err, result) { | |
if (err) { | |
throw err; | |
} else { | |
looper(incr + 1); | |
// call again! | |
} | |
}); | |
} | |
} | |
looper(0); // start loop @ 0 | |
} | |
); | |
// 50 parallel calls, may not return in order, final function called when done with each response | |
app.get('/parallel', | |
function(request, response) { | |
var start = new Date().getTime(); | |
Step( | |
function() { | |
for (var i = 0; i < REQUESTS; i++) { | |
sdb_request(this.parallel()); // special callback provided by Step | |
} | |
}, | |
function(err, result1, result2, result3) { // etc, there will be 51 params | |
// each result is available as an argument to the final function, see this.arguments | |
if (err) { | |
throw err; | |
} | |
var end = new Date().getTime(); | |
var time = end - start; | |
// Exit here | |
response.send(REQUESTS + " parallel calls to SimpleDB took: " + time); | |
} | |
); | |
} | |
); | |
var port = process.env.PORT || 3000; | |
app.listen(port, | |
function() { | |
console.log("Listening on " + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment