Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b08fdb1801 | |||
| a0b975003a | |||
| 81250722ea | |||
| b7712d3d9d | |||
| c38fb6ac4c |
+73
-4
@@ -207,6 +207,75 @@ class Container implements ContainerInterface
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 清理所有非基础设施类的单例引用,释放 Swoole 常驻进程中的内存
|
||||
* 保留框架核心类(Kiri、Database、Psr、Symfony)的单例
|
||||
* 应在请求/任务完成后调用,防止单例映射无限增长
|
||||
* @return void
|
||||
*/
|
||||
public function clearNonInfrastructure(): void
|
||||
{
|
||||
$keepPrefixes = [
|
||||
ContainerInterface::class,
|
||||
'Kiri\\', 'Database\\', 'Psr\\', 'Symfony\\',
|
||||
'Swoole\\', 'MongoDB\\',
|
||||
];
|
||||
|
||||
foreach (array_keys($this->_singletons) as $className) {
|
||||
$shouldKeep = false;
|
||||
foreach ($keepPrefixes as $prefix) {
|
||||
if (str_starts_with($className, $prefix)) {
|
||||
$shouldKeep = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$shouldKeep) {
|
||||
unset($this->_singletons[$className]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array_keys($this->_reflection) as $className) {
|
||||
$shouldKeep = false;
|
||||
foreach ($keepPrefixes as $prefix) {
|
||||
if (str_starts_with($className, $prefix)) {
|
||||
$shouldKeep = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$shouldKeep) {
|
||||
unset($this->_reflection[$className]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array_keys($this->_parameters) as $className) {
|
||||
$shouldKeep = false;
|
||||
foreach ($keepPrefixes as $prefix) {
|
||||
if (str_starts_with($className, $prefix)) {
|
||||
$shouldKeep = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$shouldKeep) {
|
||||
unset($this->_parameters[$className]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前单例缓存统计信息,用于内存监控
|
||||
* @return array{singletons: int, reflections: int, params: int}
|
||||
*/
|
||||
public function getMemoryStats(): array
|
||||
{
|
||||
return [
|
||||
'singletons' => count($this->_singletons),
|
||||
'reflections' => count($this->_reflection),
|
||||
'params' => count($this->_parameters),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $className
|
||||
* @param array $construct
|
||||
@@ -226,10 +295,10 @@ class Container implements ContainerInterface
|
||||
}
|
||||
|
||||
$isController = class_exists(\Kiri\Router\Base\Controller::class) && $reflect->isSubclassOf(\Kiri\Router\Base\Controller::class);
|
||||
$needsProxy = !$isController && class_exists(\Kiri\Router\Annotate\DeferRegistry::class) && \Kiri\Router\Annotate\DeferRegistry::hasAny($className);
|
||||
$needsProxy = !$isController && class_exists(\Kiri\Router\Defer\DeferRegistry::class) && \Kiri\Router\Defer\DeferRegistry::hasAny($className);
|
||||
|
||||
if ($needsProxy) {
|
||||
$newInstance = \Kiri\Router\Annotate\DeferProxyGenerator::create($className, $construct);
|
||||
$newInstance = \Kiri\Router\Defer\DeferProxyGenerator::create($className, $construct);
|
||||
} else {
|
||||
$newInstance = $reflect->newInstanceArgs($construct);
|
||||
}
|
||||
@@ -260,10 +329,10 @@ class Container implements ContainerInterface
|
||||
$construct = $this->getMethodParams($handler);
|
||||
}
|
||||
$isController = class_exists(\Kiri\Router\Base\Controller::class) && $reflect->isSubclassOf(\Kiri\Router\Base\Controller::class);
|
||||
$needsProxy = !$isController && class_exists(\Kiri\Router\Annotate\DeferRegistry::class) && \Kiri\Router\Annotate\DeferRegistry::hasAny($reflect->getName());
|
||||
$needsProxy = !$isController && class_exists(\Kiri\Router\Defer\DeferRegistry::class) && \Kiri\Router\Defer\DeferRegistry::hasAny($reflect->getName());
|
||||
|
||||
if ($needsProxy) {
|
||||
$newInstance = \Kiri\Router\Annotate\DeferProxyGenerator::create($reflect->getName(), $construct);
|
||||
$newInstance = \Kiri\Router\Defer\DeferProxyGenerator::create($reflect->getName(), $construct);
|
||||
} else {
|
||||
$newInstance = $reflect->newInstanceArgs($construct);
|
||||
}
|
||||
|
||||
+7
-8
@@ -10,13 +10,12 @@ class HotReloadState
|
||||
|
||||
public function store(array $changedFiles): void
|
||||
{
|
||||
$normalizedFiles = array_map([$this, 'normalizePath'], array_filter($changedFiles));
|
||||
$normalizedFiles = array_values(array_unique($normalizedFiles));
|
||||
|
||||
$payload = [
|
||||
'timestamp' => time(),
|
||||
'changed_files' => $changedFiles
|
||||
|> array_filter(...)
|
||||
|> (fn($x) => array_map([$this, 'normalizePath'], $x))
|
||||
|> array_unique(...)
|
||||
|> array_values(...),
|
||||
'changed_files' => $normalizedFiles,
|
||||
];
|
||||
|
||||
$directory = dirname($this->getFilePath());
|
||||
@@ -50,9 +49,9 @@ class HotReloadState
|
||||
}
|
||||
|
||||
$files = is_array($data['changed_files'] ?? null) ? $data['changed_files'] : [];
|
||||
return array_map([$this, 'normalizePath'], $files)
|
||||
|> array_unique(...)
|
||||
|> array_values(...);
|
||||
$files = array_map([$this, 'normalizePath'], $files);
|
||||
$files = array_values(array_unique($files));
|
||||
return $files;
|
||||
}
|
||||
|
||||
private function getFilePath(): string
|
||||
|
||||
+3
-4
@@ -85,10 +85,9 @@ class ScanManifest
|
||||
return array_keys($this->entries);
|
||||
}
|
||||
|
||||
return $this->entries
|
||||
|> array_keys(...)
|
||||
|> (fn($x) => array_filter($x, fn(string $path) => str_starts_with($path, $prefix)))
|
||||
|> array_values(...);
|
||||
$keys = array_keys($this->entries);
|
||||
$filtered = array_filter($keys, fn(string $path) => str_starts_with($path, $prefix));
|
||||
return array_values($filtered);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+24
-15
@@ -79,11 +79,6 @@ class Scanner extends Component
|
||||
}
|
||||
|
||||
$this->syncLegacyState();
|
||||
|
||||
if ($this->config['cache_enabled']) {
|
||||
$this->saveToCache($cacheFile);
|
||||
}
|
||||
|
||||
return $this->changeSet;
|
||||
}
|
||||
|
||||
@@ -118,6 +113,7 @@ class Scanner extends Component
|
||||
}
|
||||
|
||||
$this->syncLegacyState();
|
||||
|
||||
return $this->changeSet;
|
||||
}
|
||||
|
||||
@@ -466,16 +462,26 @@ class Scanner extends Component
|
||||
return array_map(fn($attr) => $attr->getName(), $reflect->getAttributes());
|
||||
}
|
||||
|
||||
public function getStats(): array
|
||||
{
|
||||
return [
|
||||
'total_files' => count($this->files),
|
||||
'cached_mtimes' => count($this->fileMtimes),
|
||||
'base_path' => $this->basePath,
|
||||
'config' => $this->config,
|
||||
'manifest_entries' => count($this->manifest->all()),
|
||||
];
|
||||
}
|
||||
public function getStats(): array
|
||||
{
|
||||
return [
|
||||
'total_files' => count($this->files),
|
||||
'cached_mtimes' => count($this->fileMtimes),
|
||||
'base_path' => $this->basePath,
|
||||
'config' => $this->config,
|
||||
'manifest_entries' => count($this->manifest->all()),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回 Master 扫描产生的完整清单数据,供 Worker 轻量重建注解路由
|
||||
* @return array{string: array{mtime: int, classes: string[]}}
|
||||
*/
|
||||
public function getManifestClasses(): array
|
||||
{
|
||||
return $this->manifest->all();
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
@@ -589,6 +595,9 @@ class Scanner extends Component
|
||||
if (method_exists($this->container, 'forgetClass')) {
|
||||
$this->container->forgetClass($class);
|
||||
}
|
||||
if (class_exists(\Kiri\Router\Defer\DeferRegistry::class)) {
|
||||
\Kiri\Router\Defer\DeferRegistry::removeClass($class);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user