-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstance.go
109 lines (92 loc) · 1.93 KB
/
instance.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
package xmux
import (
"net/http"
"sync"
)
// instance 数据二次封装, 用户各模块之间的数据传递
type FlowData struct {
Data interface{} // 处理后的数据
ctx map[string]interface{} // 用来传递自定义值
mu *sync.RWMutex
Response interface{} // 返回的数据结构
connectId int64
funcName string
pages map[string]struct{}
StatusCode int
Body []byte
CacheKey string
}
type conns struct {
conn map[*http.Request]*FlowData
mu *sync.RWMutex
}
var allconn *conns
// var dataLock *sync.RWMutex
func init() {
allconn = &conns{
conn: make(map[*http.Request]*FlowData),
mu: &sync.RWMutex{},
}
}
func (conns *conns) Set(r *http.Request, fd *FlowData) {
conns.mu.Lock()
defer conns.mu.Unlock()
conns.conn[r] = fd
}
func (conns *conns) Del(r *http.Request) {
conns.mu.Lock()
defer conns.mu.Unlock()
delete(conns.conn, r)
}
func (conns *conns) Get(r *http.Request) *FlowData {
conns.mu.RLock()
defer conns.mu.RUnlock()
if v, ok := conns.conn[r]; ok {
return v
}
return nil
}
func GetInstance(r *http.Request) *FlowData {
if r == nil {
return nil
}
allconn.mu.RLock()
defer allconn.mu.RUnlock()
if v, ok := allconn.conn[r]; ok {
return v
}
return nil
}
func (data *FlowData) Set(k string, v interface{}) {
data.mu.Lock()
data.ctx[k] = v
data.mu.Unlock()
}
func (data *FlowData) GetConnectId() int64 {
data.mu.RLock()
defer data.mu.RUnlock()
return data.connectId
}
func (data *FlowData) GetFuncName() string {
data.mu.RLock()
defer data.mu.RUnlock()
return data.funcName
}
func (data *FlowData) GetPageKeys() map[string]struct{} {
data.mu.RLock()
defer data.mu.RUnlock()
return data.pages
}
func (data *FlowData) Get(k string) interface{} {
data.mu.RLock()
defer data.mu.RUnlock()
if v, ok := data.ctx[k]; ok {
return v
}
return nil
}
func (data *FlowData) Del(k string) {
data.mu.Lock()
delete(data.ctx, k)
data.mu.Unlock()
}