global.go
1.4 KB
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
package protocol
import (
"sort"
"sync"
)
var(
SuccessRequestMap *sync.Map
ErrorRequestMap *sync.Map
)
const(
StatusOK ="OK"
StatusFial ="Fail"
)
func init(){
SuccessRequestMap =new(sync.Map) //&sync.Map{}
ErrorRequestMap = new(sync.Map)
//SuccessRequestMap.Store("/auth/login",&Request{Host:"1237.0.0.1"})
}
func Exists(status string,key string)bool{
m :=GetMap(status)
if _,ok:=m.Load(key);ok{
return true
}
return false
}
func SetRequest(status string,key string,req *Request){
m :=GetMap(status)
if _,ok:=m.Load(key);ok{
return
}
m.Store(key,req)
}
func GetRequest(status string,key string)(*Request){
m :=GetMap(status)
if v,ok:=m.Load(key);ok{
return v.(*Request)
}
return nil
}
func GetRequests(status string)([]*Request){
m :=GetMap(status)
var listKey []string
var list []*Request
m.Range(func(key,value interface{})bool{
listKey = append(listKey,key.(string))
return true
})
sort.Strings(listKey)
for i:=range listKey{
r :=GetRequest(status,listKey[i])
if r!=nil{
list = append(list,r)
}
}
return list
}
func DeleteRequest(status string,key string){
m :=GetMap(status)
m.Delete(key)
}
func DeleteAll(status string){
m :=GetMap(status)
m.Range(func(key,value interface{})bool{
m.Delete(key)
return true
})
}
func GetMap(status string) *sync.Map{
if status==StatusOK{
return SuccessRequestMap
}
return ErrorRequestMap
}