简体   繁体   English

Erlang主管程序

[英]Erlang supervisor processes

I have been learning Erlang intensively, and after finishing 'Programming Erlang' from Joe Armstrong, there is one thing that I keep coming back to. 我一直在深入学习Erlang,在完成Joe Armstrong的“ Programming Erlang”后,我又回到了一件事。

In my mind a Supervisor spawns One process per child handler. 在我看来,主管会为每个子处理程序生成一个进程。 So each declared gen_server type handler will run as a separate process. 因此,每个声明的gen_server类型处理程序将作为单独的进程运行。

What happens if you are building a tiny web server and you want each requests to be its own process. 如果您正在构建一个小型Web服务器,并且希望每个请求成为其自己的进程,将会发生什么。 Do you still conform to OTP principles and use a gen_server somehow (how ?), or do you create your own behaviour? 您是否仍然遵循OTP原则并以某种方式(如何?)使用gen_server,还是创建自己的行为?

How does Cowboy handle this for eg. 牛仔如何处理这个问题。 ? Does it still use gen_server ? 它仍然使用gen_server吗?

tl;dr: I find that trying to figure out the "correct" supervision structure a the beginning of a project is a form of premature optimization. tl; dr:我发现试图找出“正确的”监督结构是项目的开始,这是过早优化的一种形式。

The "right" way to design your supervision tree depends on what the worker parts of your application are doing. 设计监督树的“正确”方法取决于应用程序的工作人员正在做什么。 In the case of a web server I would probably first explore something along the lines of: 对于Web服务器,我可能首先会探索以下方面:

  • top supervisor (singular) 最高主管(单身)
    • data service supervisor (one per service type) 数据服务主管(每种服务类型一名)
      • worker pool (all workers under the service sup) 工人池(服务支持下的所有工人)
    • client connection supervisor (one) 客户端连接主管(一名)
      • connection worker pool (or one per connection, have to play with it to decide) 连接工作者池(或每个连接一个,必须使用它来决定)
    • logical supervisor (as appropriate -- massive variance here, depending on problem domain) 逻辑主管(视情况而定-这里存在很大差异,具体取决于问题域)
      • workers or supervisors (as appropriate -- have to explore/know the problem domain to have any idea how this should be structured) 工人或主管(视情况而定-必须探究/了解问题领域以了解如何组织)

So that's several workers per supervisor type at the lower level. 因此,每个较低级别的主管类型需要几个工人。 I haven't used Cowboy so I don't know how it is organized. 我没用过牛仔,所以我不知道它的组织方式。 The point I'm trying to make is that while the mechanics of handling data services serving web pages are relatively trivial, the part of the system that actually does the core problem-solving work might not be and this is going to dictate everything interesting about the system. 我要说明的一点是,虽然处理服务于网页的数据服务的机制比较琐碎,但实际上不能完成核心问题解决工作的系统部分可能并不重要,这将决定所有有趣的方面。系统。

It is a bad thing to have your problem-solving bits mixed in the same module as your web-displaying or connection handling bits. 将解决问题的位与Web显示或连接处理位混合在同一模块中是不好的。 Ideally you should be able to use the same logic units in a native application, a web application and a networked service without any changes. 理想情况下,您应该能够在本机应用程序,Web应用程序和网络服务中使用相同的逻辑单元,而无需进行任何更改。

Ultimately the answer to whether you should have 1:1 supervisors to workers or 1:n depends on what you're doing and what restart strategy gives you the best balance among recovery to a known consistent state, latency felt by the user, and resource usage. 最终,是否应该让工作人员拥有1:1的主管或拥有1:n的主管的答案取决于您在做什么,以及哪种重启策略可以使您在恢复到已知的一致状态,用户感觉到的延迟和资源之间达到最佳的平衡。用法。

One of my favorite things about Erlang is that I can start with a naive supervisor structure like the one above, play with it until I see where its not so good, and rather easily switch things around and experiment with alternatives without fundamentally altering my system much. 关于Erlang的我最喜欢的事情之一是,我可以从一个像上面那样的幼稚的主管结构开始,使用它直到发现不那么好的地方,然后很容易地切换周围的环境并尝试替代方案而无需从根本上改变我的系统。 (The same goes for playing with alternative data representations if you write proper abstractions around them.) So first, get something that works in testing. (如果在替代数据表示形式周围编写适当的抽象,则使用替代数据表示形式也是如此。)因此,首先,获得在测试中起作用的内容。 Then load it up and see if you can break it. 然后加载它,看看是否可以破坏它。 Then start worrying about the details, after you understand where the problems actually are. 了解问题的实质之后 ,再开始担心细节。

It is a common pattern to spawn one server per client in erlang, You will then use a supervisor using the simple_one_to_one strategy for the children servers. 这是一种常见的模式,即在erlang中为每个客户端生成一台服务器,然后您将对子级服务器使用使用simple_one_to_one策略的管理程序。 This allows to ask the server to start a server on_demand. 这允许请求服务器on_demand启动服务器。 Generally this is used when you don't know how many processes you will need, and when the processes are independent (a crash of one process should not impacts the other). 通常,当您不知道需要多少个进程,并且这些进程是独立的(一个进程的崩溃不会影响另一个进程)时,将使用此方法。

There is a very good information in the site learningyousomeerlang.com ( LYSE supervisor chapter ). 网站learningyousomeerlang.com( LYSE主管章节 )中有非常好的信息。 the whole site is worth to read. 整个网站值得一读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM