group rpc

This commit is contained in:
wangchuxiao 2023-01-09 16:37:33 +08:00
parent ac74d01d35
commit 69787064e9
5 changed files with 181 additions and 337 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"gorm.io/gorm"
)
@ -49,6 +50,7 @@ var (
ErrDismissedAlready = ErrInfo{DismissedAlreadyError, "DismissedAlreadyError", ""}
ErrOwnerNotAllowedQuit = ErrInfo{OwnerNotAllowedQuitError, "OwnerNotAllowedQuitError", ""}
ErrRegisteredAlready = ErrInfo{RegisteredAlreadyError, "RegisteredAlreadyError", ""}
ErrGroupTypeNotSupport = ErrInfo{GroupTypeNotSupport, "", ""}
ErrDefaultOther = ErrInfo{DefaultOtherError, "DefaultOtherError", ""}
ErrData = ErrInfo{DataError, "DataError", ""}
@ -102,6 +104,20 @@ func ToAPIErrWithErr(err error) ErrInfo {
return ErrDefaultOther
}
func SetErrorForResp(err error, commonResp *sdkws.CommonResp) {
errInfo := ToAPIErrWithErr(err)
commonResp.ErrCode = errInfo.ErrCode
commonResp.ErrMsg = errInfo.ErrMsg
commonResp.DetailErrMsg = err.Error()
}
func CommonResp2Err(resp sdkws.CommonResp) error {
if resp.ErrCode != NoError {
return errors.New(fmt.Sprintf("call rpc error, errCode is %d, errMsg is %s, detailErrMsg is %s", resp.ErrCode, resp.ErrMsg, resp.DetailErrMsg))
}
return nil
}
func Error2CommResp(ctx context.Context, info ErrInfo, detailErrMsg string) *sdkws.CommonResp {
err := &sdkws.CommonResp{
ErrCode: info.ErrCode,
@ -166,6 +182,7 @@ const (
NotInGroupYetError = 93004 //不在群组中
DismissedAlreadyError = 93004 //群组已经解散
OwnerNotAllowedQuitError = 93004 //群主不能退群
GroupTypeNotSupport = 93005
)
// 用户错误码

View File

@ -123,7 +123,10 @@ func GetBlackListFromCache(ctx context.Context, userID string) (blackIDList []st
return blackIDList, utils.Wrap(err, "")
}
func DelBlackIDListFromCache(userID string) error {
func DelBlackIDListFromCache(ctx context.Context, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "ctx", ctx)
}()
return db.DB.Rc.TagAsDeleted(blackListCache + userID)
}
@ -150,7 +153,10 @@ func GetJoinedGroupIDListFromCache(ctx context.Context, userID string) (joinedGr
return joinedGroupList, utils.Wrap(err, "")
}
func DelJoinedGroupIDListFromCache(userID string) error {
func DelJoinedGroupIDListFromCache(ctx context.Context, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
return db.DB.Rc.TagAsDeleted(joinedGroupListCache + userID)
}

View File

@ -28,6 +28,10 @@ func SetOperationID(ctx context.Context, operationID string) {
ctx.Value(TraceLogKey).(*ApiInfo).OperationID = operationID
}
func GetOperationID(ctx context.Context) string {
return ctx.Value(TraceLogKey).(*ApiInfo).OperationID
}
func ShowLog(ctx context.Context) {
t := ctx.Value(TraceLogKey).(*ApiInfo)
if ctx.Value(TraceLogKey).(*ApiInfo).GinCtx != nil {

View File

@ -11,12 +11,12 @@ import (
"strings"
)
func GetConn(ctx context.Context, operationID, serviceName string) (conn *grpc.ClientConn, err error) {
func GetConn(ctx context.Context, serviceName string) (conn *grpc.ClientConn, err error) {
defer func() {
trace_log.SetContextInfo(ctx, "GetConn", err, "serviceName", serviceName)
}()
conn = getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","),
serviceName, operationID, config.Config.Etcd.UserName, config.Config.Etcd.Password)
serviceName, trace_log.GetOperationID(ctx), config.Config.Etcd.UserName, config.Config.Etcd.Password)
if conn == nil {
return nil, constant.ErrRpcConn
}