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 string $className
|
||||||
* @param array $construct
|
* @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);
|
$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) {
|
if ($needsProxy) {
|
||||||
$newInstance = \Kiri\Router\Annotate\DeferProxyGenerator::create($className, $construct);
|
$newInstance = \Kiri\Router\Defer\DeferProxyGenerator::create($className, $construct);
|
||||||
} else {
|
} else {
|
||||||
$newInstance = $reflect->newInstanceArgs($construct);
|
$newInstance = $reflect->newInstanceArgs($construct);
|
||||||
}
|
}
|
||||||
@@ -260,10 +329,10 @@ class Container implements ContainerInterface
|
|||||||
$construct = $this->getMethodParams($handler);
|
$construct = $this->getMethodParams($handler);
|
||||||
}
|
}
|
||||||
$isController = class_exists(\Kiri\Router\Base\Controller::class) && $reflect->isSubclassOf(\Kiri\Router\Base\Controller::class);
|
$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) {
|
if ($needsProxy) {
|
||||||
$newInstance = \Kiri\Router\Annotate\DeferProxyGenerator::create($reflect->getName(), $construct);
|
$newInstance = \Kiri\Router\Defer\DeferProxyGenerator::create($reflect->getName(), $construct);
|
||||||
} else {
|
} else {
|
||||||
$newInstance = $reflect->newInstanceArgs($construct);
|
$newInstance = $reflect->newInstanceArgs($construct);
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-8
@@ -10,13 +10,12 @@ class HotReloadState
|
|||||||
|
|
||||||
public function store(array $changedFiles): void
|
public function store(array $changedFiles): void
|
||||||
{
|
{
|
||||||
|
$normalizedFiles = array_map([$this, 'normalizePath'], array_filter($changedFiles));
|
||||||
|
$normalizedFiles = array_values(array_unique($normalizedFiles));
|
||||||
|
|
||||||
$payload = [
|
$payload = [
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
'changed_files' => $changedFiles
|
'changed_files' => $normalizedFiles,
|
||||||
|> array_filter(...)
|
|
||||||
|> (fn($x) => array_map([$this, 'normalizePath'], $x))
|
|
||||||
|> array_unique(...)
|
|
||||||
|> array_values(...),
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$directory = dirname($this->getFilePath());
|
$directory = dirname($this->getFilePath());
|
||||||
@@ -50,9 +49,9 @@ class HotReloadState
|
|||||||
}
|
}
|
||||||
|
|
||||||
$files = is_array($data['changed_files'] ?? null) ? $data['changed_files'] : [];
|
$files = is_array($data['changed_files'] ?? null) ? $data['changed_files'] : [];
|
||||||
return array_map([$this, 'normalizePath'], $files)
|
$files = array_map([$this, 'normalizePath'], $files);
|
||||||
|> array_unique(...)
|
$files = array_values(array_unique($files));
|
||||||
|> array_values(...);
|
return $files;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getFilePath(): string
|
private function getFilePath(): string
|
||||||
|
|||||||
+3
-4
@@ -85,10 +85,9 @@ class ScanManifest
|
|||||||
return array_keys($this->entries);
|
return array_keys($this->entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->entries
|
$keys = array_keys($this->entries);
|
||||||
|> array_keys(...)
|
$filtered = array_filter($keys, fn(string $path) => str_starts_with($path, $prefix));
|
||||||
|> (fn($x) => array_filter($x, fn(string $path) => str_starts_with($path, $prefix)))
|
return array_values($filtered);
|
||||||
|> array_values(...);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+24
-15
@@ -79,11 +79,6 @@ class Scanner extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->syncLegacyState();
|
$this->syncLegacyState();
|
||||||
|
|
||||||
if ($this->config['cache_enabled']) {
|
|
||||||
$this->saveToCache($cacheFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->changeSet;
|
return $this->changeSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,6 +113,7 @@ class Scanner extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->syncLegacyState();
|
$this->syncLegacyState();
|
||||||
|
|
||||||
return $this->changeSet;
|
return $this->changeSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -466,16 +462,26 @@ class Scanner extends Component
|
|||||||
return array_map(fn($attr) => $attr->getName(), $reflect->getAttributes());
|
return array_map(fn($attr) => $attr->getName(), $reflect->getAttributes());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getStats(): array
|
public function getStats(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'total_files' => count($this->files),
|
'total_files' => count($this->files),
|
||||||
'cached_mtimes' => count($this->fileMtimes),
|
'cached_mtimes' => count($this->fileMtimes),
|
||||||
'base_path' => $this->basePath,
|
'base_path' => $this->basePath,
|
||||||
'config' => $this->config,
|
'config' => $this->config,
|
||||||
'manifest_entries' => count($this->manifest->all()),
|
'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
|
public function reset(): void
|
||||||
{
|
{
|
||||||
@@ -589,6 +595,9 @@ class Scanner extends Component
|
|||||||
if (method_exists($this->container, 'forgetClass')) {
|
if (method_exists($this->container, 'forgetClass')) {
|
||||||
$this->container->forgetClass($class);
|
$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