Receive timeout
The Context.SetReceiveTimeout
defines the inactivity timeout after which the sending of a ReceiveTimeout
message is triggered. When specified, the Receive function should be able to handle an ReceiveTimeout
message.
Once set, the receive timeout stays in effect (i.e. continues firing repeatedly after inactivity periods). Pass in nil
/ null
to SetReceiveTimeout
to switch off this feature.
var props = Props.FromFunc(context =>
{
switch (context.Message)
{
case Started _:
context.SetReceiveTimeout(TimeSpan.FromSeconds(1));
break;
//this will fire after 1 second of "silence", not receiving any other messages
case ReceiveTimeout _:
Console.WriteLine("ReceiveTimeout");
break;
}
return Task.CompletedTask;
});
props := actor.PropsFromFunc(func(context actor.Context) {
switch msg := context.Message().(type) {
case *actor.Started:
context.SetReceiveTimeout(1 * time.Second)
//this will fire after 1 second of "silence", not receiving any other messages
case *actor.ReceiveTimeout:
log.Printf("ReceiveTimeout")
}
})
By setting the receive-timeout, you are setting a timer to a specific interval and the timer starts to count down from this.
Once the timer ends, it will send a ReceiveTimeout
message to itself.
And the timer resets and starts over again.
Should the actor receive any message during this time, the timer will also reset, and start counting from the specified duration again.
Meaning, ReceiveTimeout
will only be received after at not receiving any messages for at least the time of the duration.
Non-influencing messages
There is a way to still send messages to an actor without resetting the receive-timeout timer.
You can do this by marking your message as NoInfluence
.