Reactor Reactor single-threaded mode -----

Reactor pattern:

1.Reactor mode, pass simultaneously through one or more inputs to the service processor (event-driven)
2. The server handles a plurality of incoming requests and dispatches them to the appropriate synchronization processing thread, and therefore also Reactor pattern Dispatcher call mode
3.Reactor mode using IO multiplexing monitor events after receiving the event, distributed to a thread (process), this is the network server high concurrent processing key

Reactor core model components:

1.Reactor :
the Reactor runs in a separate thread, is responsible for monitoring and distributing event, distributed to the appropriate handler to react to IO, it is like a telephone company operator, which receive calls from customers and transferred to the line appropriate contact;
2.Handlers :
handler performs the actual event IO to complete the event, similar to the actual customers want to talk to officials in the company, Reactor to the corresponding IO event by scheduling the appropriate handler, the handler execution non-blocking operations

Reactor FIG single threaded model:

Here Insert Picture Description

Single Reactor single-threaded program description

1.Select是前面IO复用模型介绍的标准网络API,可以实现应用程序通过一个阻塞对象监听多路连接请求
2.Reactor对象通过Select监控客户端1请求事件,收到事件后通过Dispatch进行分发
3.如果是建立连接请求事件,则由Acceptor通过Accept处理连接请求,然后创建一个Handler对象处理连接完成后的后续业务处理
4.如果不是建立连接事件,则Reactor会分发调用连接对应的Handler来相应
5.Handler会完成read 业务处理,send的完整业务流程
总结:服务器端用一个线程通过多路复用搞定所有IO操作,编码简单,清晰明了,但是如果客户端连接数量较多,将无法支撑

方案优缺点分析:

优点:
模型简单,没有多线程,进程通信,竞争的问题,全部都在一个线程中完成
缺点:
性能问题:只有一个线程,无法完全发挥出多核CPU的性能,Handler在处理某个连接上的业务时,整个进程无法处理其他连接事件,很容易导致性能瓶颈
可靠性问题:线程意外终止,或者进入死循环,会导致整个系统通信模块不可用,不能接受和处理外部消息,造成节点故障.
使用场景:客户端的数量有限,业务处理非常快速,比如Redis在业务处理的时间复杂度O(1)的情况.

学习年限不足,知识过浅,说的不对请见谅。

世界上有10种人,一种是懂二进制的,一种是不懂二进制的。

发布了71 篇原创文章 · 获赞 54 · 访问量 42万+

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104202304