-
-
Notifications
You must be signed in to change notification settings - Fork 142
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
[http] Link between Connection layer and Request handler #414
Comments
We do, what you're looking for are connection and request timeouts. We have this feature planned for a future release. |
Nice to know. That said, it would be nice to be able to pass data along connection to request. Connection could have attributes. And connection could be injected in the request. Or exposing the 'headers' event that knows the 2 objects. Edit: I've created some kind of dynamic timeout to handle variable length request. So I'm happy with something no too polished... |
What is your use case for that? You have indirect access to all properties on the connection in the PSR-7 request. |
I've created a pseudo stream around mongodb cursor using the function futurTick() to allow the loop to retake controle. EDIT: I've done it somehow (ugly) with Closure::bind() to access private properties... $reader = static function & ($object, $property) {
$value = & \Closure::bind(function & () use ($object, $property) {
return $object->$property;
}, $object, $object)->__invoke();
return $value;
};
/** @var \React\Http\Io\StreamingServer $streamingServer */
$streamingServer = & $reader($server, 'streamingServer');
/** @var \React\Http\Io\RequestHeaderParser $parser */
$parser = & $reader($streamingServer, 'parser');
$listeners = & $reader($parser, 'listeners');
// adding in first position
\array_unshift($listeners['headers'], function (ServerRequestInterface &$request, ConnectionInterface $conn) {
$request = $request->withAttribute('conn', $conn);
}); And I use a EDIT 2: I'm putting the fake readable stream to explain, feel free to point any issue in this approach (I'm very new in non blocking coding) class GeneratorStream implements ReadableStreamInterface
{
use EventEmitterTrait;
protected $cursor;
protected $closed = false;
protected $paused = false;
protected $ticking = false;
protected $loop;
protected $chunkSize;
public function __construct(\Generator $cursor, int $chunkSize = 1000, LoopInterface $loop = null)
{
$this->cursor = $cursor;
$this->chunkSize = $chunkSize;
$this->loop = $loop ?: Loop::get();
$this->loop->futureTick([$this, 'handle']);
}
public function pause()
{
if ($this->closed || $this->paused) return;
$this->paused = true;
}
public function resume()
{
if (!$this->paused || $this->closed) return;
$this->paused = false;
$this->handle();
}
public function pipe(WritableStreamInterface $dest, array $options = [])
{
return Util::pipe($this, $dest, $options);
}
public function isReadable()
{
return !$this->closed;
}
public function close()
{
if ($this->closed) return;
$this->paused = false;
$this->closed = true;
$this->emit('close');
$this->removeAllListeners();
}
/**
* @internal
*/
public function handle()
{
if ($this->closed||$this->paused) return;
for ($i = 0; $this->cursor->valid() && (($this->chunkSize > 0 && $i < $this->chunkSize) || $this->chunkSize == 0); $this->cursor->next(), ++$i) {
$this->emit('data', [$this->cursor->current()]);
}
if ($this->cursor->valid()) {
$this->loop->futureTick([$this, 'handle']);
} else {
$this->emit('end');
$this->close();
}
}
} |
@quazardous Thanks for bringing this up. I'm not sure I follow exactly what you're trying to achieve here, can you elaborate and/or provide a gist? Your last gist implements a #324 discusses adding additional attributes to the #396 discusses ways to match the underlying TCP/IP connection to a HTTP request object by matching the source and destination addresses. Do you feel there's anything missing here? |
I'll try to reformulate. The way the read timeout is working depends on (real) streams that the loop can check on.
But in case of non stream request (Like GET)
What I need is a way to profile the timeouts at runtime because I handle different type of request:
So it would be nice to add advanced timeout capabilities (not just a general-same-timeout-for-all).
In this case the I'll try to dispatch my thoughts to the linked issues
the "advanced at runtime dynamic super timeout" could be discussed further |
The incoming response is indeed buffered by default to make it compatible with PSR-7. As per https://github.com/reactphp/http#streaming-incoming-request, you can explicitly stream the incoming response as well. In this case, you would have full control over managing the incoming stream and any timeouts yourself. Other that that, I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can always reopen this 👍 |
Hi,
I'm using this kind of code to handle connection timeouts
It's working OK. But for some GET request with long results (search) I've to cancel the timer from the request handler (because it timeouts).
But I don't know how to connect the two worlds...
The text was updated successfully, but these errors were encountered: