Fix Kafka message seq idempotency

This commit is contained in:
richjohnrambo
2026-09-03 13:21:26 +08:00
parent 256f768d15
commit a0bd0b0649
3 changed files with 247 additions and 1 deletions
+38
View File
@@ -39,6 +39,11 @@ type groupMessageUniqueRecord struct {
CreatedAt time.Time `json:"created_at"`
}
type MessageUniqueClaim struct {
Status string
Seq int64
}
type groupFanoutEnvelope struct {
OriginNodeID string `json:"origin_node_id"`
Message json.RawMessage `json:"message"`
@@ -76,6 +81,39 @@ type groupMessageRuntime interface {
var groupMsgRuntime groupMessageRuntime
func ClaimMessageUnique(tenantID types.TenantID, topic string, from types.Uid, clientMsgID string) (*MessageUniqueClaim, bool, bool, error) {
if groupMsgRuntime == nil || !tenantID.IsValid() || topic == "" || from.IsZero() || clientMsgID == "" {
return nil, true, false, nil
}
rec, claimed, err := groupMsgRuntime.Claim(tenantID, topic, from, clientMsgID)
if rec == nil {
return nil, claimed, true, err
}
return &MessageUniqueClaim{Status: rec.Status, Seq: rec.Seq}, claimed, true, err
}
func ReserveMessageSeq(tenantID types.TenantID, topic string, baseline int64) (int64, bool, error) {
if groupMsgRuntime == nil || !tenantID.IsValid() || topic == "" {
return 0, false, nil
}
seq, err := groupMsgRuntime.NextSeq(tenantID, topic, baseline)
return seq, true, err
}
func MarkMessageUniqueDone(tenantID types.TenantID, topic string, from types.Uid, clientMsgID string, seq int64, msgID string) error {
if groupMsgRuntime == nil || !tenantID.IsValid() || topic == "" || from.IsZero() || clientMsgID == "" {
return nil
}
return groupMsgRuntime.MarkDone(tenantID, topic, from, clientMsgID, seq, msgID)
}
func MarkMessageUniqueFailed(tenantID types.TenantID, topic string, from types.Uid, clientMsgID string) error {
if groupMsgRuntime == nil || !tenantID.IsValid() || topic == "" || from.IsZero() || clientMsgID == "" {
return nil
}
return groupMsgRuntime.MarkFailed(tenantID, topic, from, clientMsgID)
}
type UserPresence struct {
Online bool `json:"online"`
NodeID string `json:"node_id"`