Enhance NATS configuration and error handling. Introduce stream limits and consumer rules in configuration files. Refactor message processing to handle permanent errors. Update README and development configuration to reflect changes. Add tests for new error handling mechanisms.

This commit is contained in:
windyboy
2025-11-14 21:42:04 +08:00
parent a574cfcf27
commit 9cce4610b6
13 changed files with 510 additions and 57 deletions
+19 -2
View File
@@ -2,11 +2,13 @@ package nats
import (
"caatsm/internal/adapter"
"caatsm/internal/domain"
"caatsm/internal/infra/config"
"encoding/json"
"fmt"
"go.uber.org/zap"
"github.com/google/uuid"
"github.com/nats-io/nats.go"
"go.uber.org/zap"
)
// Publisher publishes messages to NATS JetStream
@@ -42,8 +44,23 @@ func (p *Publisher) Publish(message interface{}) error {
return fmt.Errorf("failed to marshal message: %w", err)
}
// Build JetStream message to attach dedup headers
jsMsg := nats.NewMsg(topic)
jsMsg.Data = messageBytes
switch typed := message.(type) {
case *domain.ParsedMessage:
if typed != nil && typed.Uuid != "" {
jsMsg.Header.Set("Nats-Msg-Id", typed.Uuid)
} else {
jsMsg.Header.Set("Nats-Msg-Id", uuid.NewString())
}
default:
jsMsg.Header.Set("Nats-Msg-Id", uuid.NewString())
}
// Publish to JetStream
_, err = p.js.Publish(topic, messageBytes)
_, err = p.js.PublishMsg(jsMsg)
if err != nil {
return fmt.Errorf("failed to publish message: %w", err)
}