优化用户模型和模块租户检查逻辑,新增NATS消息配置和MongoDB缓存控制
This commit is contained in:
28
nats/nats_publish.go
Normal file
28
nats/nats_publish.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package nats
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// publish 发布消息到指定主题
|
||||
func publish(ctx context.Context, subject string, data any) (err error) {
|
||||
if !IsConnected() {
|
||||
return fmt.Errorf("NATS 未连接")
|
||||
}
|
||||
// 序列化数据
|
||||
dataBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("序列化数据失败: %w", err)
|
||||
}
|
||||
// 发布消息
|
||||
metrics.PublishCount.Add(1)
|
||||
_, err = js.Publish(ctx, subject, dataBytes)
|
||||
if err != nil {
|
||||
metrics.PublishError.Add(1)
|
||||
return fmt.Errorf("发布消息失败: %w", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user