You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cloud Function v1向Pub/Sub发消息延迟高,求优化方案及批处理可行性

Cloud Function v1 向Pub/Sub发布消息的延迟优化与批处理疑问

我在Cloud Function v1中向Pub/Sub发布消息时,延迟通常在200-300ms左右。请问是否有人成功在Cloud Function v1中使用批处理功能?另外还有哪些优化延迟的建议?我对在Cloud Function中使用批处理存在一些顾虑。

以下是我的代码:

public String send(final Notification notification) {
    try {
        final TopicName topicName = TopicName.of(this.projectId, this.topicId);
        Publisher publisher = null;

        try {
            publisher = Publisher.newBuilder(topicName).build();

            final PubsubMessage pubsubMessage =
                PubsubMessage.newBuilder()
                 .setData(copyFromUtf8(notification.getContent()))
                 .putAllAttributes(notification.asMap()).setData(data)
                .build();

            final ApiFuture<String> future = publisher.publish(pubsubMessage);

            // Add an asynchronous callback to handle publish success / failure.
            ApiFutures.addCallback(
                future,
                new ApiFutureCallback<>() {
                  @Override
                  public void onFailure(final Throwable throwable) {}

                  @Override
                  public void onSuccess(final String messageId) {},

                MoreExecutors.directExecutor());

            return future.get();
        } finally {
            if (publisher != null) {
              publisher.shutdown();
              publisher.awaitTermination(1, TimeUnit.MINUTES);
            }
        }
    } catch (final Exception e) {
      throw new InternalServerErrorException(e.getMessage(), e);
    }
}

内容的提问来源于stack exchange,提问作者Jonathan Chevalier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 12:55:01