Skip to content
Snippets Groups Projects
Unverified Commit 4e68df80 authored by Jaime Pérez Crespo's avatar Jaime Pérez Crespo
Browse files

Enhance the performance of SimpleSAML\Module.

The issue here is that every time we need to list the modules or check if they are enabled, we just iterate over the modules directory and subdirectories, which is terribly expensive. Instead of doing so, we build a cache of modules specifying if they are enabled or not. In the end, this is also fixing another issue, given that enabling/disabling a module in the middle of a request being processed could lead to inconsistencies and unexpected behaviour (likely exceptions and horrible crashes). Modules should be checked in the beginning of a request and their state (enabled/disabled) frozen until the request is processed to avoid that, and this is the way to achieve so.

Additionally, we take the chance to check if modules are enabled when we search for them. This reduces the processing time to around a third of the original without this fix.
parent 0965bf14
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,14 @@ namespace SimpleSAML;
class Module
{
/**
* A cache containing the modules currently installed. Each key in the array is the module name, and the value is
* a boolean telling if the module is enabled or not.
*
* @var array
*/
private static $modules = array();
/**
* Autoload function for SimpleSAMLphp modules following PSR-0.
*
......@@ -124,6 +132,9 @@ class Module
*/
public static function isModuleEnabled($module)
{
if (isset(self::$modules[$module])) {
return self::$modules[$module];
}
$moduleDir = self::getModuleDir($module);
......@@ -135,7 +146,7 @@ class Module
$moduleEnable = $globalConfig->getArray('module.enable', array());
if (isset($moduleEnable[$module])) {
if (is_bool($moduleEnable[$module]) === true) {
if ($moduleEnable[$module] === true) {
return $moduleEnable[$module];
}
......@@ -170,6 +181,9 @@ class Module
*/
public static function getModules()
{
if (!empty(self::$modules)) {
return array_keys(self::$modules);
}
$path = self::getModuleDir('.');
......@@ -178,8 +192,6 @@ class Module
throw new \Exception('Unable to open module directory "'.$path.'".');
}
$modules = array();
while (($f = readdir($dh)) !== false) {
if ($f[0] === '.') {
continue;
......@@ -189,12 +201,12 @@ class Module
continue;
}
$modules[] = $f;
self::$modules[$f] = self::isModuleEnabled($f);
}
closedir($dh);
return $modules;
return array_keys(self::$modules);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment