설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExponentialRetryPolicy.cs 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #nullable enable
  2. using System;
  3. using System.Threading.Tasks;
  4. using UnityEngine.Purchasing.Stores.Util;
  5. namespace UnityEngine.Purchasing
  6. {
  7. class ExponentialRetryPolicy : IRetryPolicy
  8. {
  9. readonly int m_BaseRetryDelay;
  10. readonly int m_MaxRetryDelay;
  11. readonly int m_ExponentialFactor;
  12. public ExponentialRetryPolicy(int baseRetryDelay = 1000, int maxRetryDelay = 30 * 1000, int exponentialFactor = 2)
  13. {
  14. m_BaseRetryDelay = baseRetryDelay;
  15. m_MaxRetryDelay = maxRetryDelay;
  16. m_ExponentialFactor = exponentialFactor;
  17. }
  18. public void Invoke(Action<Action> actionToTry, Action? onRetryAction)
  19. {
  20. var currentRetryDelay = m_BaseRetryDelay;
  21. actionToTry(Retry);
  22. async void Retry()
  23. {
  24. onRetryAction?.Invoke();
  25. await WaitAndRetry();
  26. }
  27. async Task WaitAndRetry()
  28. {
  29. await Task.Delay(currentRetryDelay);
  30. currentRetryDelay = AdjustDelay(currentRetryDelay);
  31. actionToTry(Retry);
  32. }
  33. }
  34. int AdjustDelay(int delay)
  35. {
  36. return Math.Min(m_MaxRetryDelay, delay * m_ExponentialFactor);
  37. }
  38. }
  39. }