ResilientUtils 代码复用

This commit is contained in:
龙腾猫跃 committed 2026-05-09 11:52:03 +08:00
1 parent b54a8f1683
commit 5c79bb8954
3 files changed
+14 -21

No files matched your search

+1
View File
@@ -25,6 +25,7 @@ public static class PathUtils {
/// </summary> /// </summary>
public static string ToShortPath(string fullName, bool keepFileName = false) { public static string ToShortPath(string fullName, bool keepFileName = false) {
if (string.IsNullOrEmpty(fullName)) return fullName; if (string.IsNullOrEmpty(fullName)) return fullName;
if (!fullName.Contains(":")) return fullName;
fullName = PathUtils.Normalize(fullName, false); fullName = PathUtils.Normalize(fullName, false);
if (fullName.Length <= 200) return fullName; if (fullName.Length <= 200) return fullName;
+9 -17
View File
@@ -9,20 +9,8 @@ public static class ResilientUtils {
/// <summary> /// <summary>
/// 在抛出特定异常时,延迟并自动重试。 /// 在抛出特定异常时,延迟并自动重试。
/// </summary> /// </summary>
public static void RetryOn<TException>(Action action, int maxAttempts = 2, int delayMs = 200, [CallerMemberName] string caller = "") where TException : Exception { public static void RetryOn<TException>(Action action, int maxAttempts = 2, int delayMs = 200, [CallerMemberName] string caller = "") where TException : Exception
int attempt = 0; => RetryOn<TException, object?>(() => { action(); return null; }, maxAttempts, delayMs, caller);
while (true) {
try {
action();
return; // 成功则退出
} catch (TException ex) {
attempt++;
if (attempt >= maxAttempts) throw; // 超过最大尝试次数
Logger.Warn(ex, $"{caller} 第 {attempt} 次尝试失败,将在 {delayMs}ms 后重试");
Thread.Sleep(delayMs);
}
}
}
/// <summary> /// <summary>
/// 在抛出异常时,延迟并自动重试。 /// 在抛出异常时,延迟并自动重试。
@@ -39,9 +27,13 @@ public static class ResilientUtils {
return func(); // 成功则退出 return func(); // 成功则退出
} catch (TException ex) { } catch (TException ex) {
attempt++; attempt++;
if (attempt >= maxAttempts) throw; // 超过最大尝试次数 if (attempt < maxAttempts) {
Logger.Warn(ex, $"{caller} 第 {attempt} 次尝试失败,将在 {delayMs}ms 后重试"); Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,将在 {delayMs}ms 后重试");
Thread.Sleep(delayMs); Thread.Sleep(delayMs);
} else { // 超过最大尝试次数
Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,已到达最大尝试次数");
throw;
}
} }
} }
} }
+4 -4
View File
@@ -6,9 +6,9 @@ public class PathUtilsTests {
[Test] [Test]
[Arguments(@"\\?\C:\foo\bar.txt", @"\\?\C:\foo")] [Arguments(@"\\?\C:\foo\bar.txt", @"\\?\C:\foo")]
[Arguments(@"C:\foo\bar.txt", @"C:\foo")] [Arguments(@"C:\foo\bar.txt", @"C:\foo")]
[Arguments(@"\\?\C:\foo\bar", @"\\?\C:\foo")] [Arguments(@"\\?\C:\foo\Bar", @"\\?\C:\foo")]
[Arguments(@"C:\foo\bar", @"C:\foo")] [Arguments(@"C:\foo\bar", @"C:\foo")]
[Arguments(@"C:\foo\bar\", @"C:\foo")] [Arguments(@"C:\Foo\bar\", @"C:\Foo")]
[Arguments(@"\\?\C:\foo\", @"\\?\C:")] [Arguments(@"\\?\C:\foo\", @"\\?\C:")]
[Arguments(@"C:\foo", @"C:")] [Arguments(@"C:\foo", @"C:")]
[Arguments(@"C:\foo\", @"C:")] [Arguments(@"C:\foo\", @"C:")]
@@ -31,7 +31,7 @@ public class PathUtilsTests {
[Arguments("https://foo.bar/file/long/pack.zip?arg=1", "pack.zip")] [Arguments("https://foo.bar/file/long/pack.zip?arg=1", "pack.zip")]
[Arguments("https://foo.bar/file/", "file")] [Arguments("https://foo.bar/file/", "file")]
[Arguments("https://foo.bar/file/?arg=1", "file")] [Arguments("https://foo.bar/file/?arg=1", "file")]
[Arguments("https://foo.bar/file?arg=1", "file")] [Arguments("https://foo.bar/File?arg=1", "File")]
public async Task 路径处理_GetLastPart(string input, string expected) public async Task 路径处理_GetLastPart(string input, string expected)
=> await Assert.That(PathUtils.GetLastPart(input)).IsEqualTo(expected); => await Assert.That(PathUtils.GetLastPart(input)).IsEqualTo(expected);
@@ -44,7 +44,7 @@ public class PathUtilsTests {
[Arguments(@"\\?\C:\foo\", "foo")] [Arguments(@"\\?\C:\foo\", "foo")]
[Arguments(@"C:\foo", "foo")] [Arguments(@"C:\foo", "foo")]
[Arguments(@"C:\foo\", "foo")] [Arguments(@"C:\foo\", "foo")]
[Arguments("create.jar.disabled", "create.jar")] [Arguments("cREate.jar.disabled", "cREate.jar")]
[Arguments("create.jar", "create")] [Arguments("create.jar", "create")]
[Arguments("create", "create")] [Arguments("create", "create")]
[Arguments("https://foo.bar/file/page.xaml.vb?arg=1", "page.xaml")] [Arguments("https://foo.bar/file/page.xaml.vb?arg=1", "page.xaml")]