function setRequestTimer($topic, $id, $request, $timeout)
{
if ($timeout > 0) {
$self = $this;
$topics = $this->getTopics($topic);
$future = new Future();
$timer = $this->timer->setTimeout(function () use($future) {
$future->reject(new TimeoutException('timeout'));
}, $timeout);
$request->whenComplete(function () use($self, $timer) {
$self->timer->clearTimeout($timer);
})->fill($future);
return $future->catchError(function ($e) use($self, $topics, $topic, $id) {
if ($e instanceof TimeoutException) {
$checkoffline = function () use($self, &$checkoffline, $topics, $topic, $id) {
$t = $topics[$id];
$t->timer = $self->timer->setTimeout($checkoffline, $t->heartbeat);
if ($t->count < 0) {
$self->offline($topics, $topic, $id);
} else {
--$t->count;
}
};
$checkoffline();
}
});
}
return $request;
}