https://metaplay.io logo
Question: Action best practice for async server pr...
# questions-and-feedback
u
Certain operations on the server are async (calling 3rd parties, EntityAsk etc.). However the PlayerAction that calls the server listener interface is not async, so I wonder if it is a good practice to use
_ = Task.Run(async () =>{});
to offload the work to the threadpool? And upon completion of the task, call another PlayerAction that uses the Client listener interface to inform the client about the result. I also saw that there was
MetaRequest/MetaResponse
in the code to query the server. But as this targets the SessionActor, I guess it is not appropriate to use if it involves updating ServerOnly fields on the Player? I was looking for more information regarding the MetaRequest/MetaResponse but couldn't find anything in the documentation. Any help to shed to some light on what would be the best way to handle this would be very appreciated! Many thanks
h
The recommended way is to offload the task to threadpool but then handle the result on actor context again. This can be done via
ContinueTaskOnActorContext(Task.Run(() => MyComputeHeavyTask(param)), result => {}, failure => {})
And yes, upon completion, you can issue a server action to update the results. Client can either react via Client Listener, or depending on the type of the action, the client can poll the result state. For example, there could be a "mailbox"-kind
[MetaMemember(123)] MyResultType PendingOperationResult = null;
in PlayerActor and then the serveraction would update this. Then the client observe the field being set, and use another action to clear the field. The benefit of this polling is that even if the session is lost during the action, the client will see the action results upon next session start. The downside is the complexity. To avoid having to build a custom mailbox, you can also use the mail system to communicate mail-like content to the client. The downside here is that it may complicate your mail management. As you've seen MetaRequest/MetaResponse does not yet support communicating with Players. The implementation is a work-in-progress and not used in public samples. The intent is to extend and then document the mechanism and provide samples for it, but the schedule for this is currently uncertain.
u
Thanks a lot for your answer. These are good things to keep in mind! I didnt know about the
ContinueTaskOnActorContext
. Will change my implementation to use this 👍