gpt4 book ai didi

scala - Akka actorSelection 失败后重新连接

转载 作者:行者123 更新时间:2023-12-01 02:22:28 25 4
gpt4 key购买 nike

我有一个 Actor ,它监视另一个 Actor (这是远程的,因此不能直接引用)。它通过使用 actorSelection 获得对 actor 的引用。和 Identify然后观察结果 ActorRef .那个有效。

但是现在我想在另一个参与者终止时自动重新连接,所以我重用了相同的 actorSelection (Actor get 在同一位置实例化),但这次通过 Identify 进行查找。永远失败。我不知道为什么它会起作用,当 Actor 已经在开始时被实例化而不是,否则。

编辑:奇怪的是,在第一次连接之前存在关联错误,而在尝试重新连接时却没有,即使远程 jvm 已完全终止。我刚刚注意到,如果您在失败后等待大约一分钟或更长时间,关联错误将返回并且连接再次成功。有没有办法配置这个(它是一个缓存?)机制。

这是标准行为还是我做错了什么?

万一我搞砸了我的代码:

object ServerMonitor {
case object Request
case class Reply(ref: ActorRef)
}

class ServerMonitor(path: String) extends Actor with ActorLogging {
import ServerMonitor._

var server: Option[ActorRef] = None
var listeners: Set[ActorRef] = Set.empty

def receive = {
case ActorIdentity("server", Some(ref)) =>
server = Some(ref)
context.watch(ref)
listeners.foreach(_ ! Reply(ref))
listeners = Set.empty
log.info(s"connected to the server at $path")

case ActorIdentity("server", None) =>
server = None
log.warning(s"couldnt reach the server at $path")
import context.dispatcher
context.system.scheduler.scheduleOnce(1 second) {
context.actorSelection(path) ! Identify("server")
}

case Terminated(ref) =>
log.warning("server terminated")
server = None
context.actorSelection(path) ! Identify("server")

case Request =>
server.fold {
listeners += sender
} { ref =>
sender ! Reply(ref)
}
}

override def preStart() {
context.actorSelection(path) ! Identify("server")
}
}

最佳答案

好的,我刚刚发现,问题是什么。有一个配置值:

# The length of time to gate an address whose name lookup has failed
# or has explicitly signalled that it will not accept connections
# (remote system is shutting down or the requesting system is quarantined).
# No connection attempts will be made to an address while it remains
# gated. Any messages sent to a gated address will be directed to dead
# letters instead. Name lookups are costly, and the time to recovery
# is typically large, therefore this setting should be a value in the
# order of seconds or minutes.
gate-invalid-addresses-for = 60 s

可以设置为低,以允许在远程系统恢复后快速重新连接。即使考虑到上述原因,60 年代对我来说似乎也高得离谱。

关于scala - Akka actorSelection 失败后重新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19251830/

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