为什么我们需要simple_one_for_one? [英] Why do we need simple_one_for_one?

查看:140
本文介绍了为什么我们需要simple_one_for_one?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,simple_one_for_one对聊天应用程序非常有用,因为每个聊天客户端都是一个服务器进程(gen_server).这是正确的吗?

Somebody told me that simple_one_for_one is very useful for chat applications, because each chat client is a server process (gen_server). Is this right?

我想知道为什么我们需要它吗?为什么不只创建一个中心服务器(gen_server)来处理所有聊天客户端通信?因为也许聊天客户端的数量很大,所以只有一台服务器无法快速处理,这会使系统变慢吗?

And I wonder why do we need it? Why not create only one center server (gen_server) to handle all chat client communication? Because maybe the number of chat clients is very large so only one server couldn't handle fast, make the system slow down?

我认为也许创建过多的服务器(例如simple_one_for_one)可能会占用过多的系统资源.我是OTP的新手,所以我确实需要对此进行解释.

I think maybe creating too many servers like simple_one_for_one may take too much system resource. I'm a new OTP guy, so I really need explanation about this point.

推荐答案

是的,您的想法是每个客户端都有一个进程(gen_server).

Yes, the idea is that you would have a process (gen_server) per client.

这使您可以将一个客户端的故障与另一个客户端隔离.

This lets you isolate failure of one client from another.

如果每个人都在一个流程中,则必须非常小心地处理所有可能出错并导致流程崩溃的事情(因此,断开所有客户端的连接).

If you had everyone in a single process, you have to be very careful to handle all the things that might go wrong and crash you process (thus, disconnecting all your clients).

每个客户端只有一个进程,您可以为幸福的路径编写代码,并在出现问题时让它崩溃.最糟糕的情况是您放弃了一个客户.

With one process per client, you can code for the happy path and just let it crash when things go wrong. Worst case is you drop a single client.

进程相当便宜(类似于创建线程).在现代机器上,您可以拥有数百万.

Processes are fairly cheap (nothing like creating threads). On a modern machine you can have millions.

如果您的用户群数以百万计,那么我肯定您最终将拥有不止一台服务器.因此,可以轻松地将一个盒子上的数十万到几百万扩展的东西就足够了.

If your user base is in the many millions, I'm sure you'd end up with more than one server anyway. So something that can easily scale to the hundreds of thousands to low millions on a box is plenty.

这篇关于为什么我们需要simple_one_for_one?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆