mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-05 17:45:59 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
This commit is contained in:
@@ -273,8 +273,12 @@ const (
|
||||
const OperationID = "operationID"
|
||||
const OpUserID = "opUserID"
|
||||
const ConnID = "connID"
|
||||
const OpUserIDPlatformID = "platformID"
|
||||
const OpUserPlatform = "platform"
|
||||
const Token = "token"
|
||||
const RpcCustomHeader = "customHeader" // rpc中间件自定义ctx参数
|
||||
const CheckKey = "CheckKey"
|
||||
const TriggerID = "triggerID"
|
||||
const RemoteAddr = "remoteAddr"
|
||||
|
||||
const (
|
||||
UnreliableNotification = 1
|
||||
@@ -325,3 +329,5 @@ const (
|
||||
FlagPrometheusPort = "prometheus_port"
|
||||
FlagConf = "config_folder_path"
|
||||
)
|
||||
|
||||
const OpenIMCommonConfigKey = "OpenIMServerConfig"
|
||||
|
||||
Vendored
-1
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
|
||||
pbMsg "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/tx"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
@@ -106,7 +106,7 @@ func (f *friendDatabase) BecomeFriends(ctx context.Context, ownerUserID string,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opUserID := tracelog.GetOperationID(ctx)
|
||||
opUserID := mcontext.GetOperationID(ctx)
|
||||
for _, v := range friendUserIDs {
|
||||
fs1 = append(fs1, &relation.FriendModel{OwnerUserID: ownerUserID, FriendUserID: v, AddSource: addSource, OperatorUserID: opUserID})
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/kafka"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
|
||||
"github.com/gogo/protobuf/sortkeys"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -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 {
|
||||
@@ -498,7 +497,7 @@ func (db *msgDatabase) GetMsgBySeqs(ctx context.Context, userID string, seqs []i
|
||||
if err != nil {
|
||||
if err != redis.Nil {
|
||||
prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs))
|
||||
log.Error(tracelog.GetOperationID(ctx), "get message from redis exception", err.Error(), failedSeqs)
|
||||
log.Error(mcontext.GetOperationID(ctx), "get message from redis exception", err.Error(), failedSeqs)
|
||||
}
|
||||
}
|
||||
prome.Add(prome.MsgPullFromRedisSuccessCounter, len(successMsgs))
|
||||
@@ -519,7 +518,7 @@ func (db *msgDatabase) GetSuperGroupMsgBySeqs(ctx context.Context, groupID strin
|
||||
if err != nil {
|
||||
if err != redis.Nil {
|
||||
prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs))
|
||||
log.Error(tracelog.GetOperationID(ctx), "get message from redis exception", err.Error(), failedSeqs)
|
||||
log.Error(mcontext.GetOperationID(ctx), "get message from redis exception", err.Error(), failedSeqs)
|
||||
}
|
||||
}
|
||||
prome.Add(prome.MsgPullFromRedisSuccessCounter, len(successMsgs))
|
||||
@@ -604,7 +603,7 @@ func (db *msgDatabase) deleteMsgRecursion(ctx context.Context, sourceID string,
|
||||
if err != nil || msgs.DocID == "" {
|
||||
if err != nil {
|
||||
if err == unrelation.ErrMsgListNotExist {
|
||||
log.NewDebug(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), "ID:", sourceID, "index:", index, err.Error())
|
||||
log.NewDebug(mcontext.GetOperationID(ctx), utils.GetSelfFuncName(), "ID:", sourceID, "index:", index, err.Error())
|
||||
} else {
|
||||
//log.NewError(operationID, utils.GetSelfFuncName(), "GetUserMsgListByIndex failed", err.Error(), index, ID)
|
||||
}
|
||||
@@ -618,7 +617,7 @@ func (db *msgDatabase) deleteMsgRecursion(ctx context.Context, sourceID string,
|
||||
}
|
||||
//log.NewDebug(operationID, "ID:", sourceID, "index:", index, "uid:", msgs.UID, "len:", len(msgs.Msg))
|
||||
if int64(len(msgs.Msg)) > db.msg.GetSingleGocMsgNum() {
|
||||
log.NewWarn(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), "msgs too large:", len(msgs.Msg), "docID:", msgs.DocID)
|
||||
log.NewWarn(mcontext.GetOperationID(ctx), utils.GetSelfFuncName(), "msgs too large:", len(msgs.Msg), "docID:", msgs.DocID)
|
||||
}
|
||||
if msgs.Msg[len(msgs.Msg)-1].SendTime+(remainTime*1000) < utils.GetCurrentTimestampByMill() && msgs.IsFull() {
|
||||
delStruct.delDocIDs = append(delStruct.delDocIDs, msgs.DocID)
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
package relation
|
||||
package ormutil
|
||||
|
||||
import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func gormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
func GormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
var count int64
|
||||
if err := db.Count(&count).Error; err != nil {
|
||||
return 0, nil, utils.Wrap(err, "")
|
||||
var model E
|
||||
if err := db.Model(&model).Count(&count).Error; err != nil {
|
||||
return 0, nil, errs.Wrap(err)
|
||||
}
|
||||
var es []*E
|
||||
if err := db.Limit(int(showNumber)).Offset(int(pageNumber * showNumber)).Find(&es).Error; err != nil {
|
||||
return 0, nil, utils.Wrap(err, "")
|
||||
return 0, nil, errs.Wrap(err)
|
||||
}
|
||||
return uint32(count), es, nil
|
||||
}
|
||||
|
||||
func gormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
func GormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
if len(fields) > 0 && value != "" {
|
||||
value = "%" + value + "%"
|
||||
if len(fields) == 1 {
|
||||
@@ -30,23 +31,23 @@ func gormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, s
|
||||
db = db.Where(t)
|
||||
}
|
||||
}
|
||||
return gormPage[E](db, pageNumber, showNumber)
|
||||
return GormPage[E](db, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func gormIn[E any](db **gorm.DB, field string, es []E) {
|
||||
func GormIn[E any](db **gorm.DB, field string, es []E) {
|
||||
if len(es) == 0 {
|
||||
return
|
||||
}
|
||||
*db = (*db).Where(field+" in (?)", es)
|
||||
}
|
||||
|
||||
func mapCount(db *gorm.DB, field string) (map[string]uint32, error) {
|
||||
func MapCount(db *gorm.DB, field string) (map[string]uint32, error) {
|
||||
var items []struct {
|
||||
ID string `gorm:"column:id"`
|
||||
Count uint32 `gorm:"column:count"`
|
||||
}
|
||||
if err := db.Select(field + " as id, count(1) as count").Group(field).Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
m := make(map[string]uint32)
|
||||
for _, item := range items {
|
||||
@@ -2,6 +2,7 @@ package relation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
@@ -50,7 +51,7 @@ func (b *BlackGorm) FindOwnerBlacks(ctx context.Context, ownerUserID string, pag
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
totalUint32, blacks, err := gormPage[relation.BlackModel](b.db(ctx), pageNumber, showNumber)
|
||||
totalUint32, blacks, err := ormutil.GormPage[relation.BlackModel](b.db(ctx), pageNumber, showNumber)
|
||||
total = int64(totalUint32)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
@@ -72,14 +73,14 @@ func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string) (groupM
|
||||
|
||||
func (g *GroupMemberGorm) SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (total uint32, groupList []*relation.GroupMemberModel, err error) {
|
||||
db := g.DB
|
||||
gormIn(&db, "group_id", groupIDs)
|
||||
gormIn(&db, "user_id", userIDs)
|
||||
gormIn(&db, "role_level", roleLevels)
|
||||
return gormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
||||
ormutil.GormIn(&db, "group_id", groupIDs)
|
||||
ormutil.GormIn(&db, "user_id", userIDs)
|
||||
ormutil.GormIn(&db, "role_level", roleLevels)
|
||||
return ormutil.GormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) MapGroupMemberNum(ctx context.Context, groupIDs []string) (count map[string]uint32, err error) {
|
||||
return mapCount(g.DB.Where("group_id in (?)", groupIDs), "group_id")
|
||||
return ormutil.MapCount(g.DB.Where("group_id in (?)", groupIDs), "group_id")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string) (groupUsers map[string][]string, err error) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package relation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
@@ -43,7 +44,7 @@ func (g *GroupGorm) Take(ctx context.Context, groupID string) (group *relation.G
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupModel, err error) {
|
||||
return gormSearch[relation.GroupModel](g.DB, []string{"name"}, keyword, pageNumber, showNumber)
|
||||
return ormutil.GormSearch[relation.GroupModel](g.DB, []string{"name"}, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupGorm) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package relation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
@@ -39,5 +40,5 @@ func (g *GroupRequestGorm) Take(ctx context.Context, groupID string, userID stri
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Page(ctx context.Context, userID string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupRequestModel, err error) {
|
||||
return gormSearch[relation.GroupRequestModel](g.DB.Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
|
||||
return ormutil.GormSearch[relation.GroupRequestModel](g.DB.Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
|
||||
}
|
||||
|
||||
@@ -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/tracelog"
|
||||
"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 := tracelog.NewCtx(rootFuncName)
|
||||
var operationID string
|
||||
for _, v := range cMsg.Headers {
|
||||
if string(v.Key) == constant.OperationID {
|
||||
operationID = string(v.Value)
|
||||
}
|
||||
}
|
||||
tracelog.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) {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
log "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
|
||||
"github.com/Shopify/sarama"
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
prome "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome"
|
||||
)
|
||||
|
||||
var emptyMsg = errors.New("binary msg is empty")
|
||||
|
||||
type Producer struct {
|
||||
topic string
|
||||
addr []string
|
||||
@@ -45,32 +47,48 @@ 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) {
|
||||
operationID := tracelog.GetOperationID(ctx)
|
||||
log.Info(operationID, "SendMessage", "key ", key, m.String(), p.producer)
|
||||
log.ZDebug(ctx, "SendMessage", "key ", key, "msg", m.String())
|
||||
kMsg := &sarama.ProducerMessage{}
|
||||
kMsg.Topic = p.topic
|
||||
kMsg.Key = sarama.StringEncoder(key)
|
||||
bMsg, err := proto.Marshal(m)
|
||||
if err != nil {
|
||||
log.Error(operationID, "", "proto marshal err = %s", err.Error())
|
||||
return -1, -1, err
|
||||
return 0, 0, utils.Wrap(err, "kafka proto Marshal err")
|
||||
}
|
||||
if len(bMsg) == 0 {
|
||||
log.Error(operationID, "len(bMsg) == 0 ")
|
||||
return 0, 0, errors.New("len(bMsg) == 0 ")
|
||||
return 0, 0, utils.Wrap(emptyMsg, "")
|
||||
}
|
||||
kMsg.Value = sarama.ByteEncoder(bMsg)
|
||||
log.Info(operationID, "ByteEncoder SendMessage begin", "key ", kMsg, p.producer, "len: ", kMsg.Key.Length(), kMsg.Value.Length())
|
||||
if kMsg.Key.Length() == 0 || kMsg.Value.Length() == 0 {
|
||||
log.Error(operationID, "kMsg.Key.Length() == 0 || kMsg.Value.Length() == 0 ", kMsg)
|
||||
return -1, -1, errors.New("key or value == 0")
|
||||
return 0, 0, utils.Wrap(emptyMsg, "")
|
||||
}
|
||||
kMsg.Metadata = ctx
|
||||
kMsg.Headers = []sarama.RecordHeader{{Key: []byte(constant.OperationID), Value: []byte(operationID)}}
|
||||
header, err := GetMQHeaderWithContext(ctx)
|
||||
if err != nil {
|
||||
return 0, 0, utils.Wrap(err, "")
|
||||
}
|
||||
kMsg.Headers = header
|
||||
partition, offset, err := p.producer.SendMessage(kMsg)
|
||||
log.Info(operationID, "ByteEncoder SendMessage end", "key ", kMsg.Key.Length(), kMsg.Value.Length(), p.producer)
|
||||
log.ZDebug(ctx, "ByteEncoder SendMessage end", "key ", kMsg.Key, "key length", kMsg.Value.Length())
|
||||
if err == nil {
|
||||
prome.Inc(prome.SendMsgCounter)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bufio"
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||
|
||||
//"bufio"
|
||||
"fmt"
|
||||
@@ -99,7 +99,7 @@ func loggerInit(moduleName string) *LogrusLogger {
|
||||
}
|
||||
|
||||
func InfoKv(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||||
operationID := tracelog.GetOperationID(ctx)
|
||||
operationID := mcontext.GetOperationID(ctx)
|
||||
logger.WithFields(logrus.Fields{
|
||||
"OperationID": operationID,
|
||||
"PID": logger.Pid,
|
||||
|
||||
+20
-8
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -139,22 +139,34 @@ func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndVal
|
||||
if err != nil {
|
||||
keysAndValues = append(keysAndValues, "error", err.Error())
|
||||
}
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
keysAndValues = append([]interface{}{constant.OperationID, mcontext.GetOperationID(ctx)}, keysAndValues...)
|
||||
l.zap.Errorw(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []interface{}) []interface{} {
|
||||
operationID := tracelog.GetOperationID(ctx)
|
||||
opUserID := tracelog.GetOpUserID(ctx)
|
||||
connID := tracelog.GetConnID(ctx)
|
||||
operationID := mcontext.GetOperationID(ctx)
|
||||
opUserID := mcontext.GetOpUserID(ctx)
|
||||
connID := mcontext.GetConnID(ctx)
|
||||
triggerID := mcontext.GetTriggerID(ctx)
|
||||
opUserPlatform := mcontext.GetOpUserPlatform(ctx)
|
||||
remoteAddr := mcontext.GetRemoteAddr(ctx)
|
||||
if opUserID != "" {
|
||||
keysAndValues = append([]interface{}{constant.OpUserID, tracelog.GetOpUserID(ctx)}, keysAndValues...)
|
||||
keysAndValues = append([]interface{}{constant.OpUserID, opUserID}, keysAndValues...)
|
||||
}
|
||||
if operationID != "" {
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
keysAndValues = append([]interface{}{constant.OperationID, operationID}, keysAndValues...)
|
||||
}
|
||||
if connID != "" {
|
||||
keysAndValues = append([]interface{}{constant.ConnID, tracelog.GetConnID(ctx)}, keysAndValues...)
|
||||
keysAndValues = append([]interface{}{constant.ConnID, connID}, keysAndValues...)
|
||||
}
|
||||
if triggerID != "" {
|
||||
keysAndValues = append([]interface{}{constant.TriggerID, triggerID}, keysAndValues...)
|
||||
}
|
||||
if opUserPlatform != "" {
|
||||
keysAndValues = append([]interface{}{constant.OpUserPlatform, opUserPlatform}, keysAndValues...)
|
||||
}
|
||||
if remoteAddr != "" {
|
||||
keysAndValues = append([]interface{}{constant.RemoteAddr, remoteAddr}, keysAndValues...)
|
||||
}
|
||||
return keysAndValues
|
||||
}
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
package mcontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"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)
|
||||
return SetOperationID(ctx, operationID)
|
||||
}
|
||||
|
||||
func SetOperationID(ctx context.Context, operationID string) context.Context {
|
||||
return context.WithValue(ctx, constant.OperationID, operationID)
|
||||
}
|
||||
|
||||
func SetOpUserID(ctx context.Context, opUserID string) context.Context {
|
||||
return context.WithValue(ctx, constant.OpUserID, opUserID)
|
||||
}
|
||||
|
||||
func SetConnID(ctx context.Context, connID string) context.Context {
|
||||
return context.WithValue(ctx, constant.ConnID, connID)
|
||||
}
|
||||
|
||||
func GetOperationID(ctx context.Context) string {
|
||||
if ctx.Value(constant.OperationID) != nil {
|
||||
s, ok := ctx.Value(constant.OperationID).(string)
|
||||
if ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
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 GetOpUserPlatform(ctx context.Context) string {
|
||||
if ctx.Value(constant.OpUserPlatform) != "" {
|
||||
s, ok := ctx.Value(constant.OpUserPlatform).(string)
|
||||
if ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func GetRemoteAddr(ctx context.Context) string {
|
||||
if ctx.Value(constant.RemoteAddr) != "" {
|
||||
s, ok := ctx.Value(constant.RemoteAddr).(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 {
|
||||
err = errs.ErrArgs.Wrap("ctx missing operationID")
|
||||
return
|
||||
}
|
||||
opUserID, ok1 := ctx.Value(constant.OpUserID).(string)
|
||||
if !ok1 {
|
||||
err = errs.ErrArgs.Wrap("ctx missing opUserID")
|
||||
return
|
||||
}
|
||||
platform, ok2 := ctx.Value(constant.OpUserPlatform).(string)
|
||||
if !ok2 {
|
||||
err = errs.ErrArgs.Wrap("ctx missing platform")
|
||||
return
|
||||
}
|
||||
connID, _ = ctx.Value(constant.ConnID).(string)
|
||||
return
|
||||
|
||||
}
|
||||
func WithMustInfoCtx(values []string) context.Context {
|
||||
ctx := context.Background()
|
||||
for i, v := range values {
|
||||
ctx = context.WithValue(ctx, mapper[i], v)
|
||||
|
||||
}
|
||||
return ctx
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package mw
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
block cipher.Block
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func initAesKey() {
|
||||
once.Do(func() {
|
||||
key := md5.Sum([]byte("openim:" + config.Config.Secret))
|
||||
var err error
|
||||
block, err = aes.NewCipher(key[:])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func genReqKey(args []string) string {
|
||||
initAesKey()
|
||||
plaintext := md5.Sum([]byte(strings.Join(args, ":")))
|
||||
iv := make([]byte, aes.BlockSize, aes.BlockSize+md5.Size)
|
||||
if _, err := rand.Read(iv); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ciphertext := make([]byte, md5.Size)
|
||||
cipher.NewCBCEncrypter(block, iv).CryptBlocks(ciphertext, plaintext[:])
|
||||
return base64.StdEncoding.EncodeToString(append(iv, ciphertext...))
|
||||
}
|
||||
|
||||
func verifyReqKey(args []string, key string) error {
|
||||
initAesKey()
|
||||
k, err := base64.StdEncoding.DecodeString(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid key %v", err)
|
||||
}
|
||||
if len(k) != aes.BlockSize+md5.Size {
|
||||
return errors.New("invalid key")
|
||||
}
|
||||
plaintext := make([]byte, md5.Size)
|
||||
cipher.NewCBCDecrypter(block, k[:aes.BlockSize]).CryptBlocks(plaintext, k[aes.BlockSize:])
|
||||
sum := md5.Sum([]byte(strings.Join(args, ":")))
|
||||
if string(plaintext) != string(sum[:]) {
|
||||
return errors.New("mismatch key")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package mw
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheck(t *testing.T) {
|
||||
config.Config.Secret = "123456"
|
||||
|
||||
args := []string{"1", "2", "3"}
|
||||
|
||||
key := genReqKey(args)
|
||||
fmt.Println("key:", key)
|
||||
err := verifyReqKey(args, key)
|
||||
|
||||
fmt.Println(err)
|
||||
|
||||
args = []string{"4", "5", "6"}
|
||||
|
||||
key = genReqKey(args)
|
||||
fmt.Println("key:", key)
|
||||
err = verifyReqKey(args, key)
|
||||
|
||||
fmt.Println(err)
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package mw
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
@@ -90,8 +91,9 @@ func GinParseOperationID() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
if req.OperationID == "" {
|
||||
log.ZWarn(c, "header must have operationID", errs.ErrArgs.Wrap(err.Error()))
|
||||
apiresp.GinError(c, errs.ErrArgs.Wrap("header must have operationID"+err.Error()))
|
||||
err := errors.New("header must have operationID")
|
||||
log.ZWarn(c, "header must have operationID", err)
|
||||
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error()))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -153,7 +155,7 @@ func GinParseToken(rdb redis.UniversalClient) gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Set(constant.OpUserIDPlatformID, constant.PlatformNameToID(claims.Platform))
|
||||
c.Set(constant.OpUserPlatform, claims.Platform)
|
||||
c.Set(constant.OpUserID, claims.UID)
|
||||
c.Next()
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package mw
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
@@ -22,17 +23,11 @@ func rpcClientInterceptor(ctx context.Context, method string, req, resp interfac
|
||||
return errs.ErrInternalServer.Wrap("call rpc request context is nil")
|
||||
}
|
||||
log.ZInfo(ctx, "rpc client req", "funcName", method, "req", rpcString(req))
|
||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||
if !ok {
|
||||
log.ZWarn(ctx, "ctx missing operationID", errors.New("ctx missing operationID"), "funcName", method)
|
||||
return errs.ErrArgs.Wrap("ctx missing operationID")
|
||||
ctx, err = getRpcContext(ctx, method)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
md := metadata.Pairs(constant.OperationID, operationID)
|
||||
opUserID, ok := ctx.Value(constant.OpUserID).(string)
|
||||
if ok {
|
||||
md.Append(constant.OpUserID, opUserID)
|
||||
}
|
||||
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, resp, cc, opts...)
|
||||
err = invoker(ctx, method, req, resp, cc, opts...)
|
||||
if err == nil {
|
||||
log.ZInfo(ctx, "rpc client resp", "funcName", method, "resp", rpcString(resp))
|
||||
return nil
|
||||
@@ -55,3 +50,43 @@ func rpcClientInterceptor(ctx context.Context, method string, req, resp interfac
|
||||
}
|
||||
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
|
||||
}
|
||||
|
||||
func getRpcContext(ctx context.Context, method string) (context.Context, error) {
|
||||
md := metadata.Pairs()
|
||||
if keys, _ := ctx.Value(constant.RpcCustomHeader).([]string); len(keys) > 0 {
|
||||
for _, key := range keys {
|
||||
val, ok := ctx.Value(key).([]string)
|
||||
if !ok {
|
||||
return nil, errs.ErrInternalServer.Wrap(fmt.Sprintf("ctx missing key %s", key))
|
||||
}
|
||||
if len(val) == 0 {
|
||||
return nil, errs.ErrInternalServer.Wrap(fmt.Sprintf("ctx key %s value is empty", key))
|
||||
}
|
||||
md.Set(key, val...)
|
||||
}
|
||||
md.Set(constant.RpcCustomHeader, keys...)
|
||||
}
|
||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||
if !ok {
|
||||
log.ZWarn(ctx, "ctx missing operationID", errors.New("ctx missing operationID"), "funcName", method)
|
||||
return nil, errs.ErrArgs.Wrap("ctx missing operationID")
|
||||
}
|
||||
md.Set(constant.OperationID, operationID)
|
||||
var checkArgs []string
|
||||
checkArgs = append(checkArgs, constant.OperationID, operationID)
|
||||
opUserID, ok := ctx.Value(constant.OpUserID).(string)
|
||||
if ok {
|
||||
md.Set(constant.OpUserID, opUserID)
|
||||
checkArgs = append(checkArgs, constant.OpUserID, opUserID)
|
||||
}
|
||||
opUserIDPlatformID, ok := ctx.Value(constant.OpUserPlatform).(string)
|
||||
if ok {
|
||||
md.Set(constant.OpUserPlatform, opUserIDPlatformID)
|
||||
}
|
||||
connID, ok := ctx.Value(constant.ConnID).(string)
|
||||
if ok {
|
||||
md.Set(constant.ConnID, connID)
|
||||
}
|
||||
md.Set(constant.CheckKey, genReqKey(checkArgs))
|
||||
return metadata.NewOutgoingContext(ctx, md), nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package mw
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"math"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
@@ -20,9 +21,6 @@ import (
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
const OperationID = "operationID"
|
||||
const OpUserID = "opUserID"
|
||||
|
||||
func rpcString(v interface{}) string {
|
||||
if s, ok := v.(interface{ String() string }); ok {
|
||||
return s.String()
|
||||
@@ -31,7 +29,6 @@ func rpcString(v interface{}) string {
|
||||
}
|
||||
|
||||
func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||
var operationID string
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.ZError(ctx, "rpc panic", nil, "FullMethod", info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r)
|
||||
@@ -59,17 +56,39 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
||||
if !ok {
|
||||
return nil, status.New(codes.InvalidArgument, "missing metadata").Err()
|
||||
}
|
||||
if opts := md.Get(OperationID); len(opts) != 1 || opts[0] == "" {
|
||||
if keys := md.Get(constant.RpcCustomHeader); len(keys) > 0 {
|
||||
for _, key := range keys {
|
||||
values := md.Get(key)
|
||||
if len(values) == 0 {
|
||||
return nil, status.New(codes.InvalidArgument, fmt.Sprintf("missing metadata key %s", key)).Err()
|
||||
}
|
||||
ctx = context.WithValue(ctx, key, values)
|
||||
}
|
||||
}
|
||||
args := make([]string, 0, 4)
|
||||
if opts := md.Get(constant.OperationID); len(opts) != 1 || opts[0] == "" {
|
||||
return nil, status.New(codes.InvalidArgument, "operationID error").Err()
|
||||
} else {
|
||||
operationID = opts[0]
|
||||
args = append(args, constant.OperationID, opts[0])
|
||||
ctx = context.WithValue(ctx, constant.OperationID, opts[0])
|
||||
}
|
||||
var opUserID string
|
||||
if opts := md.Get(OpUserID); len(opts) == 1 {
|
||||
opUserID = opts[0]
|
||||
if opts := md.Get(constant.OpUserID); len(opts) == 1 {
|
||||
args = append(args, constant.OpUserID, opts[0])
|
||||
ctx = context.WithValue(ctx, constant.OpUserID, opts[0])
|
||||
}
|
||||
if opts := md.Get(constant.OpUserPlatform); len(opts) == 1 {
|
||||
ctx = context.WithValue(ctx, constant.OpUserPlatform, opts[0])
|
||||
}
|
||||
if opts := md.Get(constant.ConnID); len(opts) == 1 {
|
||||
ctx = context.WithValue(ctx, constant.ConnID, opts[0])
|
||||
}
|
||||
if opts := md.Get(constant.CheckKey); len(opts) != 1 || opts[0] == "" {
|
||||
return nil, status.New(codes.InvalidArgument, "check key empty").Err()
|
||||
} else {
|
||||
if err := verifyReqKey(args, opts[0]); err != nil {
|
||||
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
|
||||
}
|
||||
}
|
||||
ctx = context.WithValue(ctx, OperationID, operationID)
|
||||
ctx = context.WithValue(ctx, OpUserID, opUserID)
|
||||
log.ZInfo(ctx, "rpc server req", "funcName", funcName, "req", rpcString(req))
|
||||
resp, err = handler(ctx, req)
|
||||
if err == nil {
|
||||
@@ -119,7 +138,7 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.ZError(ctx, "rpc server resp", err, "funcName", funcName)
|
||||
log.ZWarn(ctx, "rpc server resp", err, "funcName", funcName)
|
||||
return nil, details.Err()
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
@@ -61,7 +61,7 @@ func GetClaimFromToken(tokensString string) (*Claims, error) {
|
||||
}
|
||||
|
||||
func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
|
||||
opUserID := tracelog.GetOpUserID(ctx)
|
||||
opUserID := mcontext.GetOpUserID(ctx)
|
||||
if utils.IsContain(opUserID, config.Config.Manager.AppManagerUid) {
|
||||
return nil
|
||||
}
|
||||
@@ -72,14 +72,14 @@ func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
|
||||
}
|
||||
|
||||
func IsAppManagerUid(ctx context.Context) bool {
|
||||
return utils.IsContain(tracelog.GetOpUserID(ctx), config.Config.Manager.AppManagerUid)
|
||||
return utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.Manager.AppManagerUid)
|
||||
}
|
||||
|
||||
func CheckAdmin(ctx context.Context) error {
|
||||
if utils.IsContain(tracelog.GetOpUserID(ctx), config.Config.Manager.AppManagerUid) {
|
||||
if utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.Manager.AppManagerUid) {
|
||||
return nil
|
||||
}
|
||||
return errs.ErrIdentity.Wrap(fmt.Sprintf("user %s is not admin userID", tracelog.GetOpUserID(ctx)))
|
||||
return errs.ErrIdentity.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx)))
|
||||
}
|
||||
|
||||
func ParseRedisInterfaceToken(redisToken interface{}) (*Claims, error) {
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
package tracelog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
)
|
||||
|
||||
func NewCtx(operationID string) context.Context {
|
||||
c := context.Background()
|
||||
ctx := context.WithValue(c, constant.OperationID, operationID)
|
||||
SetOperationID(ctx, operationID)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func SetOperationID(ctx context.Context, operationID string) {
|
||||
ctx = context.WithValue(ctx, constant.OperationID, operationID)
|
||||
}
|
||||
|
||||
func SetOpUserID(ctx context.Context, opUserID string) {
|
||||
ctx = context.WithValue(ctx, constant.OpUserID, opUserID)
|
||||
}
|
||||
|
||||
func SetConnID(ctx context.Context, connID string) {
|
||||
ctx = context.WithValue(ctx, constant.ConnID, connID)
|
||||
}
|
||||
|
||||
func GetOperationID(ctx context.Context) string {
|
||||
if ctx.Value(constant.OperationID) != nil {
|
||||
s, ok := ctx.Value(constant.OperationID).(string)
|
||||
if ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
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 ""
|
||||
}
|
||||
Reference in New Issue
Block a user