如果尝试写入不再存在的客户端,远程参与者框架会做什么?
我有一个服务器,它使用远程参与者框架与多个客户端进行通信。 正如这个问题中提到的,我无法跟踪何时一个客户消失了。 因此,我的服务器仍在尝试向不存在的客户端发送消息。
- 这是一个问题吗? (我没有看到任何异常被抛出 - 但我认为如果我的服务器寿命较长,就会出现内存问题)
- 如何检测消息不再发送到客户端听? (如果我想实现某种连接清理)
I have a server which is using the remote actors framework to communicate with multiple clients. As mentioned in this question, I am having trouble keeping track of when a client disappears. Hence my server is still attempting to send messages to non-existent clients.
- Is this a problem? (I don't see any exceptions being thrown - but I assume there'll be memory issues if my server is long-lived)
- How can I detect that a message is being sent to a client no longer listening? (If I want to implement some kind of connection clean-up)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我会在这里冒险。
免责声明:尽管您将在下面阅读,您可能需要从这里开始.
我对 scala 不熟悉,但它使用与 Erlang 类似的原理,我在那里感觉有点舒服。 然而,让我对你的问题感到困惑的事情与此无关。 但更多的是您似乎正在建立的客户端-服务器关系。
根据定义,服务器不会继续向客户端发送消息,除非它收到请求。 也就是说,服务器参与者类应该围绕检查接收到的消息并采取相应行动的 case 语句构建(不一定总是)。 因此,在这种情况下,客户端是存在的,服务器不应该担心它,而是正常发送其回复消息,因为客户端只是通过请求来了解其存在。
因此,如果您的服务器仍在尝试向客户端发送消息,尽管这些客户端已关闭,但在我看来,尽管没有收到任何请求,但它仍在尝试发送消息。 在客户端-服务器关系的上下文中,这是根本错误的。 服务器应该只对请求做出反应。 否则它就会扮演客户的角色。
无论如何,您可以(并且您应该)定义任何客户端-服务器对话的结束。 这将有助于释放资源并结束已建立的连接。 为了达到这个效果:
你的客户端actor应该向服务器发送一条stop消息,并使用scala为actor类实现的任何退出函数来终止它的执行。
收到停止消息后,服务器参与者不应回复客户端。 相反,它应该像客户端 Actor 一样进行清理(如果有)并终止执行。
通过这种方式,您可以确保正确终止所有涉及的参与者并正确释放资源。
我希望这能有所帮助。 不幸的是我对scala本身并不熟悉。 否则我可以引入一些代码。 但希望上面的链接应该有所帮助。
OK, I'll risk something here.
disclaimer: despite what you will read below, you may want to start here.
I'm not familiar with scala, but it uses similar principles to Erlang's and I feel somewhat comfortable there. The thing that is confusing me about your question however, has little to do with this. But more with the client-server relationship you seem to be establishing.
By definition, a server will not keep sending messages to the client unless it receives a request. That is, the server actor class should be built (not necessarily always) around case statements that check the received message and act accordingly. So, in this context the client is existing and the server shouldn't worry about it and instead send its reply message normally, because the client just made its presence aware with a request.
So if your server is still trying to send messages to clients, despite these having been closed down, seems to me it is trying to send messages despite having received no request. This is fundamentally wrong in the context of a client-server relationship. The server should only react on request. Otherwise it is taking on the role of the client.
In any case, you can (and you should) define an end to any client-server conversation. This will help release resources and end established connections. For that effect:
Your client actor should send a stop message to the server and terminate its execution with whatever exit function scala has implemented for the actor class.
On receiving a stop message, the server actor should not reply to the client. Instead it should do its cleanup (if any) and terminate execution similarly to the client actor.
This way you ensure proper termination of all actors involved and a correct release of resources.
I hope this helped somehow. Unfortunately I'm not familiar with scala itself. Otherwise I could bring in some code. But the above link should help, hopefully.