Google PubSub / Gmail Webhook:发送电子邮件时持续收到来自 PubSub 的多个 POST 请求
Google PubSub / Gmail Webhook: Consistently Receiving Multiple POST Requests From PubSub When Sending An Email
我设置了一个 webhook,它使用 Google Cloud PubSub API 和 Java.
我看到的问题是,当我向另一个用户发送消息时,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.
我尝试过的:
- 确保我的 Google 云控制台上只有一个 Topic/Subscription。
- 将 Ack 截止时间设置为 10 到 600 秒之间的不同值。
- 已调用
service.users().stop()
以确保我没有多次调用 watch()
然后再次开始 watch()
。
我已经研究过 PubSubIO 以确保精确一次交付,但我想如果我一直收到多条 PubSub 消息,那么一定有一些根本性的错误我已经设置了我的网络钩子。
编辑:
这是我必须注意我的 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()
18=](由 Google 推荐)。
我在执行 Google pub/sub 监视请求时有类似的行为。
Gmail 在您撰写邮件时所做的是创建系统标签
"Send and Draft"
并不断使用新的 messageId 和标签保存到草稿 "Send and draft" 您已订阅对 "Inbox and Send" 的任何更改,因此您将被 webhook 命中两次或更多时间!!
来自 Gmail 的邮件始终包含标签,您必须过滤那些带有标签草稿的邮件。
我的代码使用 Gmail .net SDK 并且必须处理
//Explicitly avoid further processing
bool isdraft = y.Message.LabelIds.Contains("DRAFT");
并过滤那些不管。
我设置了一个 webhook,它使用 Google Cloud PubSub API 和 Java.
我看到的问题是,当我向另一个用户发送消息时,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.
我尝试过的:
- 确保我的 Google 云控制台上只有一个 Topic/Subscription。
- 将 Ack 截止时间设置为 10 到 600 秒之间的不同值。
- 已调用
service.users().stop()
以确保我没有多次调用watch()
然后再次开始watch()
。
我已经研究过 PubSubIO 以确保精确一次交付,但我想如果我一直收到多条 PubSub 消息,那么一定有一些根本性的错误我已经设置了我的网络钩子。
编辑: 这是我必须注意我的 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()
18=](由 Google 推荐)。
我在执行 Google pub/sub 监视请求时有类似的行为。
Gmail 在您撰写邮件时所做的是创建系统标签 "Send and Draft" 并不断使用新的 messageId 和标签保存到草稿 "Send and draft" 您已订阅对 "Inbox and Send" 的任何更改,因此您将被 webhook 命中两次或更多时间!!
来自 Gmail 的邮件始终包含标签,您必须过滤那些带有标签草稿的邮件。
我的代码使用 Gmail .net SDK 并且必须处理
//Explicitly avoid further processing
bool isdraft = y.Message.LabelIds.Contains("DRAFT");
并过滤那些不管。