I think that most of your answer depends on what
unsafeCallToSomeRemoteService does, and why the remote service is
unreliable. Can you provide more information about it?

2014-04-17 10:30 GMT+02:00 Alexey Petrushin <[email protected]>:
> Yes, I'm agree with your notes about the code and preventing callback from
> being called twice.
>
> But what about memory and descriptors leaks, is there a way to terminate
> hanging callback and clear it? Or I can just safely ignore those issues and
> node.js somehow will clear it internally?
>
>
> On Thursday, 17 April 2014 12:21:56 UTC+4, Francesco Mari wrote:
>>
>> Maybe you also want to prevent callback to execute twice if the timer
>> triggers and the remote service sends a response after the time limit.
>>
>> function safeCallToSomeRemoteService(callback) {
>>   var done = callback;
>>
>>   var timer = setTimeout(function() {
>>     var temp = done;
>>     done = function () {};
>>     temp(new Error('Remote service timed out!'));
>>   }, 3000);
>>
>>   unsafeCallToSomeRemoteService(function(err, data){
>>     clearTimeout(timer);
>>     done(err, data);
>>   });
>> }
>>
>> 2014-04-17 2:06 GMT+02:00 Ryan Graham <[email protected]>:
>> >
>> > On Wed, Apr 16, 2014 at 11:17 AM, Alexey Petrushin
>> > <[email protected]> wrote:
>> >>
>> >> Is it possible to do something like this?
>> >>
>> >>     function safeCallToSomeRemoteService(callback){
>> >>       var remoteServiceResponded = false
>> >>       unsafeCallToSomeRemoteService(function(err, data){
>> >>         remoteServiceResponded = true
>> >>         callback(err, data)
>> >>       })
>> >>
>> >>       setTimeout(function(){
>> >>         if(!remoteServiceResponded){
>> >>           somehow kill callback waiting for remote service
>> >>           callback(new Error("Remote service hanged!"))
>> >>         }
>> >>       }, 3000)
>> >>     }
>> >>
>> >> Or, if it's not possible, is it safe to just ignore such hanged
>> >> callbacks,
>> >> call callback on timeout and move on (while leaving hanging callback
>> >> active)?
>> >
>> >
>> > Ignoring whether there are better options available through the API you
>> > are
>> > using, the generic timeout code would look something like this:
>> >
>> >     function safeCallToSomeRemoteService(callback) {
>> >       var timer = setTimeout(function() {
>> >         callback(new Error('Remote service timed out!'));
>> >       }, 3000);
>> >
>> >       unsafeCallToSomeRemoteService(function(err, data){
>> >         clearTimeout(timer);
>> >         callback(err, data);
>> >       });
>> >     }
>> >
>> > ~Ryan
>> >
>> > --
>> > http://twitter.com/rmgraham
>> >
>> > --
>> > --
>> > Job Board: http://jobs.nodejs.org/
>> > Posting guidelines:
>> > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> > You received this message because you are subscribed to the Google
>> > Groups "nodejs" group.
>> > To post to this group, send email to [email protected]
>> > To unsubscribe from this group, send email to
>> > [email protected]
>> > For more options, visit this group at
>> > http://groups.google.com/group/nodejs?hl=en?hl=en
>> >
>> > ---
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "nodejs" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to [email protected].
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to