No Description
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.

GoogleConnectionRetryPolicy.cs 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #nullable enable
  2. using System;
  3. using System.Threading.Tasks;
  4. using UnityEngine.Purchasing.Stores.Util;
  5. namespace UnityEngine.Purchasing
  6. {
  7. class GoogleConnectionRetryPolicy : IRetryPolicy
  8. {
  9. readonly int m_BaseRetryDelay;
  10. readonly int m_MaxRetryDelay;
  11. readonly int m_ExponentialFactor;
  12. public GoogleConnectionRetryPolicy(int baseRetryDelay = 2000, 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. int retryAttempts = 0;
  21. var currentRetryDelay = m_BaseRetryDelay;
  22. WaitAndRetry();
  23. async void WaitAndRetry()
  24. {
  25. await Task.Delay(currentRetryDelay);
  26. currentRetryDelay = AdjustDelay(currentRetryDelay);
  27. actionToTry(WaitAndRetry);
  28. onRetryAction?.Invoke();
  29. retryAttempts++;
  30. }
  31. }
  32. int AdjustDelay(int delay)
  33. {
  34. return Math.Min(m_MaxRetryDelay, delay * m_ExponentialFactor);
  35. }
  36. }
  37. }