/
request.go
116 lines (93 loc) · 2.32 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package core
import (
"errors"
"fmt"
"net/http"
"time"
)
// 请求接口
type Request interface {
//返回*http.Request
HttpRequest() (*http.Request, error)
// 返回请求服务名
// 以便归类不同服务的统计数据
// 如果使用断路器,则必须根据不同服务端返回不同名字。
// 以 主机+端口 是一个不错的选择
ServerName() string
//
Clone() interface{}
String() string
// 尝试次数
setReqCount(reqCount int)
ReqCount() (int)
// 钩子存放获取数据
SetHookData(key string, data interface{}) (ok bool)
HookData(key string) (data interface{}, ok bool)
// 请求响应实体处理
setResponse(resp *Response)
Response() (*Response)
// 请求响应时间处理
setReqLongTime(long time.Duration)
ReqLongTime() time.Duration
}
// 请求基类
// 实现请求接口的基类,所有请求对象继承必须继承此基类
type BaseRequest struct {
reqCount int
reqLongTime time.Duration
Resp *Response
// 钩子存放数据Map
hookData map[string]interface{}
}
//返回*http.Request
func (b *BaseRequest) HttpRequest() (*http.Request, error) {
return nil, errors.New("implement Interface's Method::HttpRequest")
}
func (b *BaseRequest) ServerName() string {
return "localhost"
}
func (b *BaseRequest) String() string {
return fmt.Sprintf("\n ReqCount:%d \n", b.reqCount)
}
func (b *BaseRequest) TimeOut() time.Duration {
return -1
}
func (b *BaseRequest) Clone() interface{} {
new_obj := *b
return &new_obj
}
func (b *BaseRequest) setReqCount(reqCount int) {
b.reqCount = reqCount
}
func (b *BaseRequest) ReqCount() int {
return b.reqCount
}
// 设置请求处理时间
func (b *BaseRequest) setReqLongTime(long time.Duration) {
b.reqLongTime = long
}
func (b *BaseRequest) ReqLongTime() time.Duration {
return b.reqLongTime
}
// 设置请求返回信息结构
func (b *BaseRequest) setResponse(resp *Response) {
b.Resp = resp
}
func (b *BaseRequest) Response() *Response {
return b.Resp
}
func (b *BaseRequest) SetHookData(key string, data interface{}) (ok bool) {
if nil == b.hookData {
b.hookData = make(map[string]interface{})
}
b.hookData[key] = data
_, ok = b.hookData[key]
return
}
func (b *BaseRequest) HookData(key string) (data interface{}, ok bool) {
if nil == b.hookData {
return nil, false
}
data, ok = b.hookData[key]
return
}