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
相关产品推荐
相关产品推荐

