forked from gmsec/micro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.go
80 lines (65 loc) · 1.35 KB
/
global.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
package micro
import (
"fmt"
"sync"
"github.com/xxjwxc/public/mylog"
"github.com/gmsec/micro/client"
)
var mut sync.RWMutex
var _mp map[string]Service
var _cliMut sync.RWMutex
var _cliGroup map[string]string
func init() {
_mp = make(map[string]Service)
_cliGroup = make(map[string]string)
}
// GetService get service
func GetService(name string) Service {
mut.RLock()
defer mut.RUnlock()
if len(name) > 0 {
s, ok := _mp[name]
if ok {
return s
}
}
mylog.Info(fmt.Sprintf("[%v]:not fond ,use traverse mode", name))
for k, v := range _mp {
mylog.Info(k)
return v
}
mylog.ErrorString("please init first.")
return nil
}
// GetClient get client from cliet name
func GetClient(clientName string) client.Client {
var serverName = ""
_cliMut.RLock()
defer _cliMut.RUnlock()
if _, ok := _cliGroup[clientName]; ok {
serverName = _cliGroup[clientName]
}
s := GetService(serverName)
if s != nil {
return s.Client()
}
return nil
}
// SetClientServiceName set service name with client name
func SetClientServiceName(clientName, serviceName string) {
_cliMut.Lock()
defer _cliMut.Unlock()
_cliGroup[clientName] = serviceName
}
// IsExist existed
func IsExist(name string) bool {
mut.RLock()
defer mut.RUnlock()
_, ok := _mp[name]
return ok
}
func initService(name string, s *service) {
mut.Lock()
defer mut.Unlock()
_mp[name] = s
}