How to set a Max Execution Time for Each Request Without Sub-Processes

1,063 views
Skip to first unread message

Eric Fong

unread,
Dec 1, 2010, 11:16:53 PM12/1/10
to nodejs
Would like to set a Max Execution Time for Each Request and return the
temp result to the child on time.

How can I do that without create a sub-process?

Can I stop or signal a Script.runInNewContext context that they are
timeout and need to return something?

Is that possible?

Thanks

Preston Guillory

unread,
Dec 2, 2010, 1:44:08 AM12/2/10
to nod...@googlegroups.com
var max_execution_time = 30; // seconds

http.createServer(function(request, response) {
    var timeout = setTimeout(function() {
        timeout = null
        response.writeHead(500);
        response.end('Timed out!');
    }, max_execution_time*1000);

    do_some_stuff(function() {
        if (timeout) {
            clearTimeout(timeout);

            response.writeHead(200);
            response.end('Finished in time.');
        }
    });

    function do_some_stuff(callback) {
        // we have 30 seconds to call callback()
    }
}).listen(8000);

Eric Fong

unread,
Dec 2, 2010, 1:52:49 AM12/2/10
to nodejs
Hi

Consider do_some_stuff call db and do some heavy map and reduce
function.
which adding check if (timeout) inside those function is not possible.

When it is timeout, any way to kill the map and reduce operation like
killing a thread or process?

Eric

Preston Guillory

unread,
Dec 2, 2010, 3:10:50 AM12/2/10
to nod...@googlegroups.com
There are no threads in Node, but still you have several approaches available here.

1. Run the map/reduce work in a child process, like you said.  If it times out, kill the process.  You'd lose a lot of performance -- starting each process is expensive, and each concurrent request means another process hogging the CPU.  Maybe you can afford the performance hit.  Depends on what you're doing.

2. Sprinkle the map/reduce code with checks, like if (timeout) return.  It's probably the least performance overhead, but it'll sacrifice code cleanliness.  Anyway you said it's impossible in this case.

3. Library-specific controls.  I can imagine a map/reduce library being implemented with pause() and resume() functions.  Wouldn't that be cool?  The downside is I just made it up, so it doesn't exist, as far as I know.  (Would love to be corrected on that.)

I guess that's kind of a limited definition of "several".

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Preston Guillory

unread,
Dec 2, 2010, 3:16:43 AM12/2/10
to nod...@googlegroups.com
Does the work you're doing fit in the node.io framework?  It supports timeouts out of the box.
Reply all
Reply to author
Forward
0 new messages