mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-09 03:25:59 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
This commit is contained in:
@@ -277,6 +277,7 @@ const OpUserPlatform = "platform"
|
||||
const Token = "token"
|
||||
const RpcCustomHeader = "customHeader" // rpc中间件自定义ctx参数
|
||||
const CheckKey = "CheckKey"
|
||||
const TriggerID = "triggerID"
|
||||
|
||||
const (
|
||||
UnreliableNotification = 1
|
||||
|
||||
@@ -77,7 +77,7 @@ type MsgDatabase interface {
|
||||
|
||||
MsgToMQ(ctx context.Context, key string, msg2mq *pbMsg.MsgDataToMQ) error
|
||||
MsgToModifyMQ(ctx context.Context, aggregationID string, triggerID string, messages []*pbMsg.MsgDataToMQ) error
|
||||
MsgToPushMQ(ctx context.Context, sourceID string, msg2mq *pbMsg.MsgDataToMQ) error
|
||||
MsgToPushMQ(ctx context.Context, sourceID string, msg2mq *pbMsg.MsgDataToMQ) (int32, int64, error)
|
||||
MsgToMongoMQ(ctx context.Context, aggregationID string, triggerID string, messages []*pbMsg.MsgDataToMQ, lastSeq int64) error
|
||||
}
|
||||
|
||||
@@ -193,10 +193,9 @@ func (db *msgDatabase) MsgToModifyMQ(ctx context.Context, aggregationID string,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *msgDatabase) MsgToPushMQ(ctx context.Context, key string, msg2mq *pbMsg.MsgDataToMQ) error {
|
||||
func (db *msgDatabase) MsgToPushMQ(ctx context.Context, key string, msg2mq *pbMsg.MsgDataToMQ) (int32, int64, error) {
|
||||
mqPushMsg := pbMsg.PushMsgDataToMQ{MsgData: msg2mq.MsgData, SourceID: key}
|
||||
_, _, err := db.producerToPush.SendMessage(ctx, key, &mqPushMsg)
|
||||
return err
|
||||
return db.producerToPush.SendMessage(ctx, key, &mqPushMsg)
|
||||
}
|
||||
|
||||
func (db *msgDatabase) MsgToMongoMQ(ctx context.Context, aggregationID string, triggerID string, messages []*pbMsg.MsgDataToMQ, lastSeq int64) error {
|
||||
|
||||
@@ -8,8 +8,6 @@ package kafka
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||
"github.com/Shopify/sarama"
|
||||
)
|
||||
|
||||
@@ -42,16 +40,9 @@ func NewMConsumerGroup(consumerConfig *MConsumerGroupConfig, topics, addrs []str
|
||||
}
|
||||
}
|
||||
|
||||
func (mc *MConsumerGroup) GetContextFromMsg(cMsg *sarama.ConsumerMessage, rootFuncName string) context.Context {
|
||||
ctx := mcontext.NewCtx(rootFuncName)
|
||||
var operationID string
|
||||
for _, v := range cMsg.Headers {
|
||||
if string(v.Key) == constant.OperationID {
|
||||
operationID = string(v.Value)
|
||||
}
|
||||
}
|
||||
mcontext.SetOperationID(ctx, operationID)
|
||||
return ctx
|
||||
func (mc *MConsumerGroup) GetContextFromMsg(cMsg *sarama.ConsumerMessage) context.Context {
|
||||
return GetContextWithMQHeader(cMsg.Headers)
|
||||
|
||||
}
|
||||
|
||||
func (mc *MConsumerGroup) RegisterHandleAndConsumer(handler sarama.ConsumerGroupHandler) {
|
||||
|
||||
@@ -47,7 +47,24 @@ func NewKafkaProducer(addr []string, topic string) *Producer {
|
||||
p.producer = producer
|
||||
return &p
|
||||
}
|
||||
|
||||
func GetMQHeaderWithContext(ctx context.Context) ([]sarama.RecordHeader, error) {
|
||||
operationID, opUserID, platform, connID, err := mcontext.GetMustCtxInfo(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []sarama.RecordHeader{
|
||||
{Key: []byte(constant.OperationID), Value: []byte(operationID)},
|
||||
{Key: []byte(constant.OpUserID), Value: []byte(opUserID)},
|
||||
{Key: []byte(constant.OpUserPlatform), Value: []byte(platform)},
|
||||
{Key: []byte(constant.ConnID), Value: []byte(connID)}}, err
|
||||
}
|
||||
func GetContextWithMQHeader(header []*sarama.RecordHeader) context.Context {
|
||||
var values []string
|
||||
for _, recordHeader := range header {
|
||||
values = append(values, string(recordHeader.Value))
|
||||
}
|
||||
return mcontext.WithMustInfoCtx(values)
|
||||
}
|
||||
func (p *Producer) SendMessage(ctx context.Context, key string, m proto.Message) (int32, int64, error) {
|
||||
log.ZDebug(ctx, "SendMessage", "key ", key, "msg", m.String())
|
||||
kMsg := &sarama.ProducerMessage{}
|
||||
@@ -65,15 +82,11 @@ func (p *Producer) SendMessage(ctx context.Context, key string, m proto.Message)
|
||||
return 0, 0, utils.Wrap(emptyMsg, "")
|
||||
}
|
||||
kMsg.Metadata = ctx
|
||||
operationID, opUserID, platform, connID, err := mcontext.GetMustCtxInfo(ctx)
|
||||
header, err := GetMQHeaderWithContext(ctx)
|
||||
if err != nil {
|
||||
return 0, 0, utils.Wrap(err, "")
|
||||
}
|
||||
kMsg.Headers = []sarama.RecordHeader{
|
||||
{Key: []byte(constant.OperationID), Value: []byte(operationID)},
|
||||
{Key: []byte(constant.OpUserID), Value: []byte(opUserID)},
|
||||
{Key: []byte(constant.OpUserPlatform), Value: []byte(platform)},
|
||||
{Key: []byte(constant.ConnID), Value: []byte(connID)}}
|
||||
kMsg.Headers = header
|
||||
partition, offset, err := p.producer.SendMessage(kMsg)
|
||||
log.ZDebug(ctx, "ByteEncoder SendMessage end", "key ", kMsg.Key, "key length", kMsg.Value.Length())
|
||||
if err == nil {
|
||||
|
||||
@@ -147,14 +147,18 @@ func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []interface{}) [
|
||||
operationID := mcontext.GetOperationID(ctx)
|
||||
opUserID := mcontext.GetOpUserID(ctx)
|
||||
connID := mcontext.GetConnID(ctx)
|
||||
triggerID := mcontext.GetTriggerID(ctx)
|
||||
if opUserID != "" {
|
||||
keysAndValues = append([]interface{}{constant.OpUserID, mcontext.GetOpUserID(ctx)}, keysAndValues...)
|
||||
keysAndValues = append([]interface{}{constant.OpUserID, opUserID}, keysAndValues...)
|
||||
}
|
||||
if operationID != "" {
|
||||
keysAndValues = append([]interface{}{constant.OperationID, mcontext.GetOperationID(ctx)}, keysAndValues...)
|
||||
keysAndValues = append([]interface{}{constant.OperationID, operationID}, keysAndValues...)
|
||||
}
|
||||
if connID != "" {
|
||||
keysAndValues = append([]interface{}{constant.ConnID, mcontext.GetConnID(ctx)}, keysAndValues...)
|
||||
keysAndValues = append([]interface{}{constant.ConnID, connID}, keysAndValues...)
|
||||
}
|
||||
if triggerID != "" {
|
||||
keysAndValues = append([]interface{}{constant.TriggerID, triggerID}, keysAndValues...)
|
||||
}
|
||||
return keysAndValues
|
||||
}
|
||||
|
||||
+44
-16
@@ -6,6 +6,17 @@ import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
)
|
||||
|
||||
var mapper = []string{constant.OperationID, constant.OpUserID, constant.OpUserPlatform, constant.ConnID}
|
||||
|
||||
func WithOpUserIDContext(ctx context.Context, opUserID string) context.Context {
|
||||
return context.WithValue(ctx, constant.OpUserID, opUserID)
|
||||
}
|
||||
func WithOpUserPlatformContext(ctx context.Context, platform string) context.Context {
|
||||
return context.WithValue(ctx, constant.OpUserPlatform, platform)
|
||||
}
|
||||
func WithTriggerIDContext(ctx context.Context, triggerID string) context.Context {
|
||||
return context.WithValue(ctx, constant.TriggerID, triggerID)
|
||||
}
|
||||
func NewCtx(operationID string) context.Context {
|
||||
c := context.Background()
|
||||
ctx := context.WithValue(c, constant.OperationID, operationID)
|
||||
@@ -34,6 +45,34 @@ func GetOperationID(ctx context.Context) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func GetOpUserID(ctx context.Context) string {
|
||||
if ctx.Value(constant.OpUserID) != "" {
|
||||
s, ok := ctx.Value(constant.OpUserID).(string)
|
||||
if ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func GetConnID(ctx context.Context) string {
|
||||
if ctx.Value(constant.ConnID) != "" {
|
||||
s, ok := ctx.Value(constant.ConnID).(string)
|
||||
if ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetTriggerID(ctx context.Context) string {
|
||||
if ctx.Value(constant.TriggerID) != "" {
|
||||
s, ok := ctx.Value(constant.TriggerID).(string)
|
||||
if ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func GetMustCtxInfo(ctx context.Context) (operationID, opUserID, platform, connID string, err error) {
|
||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||
if !ok {
|
||||
@@ -54,23 +93,12 @@ func GetMustCtxInfo(ctx context.Context) (operationID, opUserID, platform, connI
|
||||
return
|
||||
|
||||
}
|
||||
func WithMustInfoCtx(values []string) context.Context {
|
||||
ctx := context.Background()
|
||||
for i, v := range values {
|
||||
ctx = context.WithValue(ctx, mapper[i], v)
|
||||
|
||||
func GetOpUserID(ctx context.Context) string {
|
||||
if ctx.Value(constant.OpUserID) != "" {
|
||||
s, ok := ctx.Value(constant.OpUserID).(string)
|
||||
if ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return ctx
|
||||
|
||||
func GetConnID(ctx context.Context) string {
|
||||
if ctx.Value(constant.ConnID) != "" {
|
||||
s, ok := ctx.Value(constant.ConnID).(string)
|
||||
if ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user