Skip to content

Commit

Permalink
Merge pull request #226 from clue-labs/caching-dns
Browse files Browse the repository at this point in the history
Default to using DNS cache (with max 256 entries) for Connector
  • Loading branch information
jsor authored Mar 11, 2020
2 parents 4fbe726 + c732009 commit 550c336
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct(LoopInterface $loop, array $options = array())
}

$factory = new DnsFactory();
$resolver = $factory->create(
$resolver = $factory->createCached(
$server,
$loop
);
Expand Down
36 changes: 36 additions & 0 deletions tests/FunctionalConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ public function connectionToTcpServerShouldSucceedWithLocalhost()
$server->close();
}

/**
* @group internet
*/
public function testConnectTwiceWithoutHappyEyeBallsOnlySendsSingleDnsQueryDueToLocalDnsCache()
{
$loop = Factory::create();

$socket = stream_socket_server('udp://127.0.0.1:0', $errno, $errstr, STREAM_SERVER_BIND);

$connector = new Connector($loop, array(
'dns' => 'udp://' . stream_socket_get_name($socket, false),
'happy_eyeballs' => false
));

// minimal DNS proxy stub which forwards DNS messages to actual DNS server
$received = 0;
$loop->addReadStream($socket, function ($socket) use (&$received) {
$request = stream_socket_recvfrom($socket, 65536, 0, $peer);

$client = stream_socket_client('udp://8.8.8.8:53');
fwrite($client, $request);
$response = fread($client, 65536);

stream_socket_sendto($socket, $response, 0, $peer);
++$received;
});

$connection = Block\await($connector->connect('example.com:80'), $loop);
$connection->close();
$this->assertEquals(1, $received);

$connection = Block\await($connector->connect('example.com:80'), $loop);
$connection->close();
$this->assertEquals(1, $received);
}

/**
* @test
* @group internet
Expand Down

0 comments on commit 550c336

Please sign in to comment.