将项目自有代码的方法名从 camelCase 统一为 snake_case, 符合 WordPress PHP 编码规范。涉及 Routing、Failover、Budget、 Analytics、Usage、ErrorHandler 等模块。外部库接口方法和 Image Provider 子系统保持不变。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
90 lines
2.3 KiB
PHP
90 lines
2.3 KiB
PHP
<?php
|
||
/**
|
||
* Availability Strategy - 可用性优先路由策略
|
||
*
|
||
* 选择健康分数最高的 Provider
|
||
*
|
||
* @package WPMind
|
||
* @since 1.9.0
|
||
*/
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace WPMind\Routing\Strategies;
|
||
|
||
use WPMind\Routing\AbstractStrategy;
|
||
use WPMind\Routing\RoutingContext;
|
||
|
||
class AvailabilityStrategy extends AbstractStrategy
|
||
{
|
||
public function get_name(): string
|
||
{
|
||
return 'availability';
|
||
}
|
||
|
||
public function get_display_name(): string
|
||
{
|
||
return '可用性优先';
|
||
}
|
||
|
||
public function get_description(): string
|
||
{
|
||
return '选择健康分数最高的 Provider,适合对稳定性要求高的场景';
|
||
}
|
||
|
||
/**
|
||
* 计算 Provider 的得分
|
||
*
|
||
* 直接使用健康分数
|
||
*/
|
||
public function calculate_score(string $providerId, RoutingContext $context): float
|
||
{
|
||
$healthScore = $context->get_health_score($providerId);
|
||
$latency = $context->get_average_latency($providerId);
|
||
|
||
// 延迟作为次要因素(延迟越低加分越多)
|
||
$latencyBonus = 0;
|
||
if ($latency > 0 && $latency < 5000) {
|
||
$latencyBonus = (5000 - $latency) / 5000 * 10; // 最多加 10 分
|
||
}
|
||
|
||
return min(100, $healthScore + $latencyBonus);
|
||
}
|
||
|
||
/**
|
||
* 对 Provider 列表进行排序
|
||
*
|
||
* 按健康分数降序排列
|
||
*/
|
||
public function rank_providers(RoutingContext $context, array $providers): array
|
||
{
|
||
$available = $this->filter_available($context, $providers);
|
||
|
||
if (empty($available)) {
|
||
return [];
|
||
}
|
||
|
||
// 计算每个 Provider 的健康分数和延迟
|
||
$providerData = [];
|
||
foreach ($available as $providerId) {
|
||
$providerData[$providerId] = [
|
||
'health' => $context->get_health_score($providerId),
|
||
'latency' => $context->get_average_latency($providerId),
|
||
];
|
||
}
|
||
|
||
// 按健康分数降序排序
|
||
uasort($providerData, function ($a, $b) {
|
||
// 健康分数比较
|
||
$healthDiff = $b['health'] - $a['health'];
|
||
if ($healthDiff !== 0) {
|
||
return $healthDiff;
|
||
}
|
||
|
||
// 健康分数相同时,延迟低的优先
|
||
return $a['latency'] - $b['latency'];
|
||
});
|
||
|
||
return array_keys($providerData);
|
||
}
|
||
}
|