Skip to content
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

Dynamic loaders #577

Open
mastir opened this issue Nov 7, 2024 · 1 comment
Open

Dynamic loaders #577

mastir opened this issue Nov 7, 2024 · 1 comment

Comments

@mastir
Copy link

mastir commented Nov 7, 2024

Hey @romm, after a while i came back to the same performance issue. My app works in microservice enviroment and simplified workflow is:

  • start thread
  • preload services (including valinor)
  • connect network services
  • get preloaded modules
  • inject modules autolaoders and run bootstrap
  • process requests (some requests can load additional modules)
  • after 500 requests or 100 preloaded modules thread restarts

Atm i recreate mapper and normalizer after every module loaded.
Example intrface:

#[TypedInterface([
    'column.add' => AddColumnDto::class,
    'column.drop' => DropColumnDto::class,
    'column.rename' => RenameColumnDto::class,
    'relation.add' => AddRelationDTO::class,
    'relation.drop' => DropRelationDto::class,
])]
interface AlterOperation {}

and methods to add interfaces like this in MapperBuilder:

    public function onModuleLoaded(string $namespace){
        $updated = false;
        foreach($this->findInterafaces($namespace) as $interfaceName){
            $attr = $this->readAttributes($interfaceName);
            if (!$attr) continue;
            $this->mapperBuilder = $this->mapperBuilder->infer($this->generateInfer($attr->param, $attr->map));
            $updated = true;
        }
        if ($updated){
            $this->mapper = $this->mapperBuilder->mapper();
            $this->normalizer = $this->mapperBuilder->normalizer(Format::array());
        }
    }

    public function generateInfer(string $param_name, array $classmap){
        $tmpfile = tempnam(sys_get_temp_dir(), 'valinor');
        $variants = implode(' | ', $classmap);
        $mapdump = var_export($classmap, true);
        $content = <<<EOT
\$classmap = $mapdump;
/**
 * @return class-string<$variants>
 */
return fn (string \$$param_name) use (\$classmap) => \$classmap[\$$param_name] ?? 'Unknown'.\$$param_name;
EOT;
        file_put_contents($tmpfile, $content);
        $method = include $tmpfile;
        unlink($tmpfile);
        return $method;
    }

I tried to implement some more generic solution in #560 with no success. is there a better way to implement it?

@romm
Copy link
Member

romm commented Nov 7, 2024

Hi @mastir, thanks for the detailed issue. I now see the problem and will be thinking about a solution. Please be patient, I don't have much time these days but I'll definitely find a solution at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants