Enhance failure count handling in RepositoryAffinity

Add lower bounds protection for failure counts to prevent negative values and integer underflow.
This commit is contained in:
hoshiizumiya
2025-11-23 21:24:10 +08:00
committed by GitHub
parent a7bb931ea5
commit 5f196253b3

View File

@@ -24,7 +24,10 @@ internal static class RepositoryAffinity
{ {
GitRepository repository = repositories[i]; GitRepository repository = repositories[i];
string key = GetSettingKey(repository.Name, repository.HttpsUrl.OriginalString); string key = GetSettingKey(repository.Name, repository.HttpsUrl.OriginalString);
counts[i] = LocalSetting.Get(key, 0);
// 对读取值做下限保护,确保排序使用的是非负失败计数
int raw = LocalSetting.Get(key, 0);
counts[i] = Math.Max(0, raw);
} }
Array.Sort(counts, ImmutableCollectionsMarshal.AsArray(repositories)); Array.Sort(counts, ImmutableCollectionsMarshal.AsArray(repositories));
@@ -43,7 +46,12 @@ internal static class RepositoryAffinity
{ {
string key = GetSettingKey(name, url); string key = GetSettingKey(name, url);
int currentCount = LocalSetting.Get(key, 0); int currentCount = LocalSetting.Get(key, 0);
LocalSetting.Set(key, unchecked(currentCount + 1));
// 防止整数上溢:当已到达 int.MaxValue 时不再自增
if (currentCount < int.MaxValue)
{
LocalSetting.Set(key, currentCount + 1);
}
} }
} }
@@ -58,7 +66,12 @@ internal static class RepositoryAffinity
{ {
string key = GetSettingKey(name, url); string key = GetSettingKey(name, url);
int currentCount = LocalSetting.Get(key, 0); int currentCount = LocalSetting.Get(key, 0);
LocalSetting.Set(key, unchecked(currentCount - 1));
// 失败次数不允许小于 0避免出现负数或整型下溢
if (currentCount > 0)
{
LocalSetting.Set(key, currentCount - 1);
}
} }
} }