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

RFC-1006 Server/Client->send 支持协程化 #13

Closed
matyhtf opened this issue Mar 30, 2018 · 0 comments
Closed

RFC-1006 Server/Client->send 支持协程化 #13

matyhtf opened this issue Mar 30, 2018 · 0 comments

Comments

@matyhtf
Copy link
Member

matyhtf commented Mar 30, 2018

现状

现在 Server/Client->send 在缓存区已满的情况下,会直接返回false,需要借助onBufferFullonBufferEmpty这样复杂的事件通知机制才能实现任务的暂停和恢复。

在实现需要大量发送的场景下,现有机制虽然可以实现,但非常复杂。

思路

现在基于协程可以实现一种机制,直接在当前协程内yield,等待数据发送完成,缓存区清空时,自动resume当前协程,继续send数据。

  • Server/Client->send返回false并且错误码为SW_ERROR_OUTPUT_BUFFER_OVERFLOW时,不返回falsephp层,而是yield挂起当前协程
  • Server/Client监听onBufferEmpty事件,在该事件触发后,缓存区内的数据已被发送完毕,这时resume对应的协程
  • 协程恢复后,继续调用Server/Client->send向缓存区内写入数据,这时因为缓存区已空,发送必然是成功的

实例

改进前

for ($i = 0; $i < 100; $i++)
{
    //在缓存区塞满时会直接返回`false`
    $server->send($fd, $data_2m);
}

改进后

for ($i = 0; $i < 100; $i++)
{
    //在缓存区塞满时会 yield 当前协程,发送完成后 resume 继续向下执行
    $server->send($fd, $data_2m);
}

选项

此项特性会改变底层的默认行为,因此需要额外的一个参数来开启。

$serv->set([
    'send_yield' => true,
]);
@matyhtf matyhtf added the 草案 draft label Mar 30, 2018
@matyhtf matyhtf changed the title Server/Client->send 支持协程化 RFC-1006 Server/Client->send 支持协程化 Apr 20, 2018
@matyhtf matyhtf closed this as completed Apr 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant