From e58d46d3c565db24c17428bf42a75d55fedd7bc6 Mon Sep 17 00:00:00 2001 From: Daniel Lowrey Date: Mon, 3 Nov 2014 23:49:19 -0500 Subject: [PATCH] Disable read watcher when no resolve operations pending --- lib/Client.php | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/Client.php b/lib/Client.php index 9e8aed1..6918357 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -83,6 +83,11 @@ class Client { */ private $serverPort = 53; + /** + * @var bool + */ + private $isReadWatcherEnabled = false; + /** * @param \Amp\Reactor $reactor * @param \Amp\Dns\RequestBuilder $requestBuilder @@ -121,18 +126,23 @@ class Client { )); } - $future = new Future($this->reactor); + if (!$this->isReadWatcherEnabled) { + $this->isReadWatcherEnabled = true; + $this->reactor->enable($this->readWatcherId); + } + + $promisor = new Future($this->reactor); $id = $this->getNextFreeLookupId(); $this->pendingLookups[$id] = [ 'name' => $name, 'requests' => $this->getRequestList($mode), 'last_type' => null, - 'future' => $future, + 'future' => $promisor, ]; $this->processPendingLookup($id); - return $future->promise(); + return $promisor->promise(); } private function connect() { @@ -142,9 +152,10 @@ class Client { } stream_set_blocking($this->socket, 0); + $this->readWatcherId = $this->reactor->onReadable($this->socket, function() { $this->onReadableSocket(); - }); + }, $enableNow = false); return true; } @@ -231,12 +242,7 @@ class Client { $this->pendingRequestsById[$id], $this->pendingRequestsByNameAndType[$name][$request['type']] ); - /* - if (!$this->pendingRequestsById) { - $this->reactor->cancel($this->readWatcherId); - $this->readWatcherId = null; - } - */ + // Interpret the response and make sure we have at least one resource record $interpreted = $this->responseInterpreter->interpret($response, $request['type']); if ($interpreted === null) { @@ -280,6 +286,11 @@ class Client { $code = $type )); } + + if (empty($this->pendingLookups)) { + $this->isReadWatcherEnabled = false; + $this->reactor->disable($this->readWatcherId); + } } private function completeRequest($request, $addr, $type) {