Skip to content

Commit 9375ff6

Browse files
committed
fix: Corrigir tratamento de caminhos e otimizar a lógica de resposta no CircuitBreaker e LoadShedder
refactor: Ajustar inicialização de localPool e simplificar acesso a estatísticas no DistributedPoolManager
1 parent 6facc6d commit 9375ff6

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

src/Middleware/CircuitBreaker.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,17 @@ private function getCircuitName(Request $request): string
117117
$path = $request->path ?? $request->getPathCallable();
118118

119119
// Normalize path to circuit name
120-
$parts = explode('/', trim($path, '/'));
120+
$path = trim($path, '/');
121+
if ($path === '') {
122+
return 'default';
123+
}
124+
125+
$parts = explode('/', $path);
121126

122127
// Group by first two segments (e.g., /api/users/* becomes api_users)
123128
$circuitParts = array_slice($parts, 0, 2);
124129

125-
return implode('_', $circuitParts) ?: 'default';
130+
return implode('_', $circuitParts);
126131
}
127132

128133
/**

src/Middleware/LoadShedder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,14 @@ private function shedRequest(Response $response, string $priorityClass): Respons
355355

356356
$config = $this->config['shed_response'];
357357

358-
return $response
358+
$response = $response
359359
->status($config['status'])
360360
->json($config['body'])
361361
->header('X-Load-Shed', 'true')
362362
->header('X-Load-Shed-Reason', $this->config['shed_strategy']);
363363

364364
foreach ($config['headers'] as $name => $value) {
365-
$response->header($name, (string) $value);
365+
$response = $response->header($name, (string) $value);
366366
}
367367

368368
return $response;

src/Performance/HighPerformanceMode.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,10 @@ function ($request, $response, $next) {
374374
}
375375

376376
// Schedule periodic tasks
377-
if (self::$monitor) {
378-
self::schedulePeriodicTask(
379-
self::$currentConfig['monitoring']['export_interval'],
380-
[self::$monitor, 'export']
381-
);
382-
}
377+
self::schedulePeriodicTask(
378+
self::$currentConfig['monitoring']['export_interval'],
379+
[self::$monitor, 'export']
380+
);
383381
}
384382

385383
/**

src/Pool/Distributed/DistributedPoolManager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class DistributedPoolManager
4545
/**
4646
* Local pool
4747
*/
48-
private DynamicPool $localPool;
48+
private ?DynamicPool $localPool = null;
4949

5050
/**
5151
* State
@@ -172,7 +172,7 @@ private function getInstanceCapabilities(): array
172172
return [
173173
'memory_limit' => ini_get('memory_limit'),
174174
'cpu_cores' => $this->getCPUCores(),
175-
'pool_config' => isset($this->localPool) ? $this->localPool->getStats()['config'] : [],
175+
'pool_config' => $this->localPool?->getStats()['config'] ?? [],
176176
];
177177
}
178178

@@ -483,7 +483,7 @@ private function updateInstanceInfo(): void
483483
'id' => $this->instanceId,
484484
'last_seen' => time(),
485485
'metrics' => $this->metrics,
486-
'pool_stats' => $this->localPool ? $this->localPool->getStats() : [],
486+
'pool_stats' => $this->localPool?->getStats() ?? [],
487487
'health' => $this->getHealthStatus(),
488488
];
489489

@@ -518,7 +518,7 @@ private function participateInLeaderElection(): void
518518
private function getHealthStatus(): array
519519
{
520520
$memoryUsage = memory_get_usage(true) / $this->parseMemoryLimit(ini_get('memory_limit'));
521-
$poolStats = $this->localPool ? $this->localPool->getStats() : [];
521+
$poolStats = $this->localPool?->getStats() ?? [];
522522

523523
$score = 1.0;
524524

0 commit comments

Comments
 (0)