Exception: 一天内调用次数过多:premium urlfetch

huangapple go评论79阅读模式
英文:

Exception: Service invoked too many times for one day: premium urlfetch

问题

以下是翻译好的部分:

"I have some scripts with functions running on a trigger. Some functions are scheduled to run once per day and few every 10 mins.
I am using a Google workspace account which has a quota of 100,000 calls/day for Urlfetch. I am quite sure that I haven't exceeded this quota. So, not sure why I keep getting this exception.

Exception: Service invoked too many times for one day: premium urlfetch.

Also, this exception comes for about an hour every day after which it resolves.

Please advise on the following :-

  1. Root cause and resolution
  2. Difference between urlfetch and premium urlfetch (as the exception says premium)"
英文:

I have some scripts with functions running on a trigger. Some functions are scheduled to run once per day and few every 10 mins.
I am using a Google workspace account which has a quota of 100,000 calls/day for Urlfetch. I am quite sure that I haven't exceeded this quota. So, not sure why I keep getting this exception.

Exception: Service invoked too many times for one day: premium urlfetch.

Also, this exception comes for about an hour every day after which it resolves.

Please advise on the following :-

  1. Root cause and resolution
  2. Difference between urlfetch and premium urlfetch (as the exception says premium)

答案1

得分: 2

超额配额错误通常表示您真的超出了配额。使用日志记录以确保代码中的逻辑是正确的,而且没有意外的重复调用UrlFetchApp().fetch()

要查找实际调用UrlFetchApp.fetch()的次数,使用console.log()。一个简单的方法是将所有的UrlFetchApp.fetch(...).getContentText()调用替换为调用一个辅助实用程序函数,如下所示:

/**
* 缓存并记录UrlFetchApp.fetch().getContentText()。
*
* @param {String} url 要获取的URL。
* @param {Object} params 用于获取的参数。
* @param {String} optContext 可选。要记录的标识符字符串。使用false来跳过记录。
* @return {String} HTTPResponse.getContentText()返回的文本。
*/
function cachedUrlFetchContentText_(url, params, optContext) {
  // 版本 1.1,由--Hyde编写,2023年3月21日
  //  - 请参考https://stackoverflow.com/a/75705228/13045193
  const cacheKey = JSON.stringify([url, params]);
  const cache = CacheService.getScriptCache();
  let cacheHit = false;
  let result;
  let resultJson = cache.get(cacheKey);
  if (resultJson) {
    result = JSON.parse(resultJson);
    cacheHit = true;
  } else {
    result = UrlFetchApp.fetch(url, params)
      .getContentText(); // 用.getContent()替换以获取原始结果
    resultJson = JSON.stringify(result);
    cache.put(cacheKey, resultJson, 21600);
  }
  if (optContext !== false) {
    console.log(`cachedUrlFetchContentText_ 上下文: ${optContext || '-'} URL: ${url} 缓存命中: ${cacheHit}`);
  }
  return result;
}

当您使用实用函数包装所有对UrlFetchApp.fetch()的调用时,也很容易包含缓存,如上所示。在许多用例中,缓存可以帮助您避免首次超出配额限制。

要查看日志,请访问我的执行仪表板或日志资源管理器

请参阅consoleCloud LoggingCache Service

英文:

Quota exceeded errors usually tell that you are really exceeding the quota. Use logging to ensure that the logic in your code is correct and that there are no unintended repeated calls to UrlFetchApp().fetch().

To find how many times you are actually calling UrlFetchApp.fetch(), use console.log(). One easy way to do that is to replace all UrlFetchApp.fetch(...).getContentText() calls with a call to a helper utility function such as this one:

/**
* Caches and logs UrlFetchApp.fetch().getContentText().
*
* @param {String} url The URL to fetch.
* @param {Object} params The parameters to use in fetch.
* @param {String} optContext Optional. An identifier string to log. Use false to skip logging.
* @return {String} The text returned by HTTPResponse.getContentText().
*/
function cachedUrlFetchContentText_(url, params, optContext) {
  // version 1.1, written by --Hyde, 21 March 2023
  //  - see https://stackoverflow.com/a/75705228/13045193
  const cacheKey = JSON.stringify([url, params]);
  const cache = CacheService.getScriptCache();
  let cacheHit = false;
  let result;
  let resultJson = cache.get(cacheKey);
  if (resultJson) {
    result = JSON.parse(resultJson);
    cacheHit = true;
  } else {
    result = UrlFetchApp.fetch(url, params)
      .getContentText(); // replace with .getContent() to get raw result
    resultJson = JSON.stringify(result);
    cache.put(cacheKey, resultJson, 21600);
  }
  if (optContext !== false) {
    console.log(`cachedUrlFetchContentText_ context: ${optContext || '-'} url: ${url} cacheHit: ${cacheHit}`);
  }
  return result;
}

When you use a utility function to wrap all calls to UrlFetchApp.fetch(), it is also easy to incorporate caching, as shown above. In many use cases, caching can help you avoid hitting quota limits in the first place.

To view the logs, visit the My Executions dashboard or the Logs Explorer.

See console, Cloud Logging and Cache Service.

答案2

得分: 0

App Script配额和限制

我想澄清一下“urlfetch”和“premium urlfetch”之间的区别。根据App Script中的Google服务配额建议,它们已分为两个类别,一个用于普通用户或Gmail帐户,另一个用于Google Workspace帐户。您端的高级错误是由您必须管理App Script项目的版本引起的(Gmail帐户的“urlfetch”限制仅为20,000次)。

找出根本原因可能导致不同的情况。大多数情况下,这可能是由于多种原因,主要原因可能是由于服务被调用太多次,例如多个具有不同项目或脚本的Sheet API调用,导致错误影响到您的用户帐户,或者新数据的不断增加被提取到您的Sheets或脚本中。需要考虑的一个关键因素是错误与每个用户的配额有关,您可以创建一个新用户,并将脚本以新用户作为新项目或副本的所有者运行。

有一个类似的情景在这个主题中提出,涉及到函数和数据呈指数增长导致出现错误以及潜在解决方案,我强烈建议您查看它:

参考资料:

英文:

App Script quotas and limitations

I would like to clarify what is the difference between both "urlfetch" and "premium urlfetch". As suggested by the Quotas for Google Services from App Script, there have been separated under two categories, one for consumer or gmail account and another one for the Google Workspace accounts, the premium error on your end is due to the edition you have to manage the App Script projects (limit on Gmail accounts are onlye 20,000 "urlfetch")

Identifying the root cause might take you to different scenarios. Most of the time could be due to multiple reasons, mainly it could be due to the service being called too many times, in this case multiple Sheet API calls with different projects or scripts, which leads to the error affecting your user account, or the constant increase of new data that is being fetch over your Sheets or scripts. One key thing to take in consideration is the error is linked to the quotas per user, you could create a new user and run the scripts with the new user as the owner of the new project or copy.

There is a similar scenario presented over this thread, about functions and how the exponential grow of data leads to the error being presented and a potential solution, I would highly suggest to review it:

References:

huangapple
  • 本文由 发表于 2023年2月19日 16:30:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定