【问题标题】:UnhandledPromiseRejectionWarning: TypeError: Channel credentials must be a ChannelCredentials object in GCP Batch publishingUnhandledPromiseRejectionWarning: TypeError: Channel credentials must be a ChannelCredentials object in GCP Batch publishing
【发布时间】:2021-11-30 01:04:48
【问题描述】:

我正在尝试使用节点模块 @google-cloud/pubsub 批量发布消息。我的批量发布代码如下所示。

const { PubSub } = require("@google-cloud/pubsub");
const grpc = require("grpc");

const createPublishEventsInBatch = (fastify, topic) => {
  const pubSub = new PubSub({ grpc });
  const batchPublisher = pubSub.topic(topic, {
    batching: {
      maxMessages: 100,
      maxMilliseconds: 1000
    }
  });

  return (logTrace, data, eventInfo, version) => {
    const { entityType, eventType } = eventInfo;
    fastify.log.debug({
      logTrace,
      eventType: eventType,
      data,
      message: `Publishing batch events for ${entityType}`
    });

    const event = createEvent(data, entityType, eventType, logTrace, version);
    batchPublisher.publish(Buffer.from(JSON.stringify(event)));

    fastify.log.debug({
      traceHeaders: logTrace,
      tenant: data.tenant,
      message: "Event publish completed",
      data
    });
  };
}; 

Pubsub 和 gRPC 版本如下。

"@google-cloud/pubsub": "^2.18.1",
"grpc": "^1.24.11"

当我使用上述代码发布消息时,出现以下错误。

(node:6) UnhandledPromiseRejectionWarning: TypeError: Channel credentials must be a ChannelCredentials object
    at new ChannelImplementation (/app/node_modules/@grpc/grpc-js/build/src/channel.js:75:19)
    at new Client (/app/node_modules/@grpc/grpc-js/build/src/client.js:61:36)
    at new ServiceClientImpl (/app/node_modules/@grpc/grpc-js/build/src/make-client.js:58:5)
    at GrpcClient.createStub (/app/node_modules/google-gax/build/src/grpc.js:334:22)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

我只在我的生产环境和暂存环境以及我所有的较低环境中看到这个问题,这工作正常。有人可以指导我解决这个问题。

【问题讨论】:

  • 您能否尝试GitHub issue 中提到的这种解决方法,即尝试在您的生产环境中重新安装模块。

标签: node.js grpc google-cloud-pubsub fastify


【解决方案1】:

与异常无关,但我想提一下,您通常希望执行一次然后缓存:

  const pubSub = new PubSub({ grpc });
  const batchPublisher = pubSub.topic(topic, {

这可以让您避免大量初始化开销,可能是一些内存泄漏(来自 proto 解析),并让您为所有请求保留一个发布队列(和批处理)。

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 2017-12-03
    相关资源
    最近更新 更多