How to implement a Timeout on a port

Jan 20, 2011 at 12:27 PM

Hi Thomas,

Is it possible to setup a timeout on a Port? I am having a sporadic problem where a message is sent to a worker to act on in an Iterator, the method starts but then does not finish and I get no exceptions. I am assuming there is some error somewhere I am not trapping but this could be a bonafide situation if someone shut the worker down halfway through processing so what I want to be able to do is provide a timeout that says after x minutes and nothing on the repsonse port, run some code that will clean up this message (in our database).

Any ideas on how to do this gratefully received

Many thanks in advance


Jan 21, 2011 at 12:07 PM

Hi Mike,

We have also come across this scenario as a typical pattern for remote communication. Currently there is no special functionality for this in the appspace, but you can expect that we will deal with this in a future version.

For now, a possibility is to use the CCR for timeout handling. The DispatcherQueue provides a method for enqueuing a timer which posts a datetime to a port after a specified amount of time has passed:

var resultTimeoutPort = new Port<DateTime>();
.EnqueueTimer(TimeSpan.FromSeconds(RESULT_TIMEOUT), resultTimeoutPort);

You can then e.g. lay a choice Arbiter over your combination of responsePort, errorPort and timeoutPort. Or if you are using iterators, you could do something like this:

var responsePort = new PortSet<int, Exception, DateTime>();
space.DefaultDispatcherQueue.EnqueueTimer(TimeSpan.FromSeconds(5),  responsePort);
mathWorker.PostWithCausality(new Division { Dividend = 6, Divisor = 2, ResponsePort = responsePort }, responsePort);
yield return responsePort.Choice();

Another possibility would be to write your own timer that posts a TimeoutException to your error port when the timeout expires. But the CCR solution is probably the most simple one right now.

Hope I could help you!