Remote actor timeout cancellation #6274
-
Is there any way for a remote actor, receiving a message with Ask, to know the timeout that was set? For instance, if an actor is fetching data from a 3rd party api that accepts a cancellation token, is it possible for the actor to trigger the cancellation on timeout? It seems that only the calling actor knows about the timeout exception. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@netclectic to do this, you would need to incorporate the request time into the content of the message itself. In some of my applications and our client's applications I will often include a simple // Deadline struct in C# computed from the current time and the timeout
// value. The deadline is used to determine if a request has timed out.
public readonly struct Deadline
{
public Deadline(TimeSpan timeout)
{
Timeout = timeout;
DeadlineTime = DateTime.UtcNow + timeout;
}
public TimeSpan Timeout { get; }
public DateTime DeadlineTime { get; }
public bool IsOverdue => DeadlineTime < DateTime.UtcNow;
} I include that in the properties of my message and then check to see if the message is overdue - if it is, I don't bother fully processing it. I have a sample that does this here: https://github.com/Aaronontheweb/AkkaStreams.Demo.HttpClient/blob/2c9dcb0394839cfe4c8541c60dc9ef8e3b66ca3a/StreamStages/RequestsWithDeadline.cs Does that help @netclectic ? |
Beta Was this translation helpful? Give feedback.
@netclectic to do this, you would need to incorporate the request time into the content of the message itself.
In some of my applications and our client's applications I will often include a simple
readonly struct Deadline
that looks like this:I i…