为什么以及如何在使用 batchCreateContacts 时超出配额 "critial read requests"

Why and how is the quota "critial read requests" exceeded when using batchCreateContacts

我正在使用 Google 人 API 编程将联系人从我们的数据库导出到 Google 联系人。我正在通过 Google Apps 脚本对 URL 上的请求进行编程。

下面的代码 - 使用 https://people.googleapis.com/v1/people:batchCreateContacts - 适用于 13 到大约 15 个单个请求,但随后 Google returns 此错误消息:

Quota exceeded for quota metric 'Critical read requests (Contact and Profile Reads)' and limit 'Critical read requests (Contact and Profile Reads) per minute per user' of service 'people.googleapis.com' for consumer 'project_number:***'.

为了提高速度,我以 10 个并行请求为一组发送请求。

关于这个问题我有以下两个问题:

  1. 为什么,对于创建 联系人,我会达到关于读取 请求的配额?
  2. 鉴于下面的图片 link,为什么发送 2 批 10 个同时请求(更准确地说:13 到 15 个单个请求)仍然会达到配额限制? quotum limit of 90 read requests per user per minute as displayed on console.cloud.google.com

感谢您的澄清!

进一步阅读:https://developers.google.com/people/api/rest/v1/people/batchCreateContacts

  let payloads = [];
  let lengthPayloads;
  let limitPayload = 200;
/*Break up contacts in payload limits*/
  contacts.forEach(function (contact, index) /*contacts is an array of objects for the API*/
    {
      if(!(index%limitPayload))
      {
        lengthPayloads = payloads.push(
          {
            'readMask': "userDefined",
            'sources': ["READ_SOURCE_TYPE_CONTACT"],
            'contacts': []
          }
        );
      }
      payloads[lengthPayloads-1]['contacts'].push(contact);
    }
  );
Logger.log("which makes "+payloads.length+" payloads");
  let parallelRequests = [];
  let lengthParallelRequests;
  let limitParallelRequest = 10;
/*Break up payloads in parallel request limits*/
  payloads.forEach(function (payload, index)
    {
      if(!(index%limitParallelRequest))
        lengthParallelRequests = parallelRequests.push([]);
      parallelRequests[lengthParallelRequests-1].push(
        {
          'url': "https://people.googleapis.com/v1/people:batchCreateContacts",
          'method': "post",
          'contentType': "application/json",
          'payload': JSON.stringify(payload),
          'headers': { 'Authorization': "Bearer " + token }, /*token is a token of a single user*/
          'muteHttpExceptions': true
        }
      );
    }
  );
Logger.log("which makes "+parallelRequests.length+" parallelrequests");
  let responses;
  parallelRequests.forEach(function (parallelRequest)
    {
      responses = UrlFetchApp.fetchAll(parallelRequest); /* error occurs here*/
      responses = responses.map(function (response) { return JSON.parse(response.getContentText()); });
      
      responses.forEach(function (response)
        {
          if(response.error)
          {
            Logger.log(JSON.stringify(response));
            throw response;
          }
          else Logger.log("ok");
        }
      );

日志输出:

which makes 22 payloads

which makes 3 parallelrequests

ok (15 times)

(the error message)

Quota exceeded for quota metric 'Critical read requests (Contact and Profile Reads)' and limit 'Critical read requests (Contact and Profile Reads) per minute per user' of service 'people.googleapis.com' for consumer 'project_number:***'.

有两种类型的配额:基于项目的配额和基于用户的配额。基于项目的配额是对您的项目本身的限制。基于用户的报价更像是洪水保护,它们限制单个用户在一段时间内可以发出的请求数量。

当您发送包含 10 个请求的批处理请求时,它会被视为 10 个请求,而不是单个批处理请求。如果您尝试 运行 这种并行,那么您肯定会超出每个用户配额每分钟的请求。

慢点这不是比赛[​​=13=]

Why, for creating contacts, I would hit a quota regarding read requests?

我会把它塞进错误消息中。

Given the picture link below, why would sending 13 to 15 requests hit that quota limit anyway? ((there are 3 read requests before this code)) quota limit of 90 read requests per user per minute as displayed on console.cloud.google.com

嗯,您每分钟发送 13 * 10 = 130,这将超过每分钟的请求。也没有办法知道你的系统有多快 运行 可能会更快,因为这取决于服务器在收到你的请求时正在做什么,实际上是在什么时候记录的。

我的建议是尊重配额限制,而不是试图理解为什么 Google 服务器上有很多变量才能确定一分钟到底是多少。您可以在 10 秒内发送 100 个请求,然后尝试在 55 秒内发送另外 100 个请求,您将收到错误消息,您也可能在 65 秒后收到错误消息,具体取决于它们何时到达服务器以及服务器何时完成处理您最初的 100 个请求.

再减速。

我在 Google 的 issue tracker 中提出了同样的问题。

似乎单个 BatchCreateContacts 或 BatchUpdateContacts 调用每个请求消耗六 (6) 个“关键读取请求”配额。仍然没有得到 creating/updating 联系人的答案,我们达到关键 read 请求的限制。