gpt4 book ai didi

google-cloud-platform - 谷歌 PubSub/Gmail 网络钩子(Hook) : Consistently Receiving Multiple POST Requests From PubSub When Sending An Email

转载 作者:行者123 更新时间:2023-12-05 05:13:31 25 4
gpt4 key购买 nike

我设置了一个网络钩子(Hook),它使用 Google Cloud PubSub API 和 Java 中的 Gmail API 监听和处理 Gmail 中收件箱和已发送文件夹中的更改。

我看到的问题是,当我向另一个用户发送消息时,PubSub 似乎在一秒钟内两次推送到我的端点,history_id 和 message_id 略有不同,但订阅相同姓名和用户电子邮件。

我知道 PubSub 保证至少一次交付,因此收到重复消息并不罕见,但因为它一直在发生并且 message_id 不同,我认为可能有基于以下 PubSub 文档的多个推送请求:

Cloud Pub/Sub assigns a unique message_id to each message, which can be used to detect duplicate messages received by the subscriber. This will not, however, allow you to detect duplicates resulting from multiple publish requests on the same data.

我尝试过的:

  1. 确保我的 Google Cloud 控制台上只有一个主题/订阅。
  2. 将 Ack 截止时间设置为 10 到 600 秒之间的不同值。
  3. 调用 service.users().stop() 以确保我没有多次调用 watch() 然后开始 watch() 再次。

我研究了 PubSubIO 以确保exactly-once-delivery,但我认为如果我一直收到多条 PubSub 消息,那么我的设置肯定存在根本性错误我的网络钩子(Hook)。

编辑:这是我必须注意我的 Gmail 帐户更改的代码。我正在使用具有全域权限的服务帐户来访问整个域中的帐户

public static Map<String, String> watchInbox(Gmail service) throws IOException {
Map<String, String> watchInboxResponse = new HashMap<>();
List<String> labelsToWatch = Arrays.asList("INBOX", "SENT");
String topicName = "projects/subscription-name/topics/topic-name";

WatchRequest request = new WatchRequest();
request.setLabelIds(labelsToWatch);
request.setTopicName(topicName);

WatchResponse response = service.users().watch("me", request).execute();

watchInboxResponse.put("historyId", response.getHistoryId().toString());
watchInboxResponse.put("expiration", response.getExpiration().toString());
return watchInboxResponse;
}

我将 historyid 和过期时间插入数据库并使用它来检查,在收到 webhook 调用时,如果距离上次已经超过 24 小时,我是否需要再次调用 watch()调用 watch(按照 Google 的建议)。

最佳答案

我在实现 Google 发布/订阅监视请求时有类似的行为。

当您撰写邮件时,Gmail 所做的是创建系统标签“发送和草稿”并不断使用新的 messageId 和标签“发送和草稿”保存到草稿,您订阅了对“收件箱和发送”的任何更改,因此您将在 webhook 上点击两次或更多时间!!

来自 Gmail 的邮件始终包含标签,您必须过滤那些带有标签草稿的邮件。

我的代码使用 Gmail .net SDK 并且必须处理

//Explicitly avoid further processing
bool isdraft = y.Message.LabelIds.Contains("DRAFT");

并过滤那些不管。

关于google-cloud-platform - 谷歌 PubSub/Gmail 网络钩子(Hook) : Consistently Receiving Multiple POST Requests From PubSub When Sending An Email,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53619495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com