如何覆盖 Google Cloud Tasks Node.js 客户端的重试配置

2023-12-19

我一直在尝试探索是否有办法重试创建任务函数 https://github.com/googleapis/nodejs-tasks/blob/master/samples/createTask.js。原因是因为我时不时地遇到超出截止日期的错误,如下所示:

错误:4 DEADLINE_EXCEEDED:Object.onReceiveStatus (/srv/node_modules/grpc/src/client_interceptors.js:1204) 处的 Object.exports.createStatusError (/srv/node_modules/grpc/src/common.js:91:15) 超出截止日期:28) 在 InterceptingListener._callNext (/srv/node_modules/grpc/src/client_interceptors.js:568:42) 在 InterceptingListener.onReceiveStatus (/srv/node_modules/grpc/src/client_interceptors.js:618:8) 在回调 ( /srv/node_modules/grpc/src/client_interceptors.js:845:24)

阅读 createTask 函数背后的代码,我发现默认超时配置 https://github.com/googleapis/nodejs-tasks/blob/master/src/v2beta3/cloud_tasks_client_config.json#L88只有10秒。

目前,我尝试通过这样做将超时延长到 30 秒(我认为这是最大值):

try {
  console.log("Sending task %j", task);
  const callOptions = {
    timeout: 30000
  };
  // Send create task request.
  const [response] = await client.createTask(request, callOptions);
  const name = response.name;
  console.log(`Created task ${name}`);
} catch (error) {
  console.error("CREATE_TASK_ERROR::", error);
}

看起来它确实有效。不过,我还想介绍一下 API 在 30 秒内无法响应的情况。

我试过这段代码:

try {
  console.log("Sending task %j", task);
  const callOptions = {
    timeout: 2000, // I've set it to 2 seconds to be able to reproduce the deadline exceeded error easily
    retry: {
      initial_retry_delay_millis: 100,
      retry_delay_multiplier: 1.3,
      max_retry_delay_millis: 60000,
      initial_rpc_timeout_millis: 20000,
      rpc_timeout_multiplier: 1.0,
      max_rpc_timeout_millis: 20000,
      total_timeout_millis: 300000
    }
  };
  // Send create task request.
  const [response] = await client.createTask(request, callOptions);
  const name = response.name;
  console.log(`Created task ${name}`);
} catch (error) {
  console.error("CREATE_TASK_ERROR::", error);
}

但我没有看到 createTask 被重试。但根据评论here https://github.com/googleapis/nodejs-tasks/blob/master/src/v2beta3/cloud_tasks_client.js#L1432,我们应该能够覆盖默认设置,包括重试。

我究竟做错了什么?请帮忙。


看来 callOptions 是错误的。

  const callOptions = {
    timeout: 2000, // I've set it to 2 seconds to be able to reproduce the deadline exceeded error easily
    retry: {
      backoffSettings: {
        initialRetryDelayMillis: 100,
        retryDelayMultiplier: 1.3,
        maxRetryDelayMillis: 60000,
        initialRpcTimeoutMillis: 20000,
        // rpc_timeout_multiplier: 1.0,  not exists
        maxRpcTimeoutMillis: 20000,
        totalTimeoutMillis: 300000
      }
    }
  };

See:

  • https://googleapis.github.io/gax-nodejs/global.html#CallOptions https://googleapis.github.io/gax-nodejs/global.html#CallOptions
  • https://googleapis.github.io/gax-nodejs/global.html#RetryOptions https://googleapis.github.io/gax-nodejs/global.html#RetryOptions
  • https://googleapis.github.io/gax-nodejs/global.html#BackoffSettings https://googleapis.github.io/gax-nodejs/global.html#BackoffSettings

但我认为使用cli设置重试参数更好。

See:

  • https://cloud.google.com/tasks/docs/creating-appengine-queues#retry https://cloud.google.com/tasks/docs/creating-appengine-queues#retry
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何覆盖 Google Cloud Tasks Node.js 客户端的重试配置 的相关文章

随机推荐