正在显示
12 个修改的文件
包含
535 行增加
和
127 行删除
Dockerfile
0 → 100644
1 | +FROM golang:1.13 as builder | ||
2 | + | ||
3 | +ENV GOPROXY https://goproxy.cn | ||
4 | +ENV GO111MODULE on | ||
5 | + | ||
6 | +RUN git clone http://gitlab.fjmaimaimai.com/mmm-go/gocomm.git /app/gocomm | ||
7 | + | ||
8 | +WORKDIR /app/ab | ||
9 | + | ||
10 | +COPY go.mod . | ||
11 | +COPY go.sum . | ||
12 | +RUN go mod download | ||
13 | + | ||
14 | +COPY . . | ||
15 | + | ||
16 | +RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -installsuffix cgo -o ab main.go | ||
17 | + | ||
18 | +#FROM scratch | ||
19 | +FROM alpine:latest | ||
20 | +RUN apk add --no-cache tzdata \ | ||
21 | + && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ | ||
22 | + && echo "Asia/Shanghai" > /etc/timezone \ | ||
23 | + && rm -rf /var/cache/apk/* /tmp/* /var/tmp/* $HOME/.cache ## 清除缓存 | ||
24 | +WORKDIR /root/ | ||
25 | +COPY --from=builder /app/ab . | ||
26 | +#RUN ls -l | ||
27 | +EXPOSE 8080 | ||
28 | +CMD ["./ab"] |
@@ -2,10 +2,13 @@ appname = ab | @@ -2,10 +2,13 @@ appname = ab | ||
2 | httpport = 8080 | 2 | httpport = 8080 |
3 | runmode = dev | 3 | runmode = dev |
4 | 4 | ||
5 | +#没设置的话 this.Ctx.Input.RequestBody 没有值 | ||
6 | +copyrequestbody = true | ||
7 | + | ||
5 | #redis相关配置 | 8 | #redis相关配置 |
6 | redis_add_port = "127.0.0.1:6379" | 9 | redis_add_port = "127.0.0.1:6379" |
7 | redis_auth = "123456" | 10 | redis_auth = "123456" |
8 | 11 | ||
9 | #远程 | 12 | #远程 |
10 | -AHost = "127.0.0.1:8081" | ||
11 | -BHost = "127.0.0.1:8082" | 13 | +AHost = "http://127.0.0.1:8081" |
14 | +BHost = "http://127.0.0.1:8082" |
1 | package controllers | 1 | package controllers |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "encoding/hex" | ||
5 | - "encoding/json" | ||
6 | - "fmt" | ||
7 | "github.com/astaxie/beego" | 4 | "github.com/astaxie/beego" |
5 | + "gitlab.fjmaimaimai.com/mmm-go/ab/protocol" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/ab/services/ab" | ||
8 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | 7 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" |
9 | - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
10 | - "github.com/astaxie/beego/httplib" | ||
11 | - "io/ioutil" | ||
12 | - "net/http" | ||
13 | - "path" | ||
14 | - "reflect" | ||
15 | - "strings" | ||
16 | ) | 8 | ) |
17 | 9 | ||
18 | type ABController struct { | 10 | type ABController struct { |
@@ -24,7 +16,8 @@ var bHost string | @@ -24,7 +16,8 @@ var bHost string | ||
24 | 16 | ||
25 | const ( | 17 | const ( |
26 | QueryResponseError = 1 //请求应答错误 | 18 | QueryResponseError = 1 //请求应答错误 |
27 | - CompareResultError=2 //应答对比错误 | 19 | + CompareResultSuccess=2 //应答对比成功 |
20 | + CompareResultError=3 //应答对比错误 | ||
28 | ) | 21 | ) |
29 | 22 | ||
30 | func init(){ | 23 | func init(){ |
@@ -34,124 +27,20 @@ func init(){ | @@ -34,124 +27,20 @@ func init(){ | ||
34 | 27 | ||
35 | //Compare | 28 | //Compare |
36 | func(this *ABController)Compare(){ | 29 | func(this *ABController)Compare(){ |
37 | - var ( | ||
38 | - msg *mybeego.Message | ||
39 | - status string = "OK" | ||
40 | - ) | 30 | + var msg *mybeego.Message |
41 | defer func(){ | 31 | defer func(){ |
42 | this.Resp(msg) | 32 | this.Resp(msg) |
43 | }() | 33 | }() |
44 | - method := this.Ctx.Request.Method | ||
45 | - head :=this.Ctx.Request.Header | ||
46 | - body :=this.ByteBody | ||
47 | - newRequest:= NewRequest(bHost,method,head,body) | ||
48 | - aResp,err :=makeHttpRequest(aHost,method,head,body) | ||
49 | - if err!=nil{ | ||
50 | - status ="FAIL" | 34 | + request :=&protocol.CompareRequest{ |
35 | + Method:this.Ctx.Request.Method, | ||
36 | + RequestUri:this.Ctx.Request.RequestURI, | ||
37 | + Head:this.Ctx.Request.Header, | ||
38 | + Body:this.ByteBody, | ||
39 | + IsRetry:false, | ||
51 | } | 40 | } |
52 | - log.Debug(fmt.Sprintf("[%v]- Method:%v \n Head:\n%v \nBody:\n%v \n Response:\n%v",status,method,head,body,aResp)) | ||
53 | - this.Data["json"] = aResp | ||
54 | - | ||
55 | - Compare :=func(){ | ||
56 | - defer func(){ | ||
57 | - if p:=recover();p!=nil{ | ||
58 | - log.Error(p) | ||
59 | - } | ||
60 | - }() | ||
61 | - //判断已经在错误列表里面就不再判断请求 | ||
62 | - bResp,err :=makeHttpRequest(bHost,method,head,body) | ||
63 | - if err!=nil{ | ||
64 | - log.Error(err) | ||
65 | - errorHandler(QueryResponseError,newRequest) | ||
66 | - return | ||
67 | - } | ||
68 | - cResult,err :=compareResponse(aResp,bResp) | ||
69 | - if err!=nil{ | ||
70 | - log.Error(err) | ||
71 | - errorHandler(QueryResponseError,newRequest) | ||
72 | - return | ||
73 | - } | ||
74 | - if !cResult{ | ||
75 | - errorHandler(CompareResultError,newRequest) | ||
76 | - log.Warn(fmt.Sprintf("" + | ||
77 | - "[Companre Error] - ErrorType:%v " + | ||
78 | - "\nMethod:\n %v " + | ||
79 | - "\nHead:\n %v " + | ||
80 | - "\nAResponse:\n %v " + | ||
81 | - "\nBResponse:\n %v",CompareResultError,method,head,string(aResp),string(bResp))) | ||
82 | - }else{ | ||
83 | - //记录到记录行里面 | ||
84 | - log.Info("[Success] \nMethod:\n %v " + | ||
85 | - "\nHead:\n %v " + | ||
86 | - "\nBody:\n%v",method,head,string(body)) | ||
87 | - } | ||
88 | - } | ||
89 | - | ||
90 | - go Compare() | ||
91 | - this.ServeJSON() | 41 | + msg = this.GenMessage(ab.Compare(request)) |
92 | } | 42 | } |
93 | -func compareResponse(aResp,bResp []byte)(result bool,err error){ | ||
94 | - defer func(){ | ||
95 | - if p:=recover();p!=nil{ | ||
96 | - log.Error(p) | ||
97 | - } | ||
98 | - }() | ||
99 | - var mapA = make(map[string]interface{}) | ||
100 | - var mapB = make(map[string]interface{}) | ||
101 | 43 | ||
102 | - if err =json.Unmarshal(aResp,&mapA);err!=nil{ | ||
103 | - result = false | ||
104 | - return | ||
105 | - } | ||
106 | - if err =json.Unmarshal(bResp,&mapB);err!=nil{ | ||
107 | - result = false | ||
108 | - return | ||
109 | - } | ||
110 | - result = reflect.DeepEqual(mapA,mapB) | ||
111 | - return | ||
112 | -} | ||
113 | -func errorHandler(errType int,req Request){ | ||
114 | - //写redis error | ||
115 | -} | ||
116 | -func makeHttpRequest(host,method string,head map[string][]string,body []byte)(data []byte,err error){ | ||
117 | - var ( | ||
118 | - resp *http.Response | ||
119 | - ) | ||
120 | - rawUrl := path.Join(host,method) | ||
121 | - req := httplib.NewBeegoRequest(rawUrl,"Post") | ||
122 | - for k,v:=range head{ | ||
123 | - req.Header(k,strings.Join(v,",")) | ||
124 | - } | ||
125 | - req.Body(body) | ||
126 | - resp,err =req.DoRequest() | ||
127 | - if err!=nil{ | ||
128 | - return | ||
129 | - } | ||
130 | - data,err = ioutil.ReadAll(resp.Body) | ||
131 | - defer resp.Body.Close() | ||
132 | - return | ||
133 | -} | ||
134 | 44 | ||
135 | 45 | ||
136 | -type Request struct { | ||
137 | - Host string | ||
138 | - Method string | ||
139 | - Head map[string][]string | ||
140 | - Body string //hex | ||
141 | -} | ||
142 | -func NewRequest(host,method string,head map[string][]string,body []byte)Request{ | ||
143 | - return Request{ | ||
144 | - Host:host, | ||
145 | - Method:method, | ||
146 | - Head:head, | ||
147 | - Body:hex.EncodeToString(body), | ||
148 | - } | ||
149 | -} | ||
150 | -func(req Request)GetBody()([]byte){ | ||
151 | - data,err:= hex.DecodeString(req.Body) | ||
152 | - if err!=nil{ | ||
153 | - log.Error(err) | ||
154 | - return []byte{} | ||
155 | - } | ||
156 | - return data | ||
157 | -} | ||
46 | + |
@@ -46,4 +46,4 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message | @@ -46,4 +46,4 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message | ||
46 | } | 46 | } |
47 | msg = mybeego.NewMessage(1) | 47 | msg = mybeego.NewMessage(1) |
48 | return msg | 48 | return msg |
49 | -} | ||
49 | +} |
controllers/history.go
0 → 100644
1 | +package controllers | ||
2 | + | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/ab/protocol" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/ab/services/history" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
9 | +) | ||
10 | + | ||
11 | +type HistoryController struct { | ||
12 | + BaseController | ||
13 | +} | ||
14 | + | ||
15 | +//SuccessList | ||
16 | +func(this *HistoryController)SuccessList(){ | ||
17 | + var msg *mybeego.Message | ||
18 | + defer func(){ | ||
19 | + this.Resp(msg) | ||
20 | + }() | ||
21 | + var request *protocol.SuccessListRequest | ||
22 | + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | ||
23 | + log.Error(err) | ||
24 | + msg = mybeego.NewMessage(1) | ||
25 | + return | ||
26 | + } | ||
27 | + if b,m :=this.Valid(request);!b{ | ||
28 | + msg = m | ||
29 | + return | ||
30 | + } | ||
31 | + msg = this.GenMessage(history.SuccessList(request)) | ||
32 | +} | ||
33 | + | ||
34 | +//GetRequest | ||
35 | +func(this *HistoryController)GetRequest(){ | ||
36 | + var msg *mybeego.Message | ||
37 | + defer func(){ | ||
38 | + this.Resp(msg) | ||
39 | + }() | ||
40 | + var request *protocol.GetRequestRequest | ||
41 | + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | ||
42 | + log.Error(err) | ||
43 | + msg = mybeego.NewMessage(1) | ||
44 | + return | ||
45 | + } | ||
46 | + if b,m :=this.Valid(request);!b{ | ||
47 | + msg = m | ||
48 | + return | ||
49 | + } | ||
50 | + msg = this.GenMessage(history.GetRequest(request)) | ||
51 | +} | ||
52 | + | ||
53 | +//Retry | ||
54 | +func(this *HistoryController)Retry(){ | ||
55 | + var msg *mybeego.Message | ||
56 | + defer func(){ | ||
57 | + this.Resp(msg) | ||
58 | + }() | ||
59 | + var request *protocol.RetryRequest | ||
60 | + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | ||
61 | + log.Error(err) | ||
62 | + msg = mybeego.NewMessage(1) | ||
63 | + return | ||
64 | + } | ||
65 | + if b,m :=this.Valid(request);!b{ | ||
66 | + msg = m | ||
67 | + return | ||
68 | + } | ||
69 | + msg = this.GenMessage(history.Retry(request)) | ||
70 | +} | ||
71 | + | ||
72 | +//DeleteHistory | ||
73 | +func(this *HistoryController)DeleteHistory(){ | ||
74 | + var msg *mybeego.Message | ||
75 | + defer func(){ | ||
76 | + this.Resp(msg) | ||
77 | + }() | ||
78 | + var request *protocol.DeleteHistoryRequest | ||
79 | + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | ||
80 | + log.Error(err) | ||
81 | + msg = mybeego.NewMessage(1) | ||
82 | + return | ||
83 | + } | ||
84 | + if b,m :=this.Valid(request);!b{ | ||
85 | + msg = m | ||
86 | + return | ||
87 | + } | ||
88 | + msg = this.GenMessage(history.DeleteHistory(request)) | ||
89 | +} |
protocol/ab.go
0 → 100644
1 | +package protocol | ||
2 | + | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
6 | +) | ||
7 | + | ||
8 | +/*Compare */ | ||
9 | +type CompareRequest struct { | ||
10 | + Method string | ||
11 | + RequestUri string | ||
12 | + Head map[string][]string | ||
13 | + Body []byte | ||
14 | + IsRetry bool | ||
15 | +} | ||
16 | +type CompareResponse struct { | ||
17 | +} | ||
18 | + | ||
19 | +type Request struct { | ||
20 | + Host string `json:"-"` | ||
21 | + Method string `json:"method"` | ||
22 | + RequestUri string `json:"request_uri"` | ||
23 | + Head map[string][]string `json:"head"` | ||
24 | + Body interface{} `json:"body"` | ||
25 | + AResp interface{} `json:"a_resp"` | ||
26 | + BResp interface{} `json:"b_resp"` | ||
27 | +} | ||
28 | + | ||
29 | +func NewRequest(host,method,requestUri string,head map[string][]string,body []byte)Request{ | ||
30 | + var mapBody = make(map[string]interface{}) | ||
31 | + json.Unmarshal(body,&mapBody) | ||
32 | + return Request{ | ||
33 | + //Host:host, | ||
34 | + Method:method, | ||
35 | + RequestUri:requestUri, | ||
36 | + Head:head, | ||
37 | + Body:mapBody, | ||
38 | + } | ||
39 | +} | ||
40 | +func(req Request)GetBody()([]byte){ | ||
41 | + data,err:= json.Marshal(req.Body) | ||
42 | + if err!=nil{ | ||
43 | + log.Error(err) | ||
44 | + return []byte{} | ||
45 | + } | ||
46 | + return data | ||
47 | +} |
protocol/global.go
0 → 100644
1 | +package protocol | ||
2 | + | ||
3 | +import ( | ||
4 | + "sort" | ||
5 | + "sync" | ||
6 | +) | ||
7 | + | ||
8 | +var( | ||
9 | + SuccessRequestMap *sync.Map | ||
10 | + ErrorRequestMap *sync.Map | ||
11 | +) | ||
12 | + | ||
13 | +const( | ||
14 | + StatusOK ="OK" | ||
15 | + StatusFial ="Fail" | ||
16 | +) | ||
17 | + | ||
18 | +func init(){ | ||
19 | + SuccessRequestMap =new(sync.Map) //&sync.Map{} | ||
20 | + ErrorRequestMap = new(sync.Map) | ||
21 | + //SuccessRequestMap.Store("/auth/login",&Request{Host:"1237.0.0.1"}) | ||
22 | +} | ||
23 | + | ||
24 | +func Exists(status string,key string)bool{ | ||
25 | + m :=GetMap(status) | ||
26 | + if _,ok:=m.Load(key);ok{ | ||
27 | + return true | ||
28 | + } | ||
29 | + return false | ||
30 | +} | ||
31 | + | ||
32 | +func SetRequest(status string,key string,req *Request){ | ||
33 | + m :=GetMap(status) | ||
34 | + if _,ok:=m.Load(key);ok{ | ||
35 | + return | ||
36 | + } | ||
37 | + m.Store(key,req) | ||
38 | +} | ||
39 | + | ||
40 | +func GetRequest(status string,key string)(*Request){ | ||
41 | + m :=GetMap(status) | ||
42 | + if v,ok:=m.Load(key);ok{ | ||
43 | + return v.(*Request) | ||
44 | + } | ||
45 | + return nil | ||
46 | +} | ||
47 | + | ||
48 | +func GetRequests(status string)([]*Request){ | ||
49 | + m :=GetMap(status) | ||
50 | + var listKey []string | ||
51 | + var list []*Request | ||
52 | + m.Range(func(key,value interface{})bool{ | ||
53 | + listKey = append(listKey,key.(string)) | ||
54 | + return true | ||
55 | + }) | ||
56 | + sort.Strings(listKey) | ||
57 | + for i:=range listKey{ | ||
58 | + r :=GetRequest(status,listKey[i]) | ||
59 | + if r!=nil{ | ||
60 | + list = append(list,r) | ||
61 | + } | ||
62 | + } | ||
63 | + return list | ||
64 | +} | ||
65 | + | ||
66 | +func DeleteRequest(status string,key string){ | ||
67 | + m :=GetMap(status) | ||
68 | + m.Delete(key) | ||
69 | +} | ||
70 | + | ||
71 | +func DeleteAll(status string){ | ||
72 | + m :=GetMap(status) | ||
73 | + m.Range(func(key,value interface{})bool{ | ||
74 | + m.Delete(key) | ||
75 | + return true | ||
76 | + }) | ||
77 | +} | ||
78 | + | ||
79 | +func GetMap(status string) *sync.Map{ | ||
80 | + if status==StatusOK{ | ||
81 | + return SuccessRequestMap | ||
82 | + } | ||
83 | + return ErrorRequestMap | ||
84 | +} |
protocol/history.go
0 → 100644
1 | +package protocol | ||
2 | + | ||
3 | +/*SuccessList */ | ||
4 | +type SuccessListRequest struct { | ||
5 | + Status int `json:"status"` // 1 succ 0 fail | ||
6 | +} | ||
7 | +type SuccessListResponse struct { | ||
8 | + List []*Request `json:"list"` | ||
9 | +} | ||
10 | + | ||
11 | +/*GetRequest */ | ||
12 | +type GetRequestRequest struct { | ||
13 | + Status int `json:"status" valid:"Required"` | ||
14 | + Uri string `json:"uri" valid:"Required"` | ||
15 | +} | ||
16 | +type GetRequestResponse struct { | ||
17 | + *Request | ||
18 | +} | ||
19 | + | ||
20 | + | ||
21 | +/*Retry */ | ||
22 | +type RetryRequest struct { | ||
23 | + Status int `json:"status" valid:"Required"` | ||
24 | + Uri string `json:"uri" valid:"Required"` | ||
25 | +} | ||
26 | +type RetryResponse struct { | ||
27 | + ResultEqual bool `json:"result_equal"` | ||
28 | +} | ||
29 | + | ||
30 | +/*DeleteHistory */ | ||
31 | +type DeleteHistoryRequest struct { | ||
32 | + Status int `json:"status" valid:"Required"` | ||
33 | + Uri string `json:"uri" valid:"Required"` | ||
34 | +} | ||
35 | +type DeleteHistoryResponse struct { | ||
36 | +} |
@@ -8,4 +8,12 @@ import ( | @@ -8,4 +8,12 @@ import ( | ||
8 | func init() { | 8 | func init() { |
9 | //beego.Router("/", &controllers.MainController{}) | 9 | //beego.Router("/", &controllers.MainController{}) |
10 | beego.Router("/*", &controllers.ABController{},"Post:Compare") | 10 | beego.Router("/*", &controllers.ABController{},"Post:Compare") |
11 | + | ||
12 | + { | ||
13 | + history :=&controllers.HistoryController{} | ||
14 | + beego.Router("/history/successList",history,"post:SuccessList") | ||
15 | + beego.Router("/history/getRequest",history,"post:GetRequest") | ||
16 | + beego.Router("/history/retry",history,"post:Retry") | ||
17 | + beego.Router("/history/deleteHistory",history,"post:DeleteHistory") | ||
18 | + } | ||
11 | } | 19 | } |
services/ab/ab.go
0 → 100644
1 | +package ab | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "github.com/astaxie/beego/httplib" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/ab/protocol" | ||
8 | + "github.com/astaxie/beego" | ||
9 | + "io/ioutil" | ||
10 | + "net/http" | ||
11 | + "reflect" | ||
12 | + "strings" | ||
13 | + "encoding/json" | ||
14 | +) | ||
15 | + | ||
16 | +var aHost string | ||
17 | +var bHost string | ||
18 | + | ||
19 | +const ( | ||
20 | + QueryResponseError = 1 //请求应答错误 | ||
21 | + CompareResultSuccess=2 //应答对比成功 | ||
22 | + CompareResultError=3 //应答对比错误 | ||
23 | +) | ||
24 | + | ||
25 | +func init(){ | ||
26 | + aHost =beego.AppConfig.String("AHost") | ||
27 | + bHost =beego.AppConfig.String("BHost") | ||
28 | +} | ||
29 | + | ||
30 | +func Compare(request *protocol.CompareRequest)(rsp interface{},err error){ | ||
31 | + var ( | ||
32 | + status string = protocol.StatusOK | ||
33 | + key string | ||
34 | + ) | ||
35 | + | ||
36 | + method := request.Method | ||
37 | + requestURI :=request.RequestUri | ||
38 | + head :=request.Head | ||
39 | + body :=request.Body | ||
40 | + newRequest:= protocol.NewRequest(bHost,method,requestURI,head,body) | ||
41 | + aResp,err :=makeHttpRequest(aHost,method,requestURI,head,body) | ||
42 | + | ||
43 | + key =requestURI | ||
44 | + if err!=nil{ | ||
45 | + log.Error(err) | ||
46 | + status =protocol.StatusFial | ||
47 | + } | ||
48 | + var mapA = make(map[string]interface{}) | ||
49 | + if err =json.Unmarshal(aResp,&mapA);err!=nil{ | ||
50 | + return | ||
51 | + } | ||
52 | + //log.Debug(fmt.Sprintf("[%v]- URI:%v \n Head:\n%v \nBody:\n%v \n Response:\n%v",status,requestURI,head,string(body),string(aResp))) | ||
53 | + Compare :=func(){ | ||
54 | + defer func(){ | ||
55 | + if p:=recover();p!=nil{ | ||
56 | + log.Error(p) | ||
57 | + } | ||
58 | + }() | ||
59 | + //判断已经在错误列表里面就不再判断请求 | ||
60 | + bResp,err :=makeHttpRequest(bHost,method,requestURI,head,body) | ||
61 | + if err!=nil{ | ||
62 | + log.Error(err) | ||
63 | + errorHandler(QueryResponseError,&newRequest) | ||
64 | + return | ||
65 | + } | ||
66 | + cResult,err :=compareResponse(aResp,bResp,&newRequest) | ||
67 | + if err!=nil{ | ||
68 | + log.Error(err) | ||
69 | + errorHandler(QueryResponseError,&newRequest) | ||
70 | + return | ||
71 | + } | ||
72 | + if !cResult{ | ||
73 | + errorHandler(CompareResultError,&newRequest) | ||
74 | + log.Warn(fmt.Sprintf("" + | ||
75 | + "[Compare Error] - ErrorType:%v " + | ||
76 | + "Method: %v " + | ||
77 | + "\nHead: %v " + | ||
78 | + "\nAResponse: %v " + | ||
79 | + "\nBResponse: %v",CompareResultError,requestURI,head,string(aResp),string(bResp))) | ||
80 | + }else{ | ||
81 | + //记录到记录行里面 | ||
82 | + errorHandler(CompareResultSuccess,&newRequest) | ||
83 | + log.Info(fmt.Sprintf("[Compare Success] Method:%v " + | ||
84 | + "\nHead:%v " + | ||
85 | + "\nBody:%v",requestURI,head,string(body))) | ||
86 | + } | ||
87 | + } | ||
88 | + if status==protocol.StatusOK && (!protocol.Exists(protocol.StatusOK,key) || request.IsRetry){ | ||
89 | + if !protocol.Exists(protocol.StatusFial,key){ | ||
90 | + go Compare() //已经在错误列表 | ||
91 | + } | ||
92 | + } | ||
93 | + rsp =mapA | ||
94 | + return | ||
95 | +} | ||
96 | +func compareResponse(aResp,bResp []byte,req *protocol.Request)(result bool,err error){ | ||
97 | + defer func(){ | ||
98 | + if p:=recover();p!=nil{ | ||
99 | + log.Error(p) | ||
100 | + } | ||
101 | + }() | ||
102 | + var mapA = make(map[string]interface{}) | ||
103 | + var mapB = make(map[string]interface{}) | ||
104 | + | ||
105 | + if err =json.Unmarshal(aResp,&mapA);err!=nil{ | ||
106 | + result = false | ||
107 | + return | ||
108 | + } | ||
109 | + if err =json.Unmarshal(bResp,&mapB);err!=nil{ | ||
110 | + result = false | ||
111 | + return | ||
112 | + } | ||
113 | + req.AResp = mapA | ||
114 | + req.BResp = mapB | ||
115 | + result = reflect.DeepEqual(mapA,mapB) | ||
116 | + return | ||
117 | +} | ||
118 | +func errorHandler(errType int,req *protocol.Request){ | ||
119 | + if errType==QueryResponseError{ | ||
120 | + protocol.SetRequest(protocol.StatusFial,req.RequestUri,req) | ||
121 | + return | ||
122 | + } | ||
123 | + if errType==CompareResultSuccess{ | ||
124 | + protocol.SetRequest(protocol.StatusOK,req.RequestUri,req) | ||
125 | + protocol.DeleteRequest(protocol.StatusFial,req.RequestUri) | ||
126 | + return | ||
127 | + } | ||
128 | + if errType==CompareResultError{ | ||
129 | + protocol.SetRequest(protocol.StatusFial,req.RequestUri,req) | ||
130 | + return | ||
131 | + } | ||
132 | +} | ||
133 | +func makeHttpRequest(host,method,requestUri string,head map[string][]string,body []byte)(data []byte,err error){ | ||
134 | + var ( | ||
135 | + resp *http.Response | ||
136 | + ) | ||
137 | + rawUrl := host+requestUri | ||
138 | + req := httplib.Post(rawUrl) | ||
139 | + for k,v:=range head{ | ||
140 | + req.Header(k,strings.Join(v,",")) | ||
141 | + } | ||
142 | + req.Body(body) | ||
143 | + resp,err =req.Response() | ||
144 | + if err!=nil{ | ||
145 | + return | ||
146 | + } | ||
147 | + data,err = ioutil.ReadAll(resp.Body) | ||
148 | + defer resp.Body.Close() | ||
149 | + return | ||
150 | +} |
services/history/history.go
0 → 100644
1 | +package history | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/prometheus/common/log" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/ab/protocol" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/ab/services/ab" | ||
7 | +) | ||
8 | + | ||
9 | +func SuccessList(request *protocol.SuccessListRequest)(rsp *protocol.SuccessListResponse,err error){ | ||
10 | + var ( | ||
11 | + | ||
12 | + ) | ||
13 | + rsp =&protocol.SuccessListResponse{ | ||
14 | + List:protocol.GetRequests(getStatus(request.Status)), | ||
15 | + } | ||
16 | + return | ||
17 | +} | ||
18 | + | ||
19 | +func getStatus(statu int)string{ | ||
20 | + status :=protocol.StatusOK | ||
21 | + if statu==0{ | ||
22 | + status = protocol.StatusFial | ||
23 | + } | ||
24 | + return status | ||
25 | +} | ||
26 | + | ||
27 | +func GetRequest(request *protocol.GetRequestRequest)(rsp *protocol.GetRequestResponse,err error){ | ||
28 | + var ( | ||
29 | + | ||
30 | + ) | ||
31 | + rsp =&protocol.GetRequestResponse{ | ||
32 | + protocol.GetRequest(getStatus(request.Status),request.Uri), | ||
33 | + } | ||
34 | + return | ||
35 | +} | ||
36 | + | ||
37 | +func Retry(request *protocol.RetryRequest)(rsp *protocol.RetryResponse,err error){ | ||
38 | + var ( | ||
39 | + | ||
40 | + ) | ||
41 | + historyRequest := protocol.GetRequest(getStatus(request.Status),request.Uri) | ||
42 | + if historyRequest==nil{ | ||
43 | + log.Warn("request not found , Uri:",request.Uri) | ||
44 | + return | ||
45 | + } | ||
46 | + compareRequest :=&protocol.CompareRequest{ | ||
47 | + Method:historyRequest.Method, | ||
48 | + RequestUri:historyRequest.RequestUri, | ||
49 | + Head:historyRequest.Head, | ||
50 | + Body: historyRequest.GetBody(), | ||
51 | + IsRetry:true, | ||
52 | + } | ||
53 | + _,err = ab.Compare(compareRequest) | ||
54 | + if err!=nil{ | ||
55 | + log.Error(err) | ||
56 | + return | ||
57 | + } | ||
58 | + newRequest := protocol.GetRequest(getStatus(1),request.Uri) | ||
59 | + rsp =&protocol.RetryResponse{ResultEqual:false} | ||
60 | + if newRequest!=nil{//获取到成功的请求用力 | ||
61 | + rsp =&protocol.RetryResponse{ResultEqual:true} | ||
62 | + } | ||
63 | + return | ||
64 | +} | ||
65 | + | ||
66 | +func DeleteHistory(request *protocol.DeleteHistoryRequest)(rsp *protocol.DeleteHistoryResponse,err error){ | ||
67 | + var ( | ||
68 | + | ||
69 | + ) | ||
70 | + protocol.DeleteRequest(getStatus(request.Status),request.Uri) | ||
71 | + rsp =&protocol.DeleteHistoryResponse{} | ||
72 | + return | ||
73 | +} |
-
请 注册 或 登录 后发表评论