Skip to content

Instantly share code, notes, and snippets.

@stockholmux
Last active August 9, 2023 13:54
Show Gist options
  • Save stockholmux/3a4b2d1480f27df8be67 to your computer and use it in GitHub Desktop.
Save stockholmux/3a4b2d1480f27df8be67 to your computer and use it in GitHub Desktop.
Time limited call to redis
/**
* returns a function that acts like the Redis command indicated by cmd except that it will time out after a given number of milliseconds
*
* @param {string} cmd The redis commmand to execute ('get','hset','sort', etc.)
* @param {integer} timeLimit The number of milliseconds to wait until returning an error to the callback.
*
*/
function timeLimited(cmd, timeLimit) {
//We aren't defining arguments here - this will allow the flexibility of passing a variable number of arguments to Redis
return function() { //look ma, no explicit arguments
var
//arguments, even if not defined in the function declaration, are still stored in the arguments pseudo-array.
//We'll first have to convert the arguments into a real array using slice - this will allow us to push/pop/apply to it
argsAsArr = Array.prototype.slice.call(arguments),
//assume that the last argument of any given call is a callback
cb = argsAsArr.pop(),
//we'll hold the reference to the timeout in this variable
timeoutHandler;
//after the timeLimit, throw an error
timeoutHandler = setTimeout(function(){
cb(new Error('Redis timed out'));
//make cb a no-op to prevent double callback
cb = function() {};
}, timeLimit);
//since we've pop'ed off the original cb - let's add in our own replacement callback
argsAsArr.push(function(err, values){
//disable the original timeout
clearTimeout(timeoutHandler);
//call back as normal
cb(err,values);
});
//send the original arguments, with the replaced callback
client[cmd].apply(client,argsAsArr);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment