Retry.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Creates a sequence that retries enumerating the source sequence as long as an error occurs.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <param name="source">Source sequence.</param>
  14. /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
  15. public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source)
  16. {
  17. if (source == null)
  18. {
  19. throw new ArgumentNullException(nameof(source));
  20. }
  21. return RetryInfinite(source);
  22. }
  23. /// <summary>
  24. /// Creates a sequence that retries enumerating the source sequence as long as an error occurs, with the specified
  25. /// maximum number of retries.
  26. /// </summary>
  27. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  28. /// <param name="source">Source sequence.</param>
  29. /// <param name="retryCount">Maximum number of retries.</param>
  30. /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
  31. public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source, int retryCount)
  32. {
  33. if (source == null)
  34. {
  35. throw new ArgumentNullException(nameof(source));
  36. }
  37. if (retryCount < 0)
  38. {
  39. throw new ArgumentOutOfRangeException(nameof(retryCount));
  40. }
  41. return RetryFinite(source, retryCount);
  42. }
  43. private static IEnumerable<TSource> RetryInfinite<TSource>(IEnumerable<TSource> source)
  44. {
  45. while (true)
  46. {
  47. var enumerator = default(IEnumerator<TSource>);
  48. try
  49. {
  50. enumerator = source.GetEnumerator();
  51. }
  52. catch
  53. {
  54. continue;
  55. }
  56. using (enumerator)
  57. {
  58. while (true)
  59. {
  60. var v = default(TSource);
  61. try
  62. {
  63. if (!enumerator.MoveNext())
  64. {
  65. yield break;
  66. }
  67. v = enumerator.Current;
  68. }
  69. catch
  70. {
  71. break;
  72. }
  73. yield return v;
  74. }
  75. }
  76. }
  77. }
  78. private static IEnumerable<TSource> RetryFinite<TSource>(IEnumerable<TSource> source, int retryCount)
  79. {
  80. var lastException = default(Exception);
  81. for (var i = 0; i < retryCount; i++)
  82. {
  83. var enumerator = default(IEnumerator<TSource>);
  84. try
  85. {
  86. enumerator = source.GetEnumerator();
  87. }
  88. catch (Exception ex)
  89. {
  90. lastException = ex;
  91. continue;
  92. }
  93. using (enumerator)
  94. {
  95. while (true)
  96. {
  97. var v = default(TSource);
  98. try
  99. {
  100. if (!enumerator.MoveNext())
  101. {
  102. yield break;
  103. }
  104. v = enumerator.Current;
  105. }
  106. catch (Exception ex)
  107. {
  108. lastException = ex;
  109. break;
  110. }
  111. yield return v;
  112. }
  113. }
  114. }
  115. if (lastException != null)
  116. {
  117. throw lastException;
  118. }
  119. }
  120. }
  121. }