Microsoft Graph SDK for .NET 是否自动处理速率限制?

Eri*_*sen 5 c# azure-active-directory microsoft-graph-sdks microsoft-graph-api

Microsoft 将某些 Graph 端点的速率限制为每 10 分钟 10,000 个请求(来源)。如果达到限制,Retry-After标头会指示在发送另一个请求之前要等待多长时间。

这是由 Graph SDK 自动处理的吗?如果没有,呼叫者应该采取什么步骤?

Ale*_*lex 4

我不相信 Graph C# SDK 在请求受到限制时会自动重试,但 https://github.com/venkateshchepuru/aspnet-webhooks-rest-sample/blob/87b1aa4967392096d22d382b7a8848bd9c0afeea/GraphWebhooks/Helpers/GraphHttpClient 上有一个示例。 cs显示了 429 和 503 的指数退避逻辑。

该示例还遵循许多其他最佳实践 - 最大重试次数、记录请求 ID 和时间戳、指数退避等。

解析头后重试的代码:

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);
    }
Run Code Online (Sandbox Code Playgroud)

用于确定使用重试后标头还是指数退避的代码:

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;
                }
Run Code Online (Sandbox Code Playgroud)

  • 此功能目前正在审核中。请访问 https://github.com/microsoftgraph/msgraph-sdk-dotnet/pull/301 (5认同)