Refactor Kafka publishing without DB outbox
This commit is contained in:
@@ -51,6 +51,7 @@ type groupMessageRuntime interface {
|
||||
MarkDone(tenantID types.TenantID, topic string, from types.Uid, clientMsgID string, seq int64, msgID string) error
|
||||
MarkFailed(tenantID types.TenantID, topic string, from types.Uid, clientMsgID string) error
|
||||
CacheHotMessage(tenantID types.TenantID, msg *types.Message) error
|
||||
CachePendingHotMessage(tenantID types.TenantID, msg *types.Message) error
|
||||
GetHotMessages(tenantID types.TenantID, topic string, opts *types.QueryOpt, maxLimit int) ([]types.Message, bool, error)
|
||||
InvalidateHotMessages(tenantID types.TenantID, topic string, ranges []types.Range) error
|
||||
MarkUserOnline(tenantID types.TenantID, uid types.Uid, sid, nodeID, device, userAgent string) error
|
||||
@@ -187,6 +188,27 @@ func DeleteCachedObject(tenantID types.TenantID, namespace, id string) error {
|
||||
return groupMsgRuntime.DeleteCachedObject(tenantID, namespace, id)
|
||||
}
|
||||
|
||||
func CacheHotMessage(tenantID types.TenantID, msg *types.Message) error {
|
||||
if groupMsgRuntime == nil || !tenantID.IsValid() || msg == nil || msg.Topic == "" || msg.SeqId <= 0 {
|
||||
return nil
|
||||
}
|
||||
return groupMsgRuntime.CacheHotMessage(tenantID, msg)
|
||||
}
|
||||
|
||||
func CachePendingHotMessage(tenantID types.TenantID, msg *types.Message) error {
|
||||
if groupMsgRuntime == nil || !tenantID.IsValid() || msg == nil || msg.Topic == "" || msg.SeqId <= 0 {
|
||||
return nil
|
||||
}
|
||||
return groupMsgRuntime.CachePendingHotMessage(tenantID, msg)
|
||||
}
|
||||
|
||||
func GetHotMessages(tenantID types.TenantID, topic string, opts *types.QueryOpt, maxLimit int) ([]types.Message, bool, error) {
|
||||
if groupMsgRuntime == nil || !tenantID.IsValid() || topic == "" {
|
||||
return nil, false, nil
|
||||
}
|
||||
return groupMsgRuntime.GetHotMessages(tenantID, topic, opts, maxLimit)
|
||||
}
|
||||
|
||||
func InvalidateHotMessages(tenantID types.TenantID, topic string, ranges []types.Range) error {
|
||||
if groupMsgRuntime == nil || !tenantID.IsValid() || topic == "" {
|
||||
return nil
|
||||
@@ -239,7 +261,7 @@ func initGroupMessageRuntime(raw json.RawMessage) error {
|
||||
cfg.KeyPrefix = "im"
|
||||
}
|
||||
if cfg.PendingTTL <= 0 {
|
||||
cfg.PendingTTL = 120
|
||||
cfg.PendingTTL = 300
|
||||
}
|
||||
if cfg.DoneTTL <= 0 {
|
||||
cfg.DoneTTL = 86400
|
||||
@@ -380,15 +402,25 @@ func (r *redisGroupMessageRuntime) MarkFailed(tenantID types.TenantID, topic str
|
||||
}
|
||||
|
||||
func (r *redisGroupMessageRuntime) CacheHotMessage(tenantID types.TenantID, msg *types.Message) error {
|
||||
return r.cacheHotMessage(tenantID, msg, time.Duration(r.cfg.HotMsgTTL)*time.Second)
|
||||
}
|
||||
|
||||
func (r *redisGroupMessageRuntime) CachePendingHotMessage(tenantID types.TenantID, msg *types.Message) error {
|
||||
return r.cacheHotMessage(tenantID, msg, time.Duration(r.cfg.PendingTTL)*time.Second)
|
||||
}
|
||||
|
||||
func (r *redisGroupMessageRuntime) cacheHotMessage(tenantID types.TenantID, msg *types.Message, ttl time.Duration) error {
|
||||
if msg == nil || msg.Topic == "" || msg.SeqId <= 0 {
|
||||
return types.ErrMalformed
|
||||
}
|
||||
if ttl <= 0 {
|
||||
return types.ErrMalformed
|
||||
}
|
||||
payload, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx := context.Background()
|
||||
ttl := time.Duration(r.cfg.HotMsgTTL) * time.Second
|
||||
seq := strconv.FormatInt(msg.SeqId, 10)
|
||||
pipe := r.client.Pipeline()
|
||||
pipe.Set(ctx, r.hotMsgKey(tenantID, msg.Topic, msg.SeqId), payload, ttl)
|
||||
|
||||
Reference in New Issue
Block a user