gpt4 book ai didi

scala - Akka + WithinTimeRange

转载 作者:行者123 更新时间:2023-12-01 10:38:36 25 4
gpt4 key购买 nike

我已经测试了 akka 的容错系统,到目前为止,在谈论根据指定的 maxNrOfRetries 重试发送消息时,它一直很好。

但是,它不会在给定的时间范围内重新启动 actor,它会立即重新启动,忽略时间范围内的内容。

我尝试了 AllForOneStrategy 和 OneForOneStrategy 但没有改变任何东西。

尝试关注这篇博文:http://letitcrash.com/post/23532935686/watch-the-routees ,这是我一直在使用的代码。

class Supervisor extends Actor with ActorLogging {

var replyTo: ActorRef = _

val child = context.actorOf(
Props(new Child)
.withRouter(
RoundRobinPool(
nrOfInstances = 5,
supervisorStrategy =
AllForOneStrategy(maxNrOfRetries = 3, withinTimeRange = 10.second) {
case _: NullPointerException => Restart
case _: Exception => Escalate
})), name = "child-router")

child ! GetRoutees

def receive = {
case RouterRoutees(routees) =>
routees foreach context.watch

case "start" =>
replyTo = sender()
child ! "error"

case Terminated(actor) =>
replyTo ! -1
context.stop(self)
}
}

class Child extends Actor with ActorLogging {

override def preRestart(reason: Throwable, message: Option[Any]): Unit = {
log.info("***** RESTARTING *****")
message foreach{ self forward }
}

def receive = LoggingReceive {
case "error" =>
log.info("***** GOT ERROR *****")
throw new NullPointerException
}
}

object Boot extends App {

val system = ActorSystem()
val supervisor = system.actorOf(Props[Supervisor], "supervisor")

supervisor ! "start"

}

我做错了什么吗?

编辑

其实我误解了withinTimeRange的用途。要在某个时间范围内安排我的重试,我正在执行以下操作:

override def preRestart(reason: Throwable, message: Option[Any]): Unit = {
log.info("***** RESTARTING *****")
message foreach { msg =>
context.system.scheduler.scheduleOnce(30.seconds, self, msg)
}
}

它似乎工作正常。

最佳答案

我认为您误解了 withinTimeRange 参数的用途。该值应该与 maxNrOfRetries 结合使用,以提供一个窗口,在该窗口中支持限制重试次数。例如,正如您所指定的,这意味着如果某个 child 需要在 10 秒内重启超过 3 次,主管将不再重启该 child 。

关于scala - Akka + WithinTimeRange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31877943/

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