Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventLoop 工作原理以及 Golang 实现 #14

Open
JemmyH opened this issue Jul 26, 2021 · 1 comment
Open

EventLoop 工作原理以及 Golang 实现 #14

JemmyH opened this issue Jul 26, 2021 · 1 comment
Assignees
Labels
Digging In I'm on it. documentation Improvements or additions to documentation Implementation by Go

Comments

@JemmyH
Copy link
Owner

JemmyH commented Jul 26, 2021

Redis 单线程事件循环 中我们提到,Redis 自己实现了一个高性能事件库:aeEventLoop,本次借助 Allenxuxu/gev 的开源实现,希望能明确一个事件库的工作流程,并通过源码分析的方式理解其原理。

@JemmyH JemmyH self-assigned this Jul 26, 2021
@JemmyH JemmyH added Digging In I'm on it. documentation Improvements or additions to documentation Implementation by Go labels Jul 26, 2021
Repository owner locked and limited conversation to collaborators Jul 26, 2021
@JemmyH
Copy link
Owner Author

JemmyH commented Jul 30, 2021

在一个事件驱动的程序中,通常有一个主循环来监听事件,当对应的事件发生时,会调用设定好的回调函数。在前面的 issue 中,我们了解了 Reactor 模型,了解到它的核心是:

Reactor线程模型 = Reactor(I/O 多路复用) + 线程池

它有三个角色:ReactorAcceptorHandler。通常,EventLoop 是和 Reactor配合使用的,EventLoop 的角色通常是:

  1. Reactor 模型中的事件处理器;
  2. 是一个单独的线程,内部会维护一个 I/O多路复用器TaskQueue 任务队列,分别处理 I/O 事件和任务;

TaskQueue 是多生产者单消费者的任务队列,在多线程并发添加任务时,这种队列可以保证线程安全;
I/O 事件 即是往 I/O 多路复用中注册的关心的事件。

所以,我们可以说,EventLoop 单线程运行,循环往复执行三个动作:

  • selector 事件轮训
  • I/O 事件处理
  • 任务处理

接下来我们看看具体的实现。

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Digging In I'm on it. documentation Improvements or additions to documentation Implementation by Go
Projects
None yet
Development

No branches or pull requests

1 participant