"Google Cloud Tasks",任务创建性能

"Google Cloud Tasks", performance of task creation

我在区域 europe-west1(比利时)中有 "Google cloud function",它在位于 europe-west3(德国)的 "Google cloud task queue" 中创建任务。它类似于教程:https://cloud.google.com/tasks/docs/tutorial-gcf

云函数创建任务大约需要1-2秒。这不是优化的解决方案,因为 CF 必须等待很长时间。

  1. 如何优化任务创建时间?

  2. 将Cloud Function和Task队列迁移到一个区域是否会显着提高任务创建速度?

  3. 我可以在发送 "task queue creation" 请求时停止云功能而不等待响应吗?当然,这是不好的做法,我想避免这种情况。

import { v2beta3 } from '@google-cloud/tasks';

// cloud function
(req, res) => {
  const client = new v2beta3.CloudTasksClient();
  ...
  // send "create task" request
  client.createTask(...);

  setTimeout(() => {
    // Stop cloud function without processing of queue response
    res.sendStatus(200);
  }, 100);
}
  1. API 会花多少时间就花多少时间。我认为没有任何方法可以加速该模块。

  2. 这听起来像是您可以尝试的东西。

  3. 不,云函数在其所有异步工作也完成之前不能终止,否则云函数将限制该函数中的资源,并且该工作可能无法完成。您应该使用承诺来确定异步工作何时完成,并且仅在之后发送响应。

Ad 1. 如果在createTask 中定义任务名称,这可能会提供优化的机会。由于额外的查找,提供任务名称会影响性能。如果您不提前分配任务名称,查找成本会更低。如果需要,请确保您使用的后续任务 ID 不是连续的。例如,在一长串按顺序命名的任务(如 taskname1、taskname2、taskname3 中查找比 sa7239112、92sdkdskfs、4812kdfsdf 慢。

Documentation:

Because there is an extra lookup cost to identify duplicate task names, these calls have significantly increased latency. Using hashed strings for the task id or for the prefix of the task id is recommended. Choosing task ids that are sequential or have sequential prefixes, for example using a timestamp, causes an increase in latency and error rates in all task commands. The infrastructure relies on an approximately uniform distribution of task ids to store and serve tasks efficiently.