5

Microsoft rate-limits certain Graph endpoints to 10,000 requests per 10 minutes (source). If the limit is reached, the Retry-After header indicates how long to wait before sending another request.

Is this handled automatically by the Graph SDK? If not, what steps should the caller take?

Eric Eskildsen
  • 4,269
  • 2
  • 38
  • 55

1 Answers1

4

I don't believe the Graph C# SDK automatically retries when requests are throttled, but there is a sample at https://github.com/venkateshchepuru/aspnet-webhooks-rest-sample/blob/87b1aa4967392096d22d382b7a8848bd9c0afeea/GraphWebhooks/Helpers/GraphHttpClient.cs that shows logic for exponential backoff for 429s and 503s.

This sample also follows a number of other best practices as well - max retries, logging request ids and timestamps, exponential backoff, etc.

Code for parsing the retry after header:

private TimeSpan GetServerRecommendedPause(HttpResponseMessage response)
    {
        var retryAfter = response?.Headers?.RetryAfter;
        if (retryAfter == null)
            return TimeSpan.Zero;

        return retryAfter.Date.HasValue
            ? retryAfter.Date.Value - DateTime.UtcNow
            : retryAfter.Delta.GetValueOrDefault(TimeSpan.Zero);
    }

Code for determining to use retry-after header or exponential backoff:

if (((int)response.StatusCode == 429) || ((int)response.StatusCode == 503))
            {
                // Retry Only After the server specified time period obtained from the response.
                TimeSpan pauseDuration = TimeSpan.FromSeconds(Math.Pow(2, attempt));
                TimeSpan serverRecommendedPauseDuration = GetServerRecommendedPause(response);
                if (serverRecommendedPauseDuration > pauseDuration)
                {
                    pauseDuration = serverRecommendedPauseDuration;
                }
Michael Mainer
  • 3,387
  • 1
  • 13
  • 32
David
  • 2,412
  • 1
  • 14
  • 22
  • 5
    This feature is in review right now. Please take at https://github.com/microsoftgraph/msgraph-sdk-dotnet/pull/301 – Michael Mainer Sep 21 '18 at 17:17
  • @MichaelMainer - just saw that the feature was merged. Do we need to make any changes to the code to implement this ? If yes, can you share some sample code. TIA ! – Gautam Sheth Nov 29 '18 at 17:10
  • @GautamSheth it is not hooked up yet. We still need to create the client factory to apply the handler pipeline. So you'll either need to wait some time more on this getting implemented or provide your own retry mechanism based on capturing ServiceException and checking the status code and then providing the backoff scheme. – Michael Mainer Nov 29 '18 at 18:57