Retry.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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() ?? throw new NullReferenceException();
  51. }
  52. catch
  53. {
  54. continue;
  55. }
  56. using (enumerator)
  57. {
  58. var continueOuter = false;
  59. while (true)
  60. {
  61. var v = default(TSource);
  62. try
  63. {
  64. if (!enumerator.MoveNext())
  65. {
  66. yield break;
  67. }
  68. v = enumerator.Current;
  69. }
  70. catch
  71. {
  72. continueOuter = true;
  73. break;
  74. }
  75. yield return v;
  76. }
  77. if (continueOuter)
  78. {
  79. continue;
  80. }
  81. }
  82. }
  83. }
  84. private static IEnumerable<TSource> RetryFinite<TSource>(IEnumerable<TSource> source, int retryCount)
  85. {
  86. var lastException = default(Exception);
  87. for (var i = 0; i < retryCount; i++)
  88. {
  89. var enumerator = default(IEnumerator<TSource>);
  90. try
  91. {
  92. enumerator = source.GetEnumerator() ?? throw new NullReferenceException();
  93. }
  94. catch (Exception ex)
  95. {
  96. lastException = ex;
  97. continue;
  98. }
  99. using (enumerator)
  100. {
  101. var continueOuter = false;
  102. while (true)
  103. {
  104. var v = default(TSource);
  105. try
  106. {
  107. if (!enumerator.MoveNext())
  108. {
  109. yield break;
  110. }
  111. v = enumerator.Current;
  112. }
  113. catch (Exception ex)
  114. {
  115. lastException = ex;
  116. continueOuter = true;
  117. break;
  118. }
  119. yield return v;
  120. }
  121. if (continueOuter)
  122. {
  123. continue;
  124. }
  125. }
  126. }
  127. if (lastException != null)
  128. {
  129. throw lastException;
  130. }
  131. }
  132. }
  133. }