diff --git a/cmd/cmd.go b/cmd/cmd.go index 32042e119..b5cdced36 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -5,6 +5,7 @@ import ( "go_dreamfactory/cmd/robot" "os" + "github.com/sirupsen/logrus" flag "github.com/spf13/pflag" "github.com/spf13/cobra" @@ -28,6 +29,7 @@ func Execute() { func init() { RootCmd.AddCommand(runCmd) + initLog() } var account = flag.String("account", "", "登录账号") @@ -51,3 +53,24 @@ var runCmd = &cobra.Command{ r.Run() }, } + +func initLog() { + logrus.New() + logrus.SetFormatter(&logrus.TextFormatter{ + ForceColors: false, + FullTimestamp: false, + }) + + //设置output,默认为stderr,可以为任何io.Writer,比如文件*os.File + // file, err := os.OpenFile("robot.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + // writers := []io.Writer{ + // file, + // os.Stdout} + // //同时写文件和屏幕 + // fileAndStdoutWriter := io.MultiWriter(writers...) + // if err == nil { + // logrus.SetOutput(fileAndStdoutWriter) + // } else { + // logrus.Info("failed to log to file.") + // } +} diff --git a/cmd/robot/friend.go b/cmd/robot/friend.go index ea9b1de76..40fc95760 100644 --- a/cmd/robot/friend.go +++ b/cmd/robot/friend.go @@ -91,7 +91,7 @@ var ( enabled: true, } tcs = append(tcs, tc) - robot.addBuilders(tcs) + robot.addTestCaseAndReq(tcs) //这里一定要调用此方法才会发送请求 } }, diff --git a/cmd/robot/hero.go b/cmd/robot/hero.go index b36ff7498..9d4138080 100644 --- a/cmd/robot/hero.go +++ b/cmd/robot/hero.go @@ -1,7 +1,6 @@ package robot import ( - "fmt" "go_dreamfactory/comm" "go_dreamfactory/modules/hero" "go_dreamfactory/pb" @@ -18,12 +17,12 @@ var ( subType: hero.HeroSubTypeList, req: &pb.HeroListReq{}, rsp: &pb.HeroListResp{}, - print: func(rsp proto.Message) { - out := rsp.(*pb.HeroListResp) - for i, v := range out.List { - fmt.Printf("%d- %v\n", (i + 1), v) - } - }, + // print: func(rsp proto.Message) { + // out := rsp.(*pb.HeroListResp) + // for i, v := range out.List { + // fmt.Printf("%d- %v\n", (i + 1), v) + // } + // }, enabled: true, next: func(robot *Robot, rsp proto.Message) { tcs := []*TestCase{} @@ -38,14 +37,14 @@ var ( }, rsp: &pb.HeroInfoResp{}, enabled: true, - print: func(rsp proto.Message) { - r := rsp.(*pb.HeroInfoResp) - fmt.Printf("%v\n", r) - }, + // print: func(rsp proto.Message) { + // r := rsp.(*pb.HeroInfoResp) + // fmt.Printf("%v\n", r) + // }, } tcs = append(tcs, tc) } - robot.addBuilders(tcs) + robot.addTestCaseAndReq(tcs) //这里一定要调用此方法才会发送请求 } }, diff --git a/cmd/robot/login.go b/cmd/robot/login.go index a0fc727c5..7264e407f 100644 --- a/cmd/robot/login.go +++ b/cmd/robot/login.go @@ -60,13 +60,12 @@ func (r *Robot) AccountLogin() { req: &pb.UserCreateReq{ //设置请求参数 NickName: nick, }, - rsp: &pb.UserCreateResp{}, - // enabled: true, + rsp: &pb.UserCreateResp{}, + enabled: r.enable, } tcs = append(tcs, tc) r.addBuilders(tcs) } - }, }, } diff --git a/cmd/robot/notify.go b/cmd/robot/notify.go deleted file mode 100644 index 7e9b5bea0..000000000 --- a/cmd/robot/notify.go +++ /dev/null @@ -1,22 +0,0 @@ -package robot - -import ( - "go_dreamfactory/comm" - "go_dreamfactory/pb" -) - -var notify_builders = []*TestCase{ - { - //create - desc: "全局通知", - mainType: comm.MainTypeNotify, - subType: comm.SubTypeErrorNotify, - rsp: &pb.NotifyErrorNotifyPush{}, - enabled: true, - }, -} - -//声明加入到构建器并发起请求 -func (r *Robot) RunNotify() { - r.addBuilders(notify_builders) -} diff --git a/cmd/robot/robot.go b/cmd/robot/robot.go index c497fe61b..6ab321495 100644 --- a/cmd/robot/robot.go +++ b/cmd/robot/robot.go @@ -5,16 +5,22 @@ import ( "encoding/json" "fmt" "go_dreamfactory/comm" + "io" + "os" + + // zlog "go_dreamfactory/lego/sys/log" + "go_dreamfactory/modules/user" "go_dreamfactory/pb" "io/ioutil" - "log" "net/http" "sync" "time" + "github.com/Pallinder/go-randomdata" "github.com/gorilla/websocket" jsoniter "github.com/json-iterator/go" uuid "github.com/satori/go.uuid" + "github.com/sirupsen/logrus" "google.golang.org/protobuf/proto" ) @@ -24,56 +30,95 @@ type Robot struct { user *pb.DBUser builderMap map[string]*TestCase // linkCase *LinkCase - wg sync.WaitGroup - // reqCh chan string //请求通道uuid - // rspCh chan string //响应通道uuid + enable bool //全局开关 + reqCh chan string + endCh chan struct{} + printFormat bool //是否格式化结果 + wg sync.WaitGroup + caseTotal int32 //测试数量 + caseSuccess int32 //成功数量 + caseError int32 //失败数量 + start time.Time //启动时间 +} + +var zlog *logrus.Logger + +func initlog() { + zlog = logrus.New() + zlog.SetLevel(logrus.DebugLevel) + zlog.SetFormatter(&RobotFormatter{ + DisableTimestamp: true, + DisableQuote: true, + }) + + //设置output,默认为stderr,可以为任何io.Writer,比如文件*os.File + file, err := os.OpenFile("robot.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666) + writers := []io.Writer{ + file, + os.Stdout} + //同时写文件和屏幕 + fileAndStdoutWriter := io.MultiWriter(writers...) + if err == nil { + zlog.SetOutput(fileAndStdoutWriter) + } else { + zlog.Info("failed to log to file.") + } } func NewRobot(opts *Options) *Robot { + initlog() ws, _, err := websocket.DefaultDialer.Dial(opts.WsUrl, nil) if err != nil { - log.Fatal(err) + zlog.Fatalf("websocket conn err:%v", err) } r := &Robot{ - ws: ws, - opts: opts, - builderMap: make(map[string]*TestCase), - // reqCh: make(chan string, 1), - // rspCh: make(chan string, 10), + ws: ws, + opts: opts, + builderMap: make(map[string]*TestCase), + reqCh: make(chan string, 1), + printFormat: false, } return r } func (r *Robot) Run() { - log.Print("Robot running...") - log.Printf("websocket %s \n", r.opts.WsUrl) + zlog.Debug("Robot running...") + zlog.Infof("websocket %s ", r.opts.WsUrl) if r.opts.Create { //创建新用户 + r.enable = true r.AccountRegister(r.opts.Account, int32(r.opts.ServerId)) } else { if r.opts.Account == "" { - log.Fatal("WARNNING: account is required !!!") + zlog.Fatal("WARNNING: account is required !!!") } r.AccountLogin() } - //处理响应 go func() { for { - var msg *pb.UserMessage = &pb.UserMessage{} - _, data, err := r.ws.ReadMessage() - if err != nil { - log.Println(err) - } - - if err = proto.Unmarshal(data, msg); err != nil { - log.Fatal(err) - } - r.handleRsp(msg) + r.handleRsp() } }() - r.wg.Wait() + ticker := time.NewTicker(time.Second * 5) + go func() { + for { + select { + case <-ticker.C: + if len(r.builderMap) == 0 { + r.printReport(&TestReport{ + caseTotal: r.caseTotal, + caseSuccess: r.caseSuccess, + caseError: r.caseError, + }) + os.Exit(0) + } + } + } + }() + + select {} } type TestCase struct { @@ -85,7 +130,8 @@ type TestCase struct { rsp proto.Message //响应类型 enabled bool //是否启用 start time.Time //启用时间 - requested bool //是否已请求 //请求标识 true已发 + hs time.Duration //耗时 + requested bool //是否是请求的case print func(rsp proto.Message) //定义打印 next func(robot *Robot, rsp proto.Message) //处理下一层用例请求 } @@ -103,28 +149,28 @@ func (r *Robot) addBuilders(builders []*TestCase) { } } } - } //处理用例,发送请求 func (r *Robot) handleReq() { - for _, b := range r.builderMap { - if b.enabled && b.req != nil && !b.requested { - r.wg.Add(1) - time.Sleep(time.Second * 1) - b.start = time.Now() - head := &pb.UserMessage{MainType: b.mainType, SubType: b.subType} - defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), b.req) - err := r.SendToClient(head, b.req) - if err != nil { - delete(r.builderMap, b.id) - log.Print(err) - continue + go func() { + for _, b := range r.builderMap { + if b.enabled && b.req != nil && !b.requested { + time.Sleep(time.Millisecond * 500) + r.reqCh <- b.id + b.start = time.Now() + r.start = time.Now() + head := &pb.UserMessage{MainType: b.mainType, SubType: b.subType} + // defer traceFunc(head.MainType, head.SubType, r.user.GetUid(), b.req) + err := r.SendToClient(head, b.req) + if err != nil { + delete(r.builderMap, b.id) + zlog.Errorf("send to client err:%v", err) + } + b.requested = true } - b.requested = true - // r.reqCh <- b.id } - } + }() } //加入用例并执行请求 @@ -134,64 +180,79 @@ func (r *Robot) addTestCaseAndReq(tcs []*TestCase) { } //错误通知处理 -func (r *Robot) handleNotify(msg *pb.UserMessage) { +func (r *Robot) handleNotify(uuid string, msg *pb.UserMessage) { if msg.MainType == "notify" && msg.SubType == "errornotify" { + r.caseError++ rsp := &pb.NotifyErrorNotifyPush{} if !comm.ProtoUnmarshal(msg, rsp) { return } - printReply(msg, &TestCase{ + + tc := &TestCase{ + id: uuid, desc: "错误通知", mainType: comm.MainTypeNotify, subType: comm.SubTypeErrorNotify, rsp: rsp, - enabled: true, - }) + } + + r.printReply(msg, tc) + delete(r.builderMap, uuid) return } } //处理响应 -func (r *Robot) handleRsp(msg *pb.UserMessage) { - r.handleNotify(msg) - // uuid := <-r.reqCh - // if uuid == "" { - // log.Printf("[%v.%v] uuid is empty", msg.MainType, msg.SubType) - // return - // } - // if v, ok := r.builderMap[uuid]; ok { - for _, v := range r.builderMap { +func (r *Robot) handleRsp() { + defer func() { + r.caseTotal++ + }() + var msg *pb.UserMessage = &pb.UserMessage{} + _, data, err := r.ws.ReadMessage() + if err != nil { + zlog.Fatalf("readMessage err:%v", err) + } + + if err = proto.Unmarshal(data, msg); err != nil { + zlog.Fatalf("unmarshal err:%v", err) + } + + uuid := <-r.reqCh + + if v, ok := r.builderMap[uuid]; ok { + r.handleNotify(uuid, msg) if v.enabled && (msg.MainType == v.mainType && msg.SubType == v.subType) && v.requested { + v.hs = time.Since(v.start) if !comm.ProtoUnmarshal(msg, v.rsp) { return } - //执行自定义打印 - if v.print == nil { - printReply(msg, v) - } else { - fmt.Println() - fmt.Printf("===== %s [%s.%s]=====\n", v.desc, msg.MainType, msg.SubType) - v.print(v.rsp) - fmt.Println("==============================") - } //处理下一层用例 if v.next != nil { v.next(r, v.rsp) } + //执行自定义打印 + if v.print == nil { + r.printReply(msg, v) + } else { + zlog.Debug("==============================") + zlog.Debugf("%s [%s.%s]", v.desc, msg.MainType, msg.SubType) + v.print(v.rsp) + zlog.Debug("==============================") + } + if msg.MainType == "user" && msg.SubType == "login" { r.loginCallback(v.rsp) } else { //清除已执行的用例 delete(r.builderMap, v.id) } - // r.rspCh <- v.id - r.wg.Done() + r.caseSuccess++ } } } @@ -225,14 +286,14 @@ func (r *Robot) SendToClient(msg *pb.UserMessage, rsp proto.Message) error { //注册账号 func (r *Robot) AccountRegister(account string, sid int32) { if account == "" { - log.Fatal("account value is empty") + zlog.Fatal("account value is empty") } //http regReq := &pb.UserRegisterReq{Account: account, Sid: sid} jsonByte, _ := json.Marshal(regReq) req, err := http.NewRequest("POST", r.opts.RegUrl, bytes.NewReader(jsonByte)) if err != nil { - log.Fatalf("account register err %v", err) + zlog.Fatalf("account register err %v", err) } req.Header.Set("Content-Type", "application/json;charset=UTF-8") httpClient := &http.Client{} @@ -261,30 +322,116 @@ func (r *Robot) AccountRegister(account string, sid int32) { }, rsp: &pb.UserLoginResp{}, enabled: true, + next: func(r *Robot, rsp proto.Message) { + tcs := []*TestCase{} + if _, ok := rsp.(*pb.UserLoginResp); ok { + nick := randomdata.SillyName() + tc := &TestCase{ + desc: "创角", + mainType: string(comm.ModuleUser), + subType: user.UserSubTypeCreate, + req: &pb.UserCreateReq{ //设置请求参数 + NickName: nick, + }, + rsp: &pb.UserCreateResp{}, + enabled: r.enable, + } + tcs = append(tcs, tc) + r.addBuilders(tcs) + } + + }, }, } r.addTestCaseAndReq(user_builders) } } +type TestReport struct { + caseTotal int32 //总用例数 + caseSuccess int32 //成功数量 + caseError int32 //失败数量 + hsTotal time.Duration //总耗时 +} + //打印响应 -func printReply(msg *pb.UserMessage, builder *TestCase) { - if m, ok := builder.rsp.(*pb.NotifyErrorNotifyPush); ok { - var tt time.Duration - if builder.start.IsZero() { - tt = time.Duration(0) - } else { - tt = time.Since(builder.start) - } - log.Printf("rsp %s [%v] [%s.%s] [%v:%v]", builder.desc, tt, m.ReqMainType, m.ReqSubType, int32(m.Code), m.Data) +func (r *Robot) printReply(msg *pb.UserMessage, tc *TestCase) { + var ( + code int32 + mainType string + subType string + // arg proto.Message + // rsp proto.Message + // err error + ) + + if m, ok := tc.rsp.(*pb.NotifyErrorNotifyPush); ok { + code = int32(m.Code) + mainType = m.ReqMainType + subType = m.ReqSubType + // arg, _ = jsoniter.MarshalToString(m.Arg) } else { - log.Printf("rsp %s [%v] [%s.%s] [%v]", builder.desc, time.Since(builder.start), msg.MainType, msg.SubType, builder.rsp) + mainType = msg.MainType + subType = msg.SubType + // arg, _ = jsoniter.MarshalToString(tc.req) } + // if r.printFormat { + // s, err := jsoniter.MarshalToString(tc.rsp) + // if err != nil { + // zlog.Errorf("MarshalToString err:%v", err) + // return + // } + // if rsp, err = formatJson(s); err != nil { + // zlog.Errorf("formatJson err:%v", err) + // return + // } + + // } else { + // if rsp, err = jsoniter.MarshalToString(tc.rsp); err != nil { + // zlog.Errorf("MarshalToString err:%v", err) + // return + // } + // } + + //表格显示 + // data = append(data, []string{builder.desc, fmt.Sprintf("%s.%s", mainType, subType), "0", arg, cast.ToString(code)}) + // table.SetHeader([]string{"描述", "协议", "耗时", "参数", "响应码"}) + // for _, v := range data { + // table.Append(v) + // } + // table.Render() + + // + zlog.Debug("-------------------------------------") + zlog.Debugf("uuid:%s", tc.id) + zlog.Debugf("描述:%s", tc.desc) + zlog.Debugf("协议:%s.%s", mainType, subType) + zlog.Debugf("耗时:%v", tc.hs) + zlog.Debugf("参数:%v", tc.req) + zlog.Debugf("响应码:%d", code) + zlog.Debugf("返回:%v", tc.rsp) +} + +//打印测试报告 +func (r *Robot) printReport(report *TestReport) { + zlog.Debug("====================================") + zlog.Infof("测试完毕,开始输出报告") + zlog.Infof("用例总数:%d", r.caseTotal) + zlog.Infof("成功:%d", r.caseSuccess) + zlog.Infof("失败:%d", r.caseError) + zlog.Infof("总耗时: %v", time.Since(r.start)) +} + +//格式化json +func formatJson(data string) (string, error) { + var out bytes.Buffer + err := json.Indent(&out, []byte(data), "", " ") + return out.String(), err } //方法参数跟踪 func traceFunc(module string, funcName string, uid string, funcArgs interface{}) { - log.Printf("req [%s.%s] [%s] [%v]", module, funcName, uid, funcArgs) + zlog.Debugf("req [%s.%s] [%s] [%v]", module, funcName, uid, funcArgs) } //在这里添加玩家成功登录以后的测试方法 @@ -306,3 +453,29 @@ func (r *Robot) onUserLoaded() { // story r.RunStory() } + +type RobotFormatter struct { + DisableTimestamp bool + DisableQuote bool +} + +func (r *RobotFormatter) Format(entry *logrus.Entry) ([]byte, error) { + var b *bytes.Buffer + if entry.Buffer != nil { + b = entry.Buffer + } else { + b = &bytes.Buffer{} + } + + var timestamp string + var newLog string + if !r.DisableTimestamp { + timestamp = entry.Time.Format("2006-01-02 15:04:05") + newLog = fmt.Sprintf("[%s] %s\n", timestamp, entry.Message) + } else { + newLog = fmt.Sprintf("%s\n", entry.Message) + } + + b.WriteString(newLog) + return b.Bytes(), nil +} diff --git a/cmd/robot/user.go b/cmd/robot/user.go index 0d29fbd4b..fc3bbb260 100644 --- a/cmd/robot/user.go +++ b/cmd/robot/user.go @@ -17,7 +17,7 @@ var user_builders = []*TestCase{ NickName: "乐谷70616", }, rsp: &pb.UserCreateResp{}, - // enabled: true, + enabled: true, }, { desc: "添加资源", mainType: string(comm.ModuleUser), diff --git a/go.mod b/go.mod index dcf8ab70c..ccca2c3b3 100644 --- a/go.mod +++ b/go.mod @@ -15,12 +15,14 @@ require ( github.com/hashicorp/consul/api v1.12.0 github.com/json-iterator/go v1.1.12 github.com/mitchellh/hashstructure v1.1.0 + github.com/modern-go/reflect2 v1.0.2 github.com/nacos-group/nacos-sdk-go v1.0.8 github.com/natefinch/lumberjack v2.0.0+incompatible github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 github.com/robfig/cron/v3 v3.0.1 github.com/rs/xid v1.3.0 github.com/satori/go.uuid v1.2.0 + github.com/sirupsen/logrus v1.8.1 github.com/smallnest/rpcx v1.7.4 github.com/spf13/cast v1.3.1 github.com/spf13/cobra v1.2.1 @@ -82,6 +84,7 @@ require ( github.com/klauspost/compress v1.13.6 // indirect github.com/klauspost/cpuid/v2 v2.0.12 // indirect github.com/klauspost/reedsolomon v1.9.16 // indirect + github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f // indirect github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect @@ -95,7 +98,6 @@ require ( github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect github.com/nxadm/tail v1.4.8 // indirect github.com/onsi/ginkgo v1.16.5 // indirect github.com/philhofer/fwd v1.1.1 // indirect @@ -128,7 +130,7 @@ require ( golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 // indirect golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect - golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect + golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/tools v0.1.10 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect diff --git a/go.sum b/go.sum index 96d3f1cd2..0f9c3a6b7 100644 --- a/go.sum +++ b/go.sum @@ -407,6 +407,7 @@ github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuOb github.com/klauspost/reedsolomon v1.9.16 h1:mR0AwphBwqFv/I3B9AHtNKvzuowI1vrj8/3UX4XRmHA= github.com/klauspost/reedsolomon v1.9.16/go.mod h1:eqPAcE7xar5CIzcdfwydOEdcmchAKAP/qs14y4GCBOk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= @@ -610,7 +611,10 @@ github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5k github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/smallnest/quick v0.0.0-20220103065406-780def6371e6 h1:J8xk0QMMrqfDLqU0m07pYRRiOIlE7I3dNWAp/pAHqfo= github.com/smallnest/quick v0.0.0-20220103065406-780def6371e6/go.mod h1:h+J5yoLzf3XMKtM9l4vOaUtS4e+si6T3sKDtheJ15wc= github.com/smallnest/rpcx v1.7.4 h1:u6ADk/Ep8BqtAoJZO7LbniWsP+nqeAtcbaPm2D4eOXg= @@ -948,6 +952,8 @@ golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f h1:8w7RhxzTVgUzw/AH/9mUV5q0vMgy40SQRursCcfmkCw= golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew= +golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/modules/gateway/agent.go b/modules/gateway/agent.go index 07fad20ce..b4b28b990 100644 --- a/modules/gateway/agent.go +++ b/modules/gateway/agent.go @@ -237,7 +237,12 @@ func (this *Agent) messageDistribution(msg *pb.UserMessage) (err error) { return } if reply.Code != pb.ErrorCode_Success { - data, _ := anypb.New(&pb.NotifyErrorNotifyPush{ReqMainType: msg.MainType, ReqSubType: msg.SubType, Code: pb.ErrorCode(reply.Code.Number())}) + + data, _ := anypb.New(&pb.NotifyErrorNotifyPush{ + ReqMainType: msg.MainType, + ReqSubType: msg.SubType, + Arg: msg.Data, + Code: pb.ErrorCode(reply.Code.Number())}) err = this.WriteMsg(&pb.UserMessage{ MainType: comm.MainTypeNotify, SubType: comm.SubTypeErrorNotify, diff --git a/pb/notify_msg.pb.go b/pb/notify_msg.pb.go index 3faf8c211..ce6de2279 100644 --- a/pb/notify_msg.pb.go +++ b/pb/notify_msg.pb.go @@ -9,6 +9,7 @@ package pb import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" + anypb "google.golang.org/protobuf/types/known/anypb" reflect "reflect" sync "sync" ) @@ -26,11 +27,13 @@ type NotifyErrorNotifyPush struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - ReqMainType string `protobuf:"bytes,1,opt,name=ReqMainType,proto3" json:"ReqMainType"` // 请求协议模块 模块名 例如:user 对应项目中 user的模块 - ReqSubType string `protobuf:"bytes,2,opt,name=ReqSubType,proto3" json:"ReqSubType"` // 请求协议函数 例如:login 对应项目中 user的模块中 api_login 的处理函数 - Code ErrorCode `protobuf:"varint,3,opt,name=Code,proto3,enum=ErrorCode" json:"Code"` // 执行返回错误码 对应 errorcode.proto 枚举 - Message string `protobuf:"bytes,4,opt,name=Message,proto3" json:"Message"` // 错误消息 - Data string `protobuf:"bytes,5,opt,name=Data,proto3" json:"Data"` // 错误数据 + ReqMainType string `protobuf:"bytes,1,opt,name=ReqMainType,proto3" json:"ReqMainType"` // 请求协议模块 模块名 例如:user 对应项目中 user的模块 + ReqSubType string `protobuf:"bytes,2,opt,name=ReqSubType,proto3" json:"ReqSubType"` // 请求协议函数 例如:login 对应项目中 user的模块中 + // api_login 的处理函数 + Code ErrorCode `protobuf:"varint,3,opt,name=Code,proto3,enum=ErrorCode" json:"Code"` // 执行返回错误码 对应 errorcode.proto 枚举 + Message string `protobuf:"bytes,4,opt,name=Message,proto3" json:"Message"` // 错误消息 + Arg *anypb.Any `protobuf:"bytes,5,opt,name=arg,proto3" json:"arg"` //参数信息 + Data *anypb.Any `protobuf:"bytes,6,opt,name=Data,proto3" json:"Data"` // 错误数据 } func (x *NotifyErrorNotifyPush) Reset() { @@ -93,11 +96,18 @@ func (x *NotifyErrorNotifyPush) GetMessage() string { return "" } -func (x *NotifyErrorNotifyPush) GetData() string { +func (x *NotifyErrorNotifyPush) GetArg() *anypb.Any { + if x != nil { + return x.Arg + } + return nil +} + +func (x *NotifyErrorNotifyPush) GetData() *anypb.Any { if x != nil { return x.Data } - return "" + return nil } //获取系统公告 请求 @@ -202,26 +212,32 @@ var file_notify_notify_msg_proto_rawDesc = []byte{ 0x6d, 0x73, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x0f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x16, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x2f, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x5f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x22, 0xa7, 0x01, 0x0a, 0x15, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x45, 0x72, 0x72, - 0x6f, 0x72, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x50, 0x75, 0x73, 0x68, 0x12, 0x20, 0x0a, 0x0b, - 0x52, 0x65, 0x71, 0x4d, 0x61, 0x69, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x0b, 0x52, 0x65, 0x71, 0x4d, 0x61, 0x69, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x12, 0x1e, - 0x0a, 0x0a, 0x52, 0x65, 0x71, 0x53, 0x75, 0x62, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x0a, 0x52, 0x65, 0x71, 0x53, 0x75, 0x62, 0x54, 0x79, 0x70, 0x65, 0x12, 0x1e, - 0x0a, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0a, 0x2e, 0x45, - 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x18, - 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x44, 0x61, 0x74, 0x61, - 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x44, 0x61, 0x74, 0x61, 0x22, 0x12, 0x0a, 0x10, - 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x47, 0x65, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, - 0x22, 0x66, 0x0a, 0x11, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x47, 0x65, 0x74, 0x4c, 0x69, 0x73, - 0x74, 0x52, 0x65, 0x73, 0x70, 0x12, 0x22, 0x0a, 0x0c, 0x4c, 0x61, 0x73, 0x74, 0x52, 0x65, 0x61, - 0x64, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x4c, 0x61, 0x73, - 0x74, 0x52, 0x65, 0x61, 0x64, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x2d, 0x0a, 0x09, 0x53, 0x79, 0x73, - 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x44, - 0x42, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x52, 0x09, 0x53, - 0x79, 0x73, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x74, 0x6f, 0x1a, 0x19, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x75, 0x66, 0x2f, 0x61, 0x6e, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xe5, 0x01, + 0x0a, 0x15, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x4e, 0x6f, 0x74, + 0x69, 0x66, 0x79, 0x50, 0x75, 0x73, 0x68, 0x12, 0x20, 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x4d, 0x61, + 0x69, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x52, 0x65, + 0x71, 0x4d, 0x61, 0x69, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x52, 0x65, 0x71, + 0x53, 0x75, 0x62, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x52, + 0x65, 0x71, 0x53, 0x75, 0x62, 0x54, 0x79, 0x70, 0x65, 0x12, 0x1e, 0x0a, 0x04, 0x43, 0x6f, 0x64, + 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0a, 0x2e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, + 0x6f, 0x64, 0x65, 0x52, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x4d, 0x65, 0x73, + 0x73, 0x61, 0x67, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x4d, 0x65, 0x73, 0x73, + 0x61, 0x67, 0x65, 0x12, 0x26, 0x0a, 0x03, 0x61, 0x72, 0x67, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x14, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x41, 0x6e, 0x79, 0x52, 0x03, 0x61, 0x72, 0x67, 0x12, 0x28, 0x0a, 0x04, 0x44, + 0x61, 0x74, 0x61, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x67, 0x6f, 0x6f, 0x67, + 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x41, 0x6e, 0x79, 0x52, + 0x04, 0x44, 0x61, 0x74, 0x61, 0x22, 0x12, 0x0a, 0x10, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x47, + 0x65, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x22, 0x66, 0x0a, 0x11, 0x4e, 0x6f, 0x74, + 0x69, 0x66, 0x79, 0x47, 0x65, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x12, 0x22, + 0x0a, 0x0c, 0x4c, 0x61, 0x73, 0x74, 0x52, 0x65, 0x61, 0x64, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x4c, 0x61, 0x73, 0x74, 0x52, 0x65, 0x61, 0x64, 0x54, 0x69, + 0x6d, 0x65, 0x12, 0x2d, 0x0a, 0x09, 0x53, 0x79, 0x73, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x18, + 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x44, 0x42, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, + 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x52, 0x09, 0x53, 0x79, 0x73, 0x4e, 0x6f, 0x74, 0x69, 0x66, + 0x79, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, } var ( @@ -242,16 +258,19 @@ var file_notify_notify_msg_proto_goTypes = []interface{}{ (*NotifyGetListReq)(nil), // 1: NotifyGetListReq (*NotifyGetListResp)(nil), // 2: NotifyGetListResp (ErrorCode)(0), // 3: ErrorCode - (*DBSystemNotify)(nil), // 4: DBSystemNotify + (*anypb.Any)(nil), // 4: google.protobuf.Any + (*DBSystemNotify)(nil), // 5: DBSystemNotify } var file_notify_notify_msg_proto_depIdxs = []int32{ 3, // 0: NotifyErrorNotifyPush.Code:type_name -> ErrorCode - 4, // 1: NotifyGetListResp.SysNotify:type_name -> DBSystemNotify - 2, // [2:2] is the sub-list for method output_type - 2, // [2:2] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name - 2, // [2:2] is the sub-list for extension extendee - 0, // [0:2] is the sub-list for field type_name + 4, // 1: NotifyErrorNotifyPush.arg:type_name -> google.protobuf.Any + 4, // 2: NotifyErrorNotifyPush.Data:type_name -> google.protobuf.Any + 5, // 3: NotifyGetListResp.SysNotify:type_name -> DBSystemNotify + 4, // [4:4] is the sub-list for method output_type + 4, // [4:4] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name } func init() { file_notify_notify_msg_proto_init() } diff --git a/pb/proto/notify/notify_msg.proto b/pb/proto/notify/notify_msg.proto index 1008862eb..48a5572dd 100644 --- a/pb/proto/notify/notify_msg.proto +++ b/pb/proto/notify/notify_msg.proto @@ -2,22 +2,24 @@ syntax = "proto3"; option go_package = ".;pb"; import "errorcode.proto"; import "notify/notify_db.proto"; +import "google/protobuf/any.proto"; //统一错误码推送 message NotifyErrorNotifyPush { - string ReqMainType =1; // 请求协议模块 模块名 例如:user 对应项目中 user的模块 - string ReqSubType = 2; // 请求协议函数 例如:login 对应项目中 user的模块中 api_login 的处理函数 - ErrorCode Code = 3; // 执行返回错误码 对应 errorcode.proto 枚举 - string Message = 4; // 错误消息 - string Data = 5; // 错误数据 + string ReqMainType = 1; // 请求协议模块 模块名 例如:user 对应项目中 user的模块 + string ReqSubType = 2; // 请求协议函数 例如:login 对应项目中 user的模块中 + // api_login 的处理函数 + ErrorCode Code = 3; // 执行返回错误码 对应 errorcode.proto 枚举 + string Message = 4; // 错误消息 + google.protobuf.Any arg = 5; //参数信息 + google.protobuf.Any Data = 6; // 错误数据 } //获取系统公告 请求 -message NotifyGetListReq { -} +message NotifyGetListReq {} //获取系统公告 回应 message NotifyGetListResp { - int64 LastReadTime = 1; //最后一次阅读时间 - repeated DBSystemNotify SysNotify = 2; //公告列表 + int64 LastReadTime = 1; //最后一次阅读时间 + repeated DBSystemNotify SysNotify = 2; //公告列表 } \ No newline at end of file