135 文件 19,211 处自动修复:空格→Tab 缩进、括号间距、 函数声明空格、前置自增、尾逗号等纯格式化改动。 零逻辑变更,php -l + token 级对比验证通过。 新增 phpcs.xml.dist / phpstan.neon.dist 项目配置。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
88 lines
2.1 KiB
PHP
88 lines
2.1 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 );
|
||
}
|
||
}
|