gpt4 book ai didi

scala - Akka:actor当前的邮箱大小或等待处理的邮件数

转载 作者:行者123 更新时间:2023-12-04 10:14:20 25 4
gpt4 key购买 nike

我试图找出等待参与者处理的待处理队列项目的数量。

我确定必须有一种方法可以从actor上下文或context.system.mailboxes等中引用

这是代码示例:

class SomeActor extends Actor {
override def receive = {

case ScanExisting => {
val queueSize = context.system.mailboxes... size ??
}

最佳答案

这种方法存在于Akka 1.x中,但在Akka 2.0中已被删除。罗兰·库恩(Roland Kuhn)撰写了详细的blog post(来自Akka用户列表中的discussion),解释了此决定的原因。以下摘录概述了查询 Actor 的邮箱大小时的一些问题:

  • it takes O(n) time to get some size answer from a concurrent queue, i.e. querying hurts when you will feel the pain the most (it might even take several seconds in case of durable mailboxes at the “wrong moment”)

  • the answer is not correct, i.e. it does not need to match the real size at either the beginning or the end of processing this request

  • making it “more correct” involves book-keeping which severely limits scalability

  • and even then the number might have changed completely by the time you receive it in your code (e.g. your thread is scheduled out for 100ms and you get 100.000 new messages during that time).


阅读整篇文章,然后重新考虑获取 Actor 邮箱大小的原因。如果您仍然想要它:

In case you cannot do without, it is quite easy to write your own mailbox implementation, building on the traits in the akka.dispatch package and inserting book-keeping code into enqueue() and dequeue(). Then you could either use down-casting (evil) or keep track of your mailboxes in an akka.actor.Extension (recommended) to access the stats from within your actor and do whatever is necessary.

But wait: did I mention that it might even be easier to tag latency-critical (but not too high frequency) messages with timestamps and react on the age of a message when processing it?

So, in summary: while there still is a way to get the mailbox size, you will probably never actually need it.


如果您希望角色邮箱大小用于监视目的,请查看 Lightbend TelemetryKamon。另外,Patrik Nordwall编写了一个 gist,当邮箱大小超过配置的限制时会记录该邮箱大小。

关于scala - Akka:actor当前的邮箱大小或等待处理的邮件数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47601619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com