use defer to reclaim resources

Signed-off-by: reasno <guxi99@gmail.com>
This commit is contained in:
reasno 2020-04-19 23:31:31 +08:00
parent e5a94460b9
commit c97899db7f

View File

@ -189,18 +189,20 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
{ {
$this->logger->debug(sprintf('WebSocket: fd[%d] closed.', $fd)); $this->logger->debug(sprintf('WebSocket: fd[%d] closed.', $fd));
Context::set(WsContext::FD, $fd);
$fdObj = FdCollector::get($fd); $fdObj = FdCollector::get($fd);
if (! $fdObj) { if (! $fdObj) {
return; return;
} }
Context::set(WsContext::FD, $fd);
defer(function () use ($fd) {
// Move those functions to defer, because onClose may throw exceptions
FdCollector::del($fd);
WsContext::release($fd);
});
$instance = $this->container->get($fdObj->class); $instance = $this->container->get($fdObj->class);
if ($instance instanceof OnCloseInterface) { if ($instance instanceof OnCloseInterface) {
$instance->onClose($server, $fd, $reactorId); $instance->onClose($server, $fd, $reactorId);
} }
FdCollector::del($fd);
WsContext::release($fd);
} }
/** /**