共用方式為


在 Microsoft 365 API 上模擬節流

一般而言,當Microsoft 365 部伺服器負載過重時,測試 節流 很困難,因為它很少發生。 使用開發代理,您可以模擬節流回應,然後檢查您的應用程式是否正確處理。

若要模擬Microsoft 365 API 上的節流,請使用 GraphRandomErrorPluginRetryAfterPlugin。 會 GraphRandomErrorPlugin 傳回Microsoft 365 API 的節流回應。 RetryAfterPlugin 會依照 API 的指示,驗證您的應用程式是否降低活動頻率。

若要開始,請在您的 Dev Proxy 設定檔案中開啟 GraphRandomErrorPluginRetryAfterPlugin

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "RetryAfterPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
    },
    {
      "name": "GraphRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "graphRandomErrorPlugin"
    }
  ],
  "urlsToWatch": [
    "https://graph.microsoft.com/v1.0/*",
    "https://graph.microsoft.com/beta/*",
    "https://graph.microsoft.us/v1.0/*",
    "https://graph.microsoft.us/beta/*",
    "https://dod-graph.microsoft.us/v1.0/*",
    "https://dod-graph.microsoft.us/beta/*",
    "https://microsoftgraph.chinacloudapi.cn/v1.0/*",
    "https://microsoftgraph.chinacloudapi.cn/beta/*",
    "!https://*.sharepoint.*/*_api/web/GetClientSideComponents",
    "https://*.sharepoint.*/*_api/*",
    "https://*.sharepoint.*/*_vti_bin/*",
    "https://*.sharepoint-df.*/*_api/*",
    "https://*.sharepoint-df.*/*_vti_bin/*"
  ]
}

警告

在組態檔中,將RetryAfterPlugin新增到GraphRandomErrorPlugin之前。 如果您在之後新增它,則請求會被GraphRandomErrorPlugin攔截並失敗,因為RetryAfterPlugin還沒有機會處理。

接下來,配置 GraphRandomErrorPlugin 來模擬節流錯誤。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "RetryAfterPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
    },
    {
      "name": "GraphRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "graphRandomErrorPlugin"
    }
  ],
  "urlsToWatch": [
    "https://graph.microsoft.com/v1.0/*",
    "https://graph.microsoft.com/beta/*",
    "https://graph.microsoft.us/v1.0/*",
    "https://graph.microsoft.us/beta/*",
    "https://dod-graph.microsoft.us/v1.0/*",
    "https://dod-graph.microsoft.us/beta/*",
    "https://microsoftgraph.chinacloudapi.cn/v1.0/*",
    "https://microsoftgraph.chinacloudapi.cn/beta/*",
    "!https://*.sharepoint.*/*_api/web/GetClientSideComponents",
    "https://*.sharepoint.*/*_api/*",
    "https://*.sharepoint.*/*_vti_bin/*",
    "https://*.sharepoint-df.*/*_api/*",
    "https://*.sharepoint-df.*/*_vti_bin/*"
  ],
  "graphRandomErrorPlugin": {
    "allowedErrors": [ 429 ]
  }
}

使用您的組態檔啟動 Dev Proxy,並測試您的應用程式,以瞭解其如何處理節流。

如果您的應用程式在被節流時減速,但未達到請求中指定的等待時間,您會看到類似Calling https://graph.microsoft.com/v1.0/endpoint again before waiting for the Retry-After period. Request will be throttled的訊息。

此訊息表示您的應用程式未正確處理節流,而且不必要地延長節流。 若要改善應用程式處理節流的方式,請更新程式代碼以等候標頭中指定的 Retry-After 時間量,然後再重試要求。