Proper pattern for nested asks

☆樱花仙子☆ 提交于 2019-12-03 21:28:37

In general Ask should be used only for the communication with an actor from external services, almost never between two actors. It's a lot more expensive than using Tell. Additional problem is to use Task.WaitAll which actually blocks current thread until all responses will arrive, which is also bad for performance and may end up with deadlock.

Similar thread was already discussed on github.

General solution for aggregation problems is:

  • Create a separate actor for aggregation process.
  • Initialize it with list of actors, it is supposed to collect data from and remember the actor, which will be informed with collected result.
  • Send request/query for each actor.
  • Handle each request/query response, aggregating it in separate data structure and remove a sender from the list of awaited actors.
  • Once there is no actor to await for - send a result and stop current actor (the one responsible for data aggregation).
  • Attach ReceiveTimeout mechanism in case, when for some reason not all actors are able to respond in reasonable time - when timeout will hit, you may return failure or list of responses collected so far.

PS: Don't use TypedActor - it's also bad for performance and is/will become obsoleted.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!