private RetryParams ComputeAdalRetry(Exception ex) { if (ex is AdalServiceException) { AdalServiceException adalServiceException = (AdalServiceException)ex; // When the Service Token Server (STS) is too busy because of “too many requests”, // it returns an HTTP error 429 with a hint about when you can try again (Retry-After response field) as a delay in seconds if (adalServiceException.ErrorCode == AdalError.ServiceUnavailable || adalServiceException.StatusCode == 429) { RetryConditionHeaderValue retryAfter = adalServiceException.Headers.RetryAfter; // Depending on the service, the recommended retry time may be in retryAfter.Delta or retryAfter.Date. Check both. if (retryAfter != null && retryAfter.Delta.HasValue) { return(new RetryParams(retryAfter.Delta.Value)); } else if (retryAfter != null && retryAfter.Date.HasValue) { return(new RetryParams(retryAfter.Date.Value.Offset)); } // We got a 429 but didn't get a specific back-off time. Use the default return(RetryParams.DefaultBackOff(0)); } } return(RetryParams.DefaultBackOff(0)); }
private RetryParams HandleAdalException(Exception ex, int currentRetryCount) { if (IsAdalServiceUnavailable(ex)) { return(ComputeAdalRetry(ex)); } else if (ex is ThrottleException) { // This is an exception that we threw, with knowledge that // one of our threads is trying to acquire a token from the server // Use the retry parameters recommended in the exception ThrottleException throttlException = (ThrottleException)ex; return(throttlException.RetryParams ?? RetryParams.DefaultBackOff(currentRetryCount)); } else { // We end up here is the exception is not an ADAL exception. An example, is under high traffic // where we could have a timeout waiting to acquire a token, waiting on the semaphore. // If we hit a timeout, we want to retry a reasonable number of times. return(RetryParams.DefaultBackOff(currentRetryCount)); } }