Skip to content

Commit

Permalink
Remove temp. DNS host cache
Browse files Browse the repository at this point in the history
We now have a better DNS cache solution found within the Symfony layer, which handles any hostname lookup.
  • Loading branch information
eddmann committed Nov 23, 2021
1 parent e14b5ef commit 8da35c4
Showing 1 changed file with 1 addition and 28 deletions.
29 changes: 1 addition & 28 deletions runtime/lib/Propel.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ public static function initConnection($conparams, $name, $defaultClass = Propel:
}

try {
$con = new $classname(self::resolveCachedDsnHost($dsn), $user, $password, $driver_options);
$con = new $classname($dsn, $user, $password, $driver_options);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PropelPDO::PROPEL_ATTR_CONNECTION_NAME, $name);

Expand Down Expand Up @@ -944,33 +944,6 @@ public static function isInstancePoolingEnabled()
{
return self::$instancePoolingEnabled;
}

/**
* Within Lambda we are unable to easily cache DNS records as we do on conventional servers.
* As each request creates a new connection, the amount of DNS queries we perform can mount up.
* This adds an application-level cache for storing the resolved IP of the desired host (env: CACHE_PDO_HOST_DNS).
*/
private static function resolveCachedDsnHost(string $dsn, int $ttlInSeconds = 300): string
{
$hostname = \getenv('PROPEL_CACHE_PDO_HOST_DNS');

if (!$hostname || false === \strpos($dsn, $hostname)) {
return $dsn;
}

if (false === \function_exists('apcu_entry')) {
return $dsn;
}

$ip = \apcu_entry($hostname, function (string $name): string {
if (\class_exists('\MyBuilder\Bundle\LogBundle\Metrics')) {
\MyBuilder\Bundle\LogBundle\Metrics::increment('pdo.dns-cache.miss', ['host' => $name]);
}
return \gethostbyname($name);
}, $ttlInSeconds);

return \str_replace($hostname, $ip, $dsn);
}
}

// Since the Propel class is not a true singleton, this code cannot go into the __construct()
Expand Down

0 comments on commit 8da35c4

Please sign in to comment.