-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
146 lines (126 loc) · 3.81 KB
/
register.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package owo
import (
"encoding/json"
"fmt"
"github.com/samuel/go-zookeeper/zk"
"github.com/wuciyou/dogo"
"sync"
"time"
)
var rootPath = "/owo_data"
type providerEvent int
const (
PROVIDER_DELETE providerEvent = iota
PROVIDER_ADD
)
type hooksMethod func(r *register)
type register struct {
addr string
rootPath string
zkConn *zk.Conn
cacheSelfProviderNodeMap map[string]*providerNode
rwm *sync.RWMutex
zkConnEvent <-chan zk.Event
hooksMethod map[string][]hooksMethod
}
func initRegister(addr string) *register {
var err error
r := ®ister{rootPath: rootPath}
r.cacheSelfProviderNodeMap = make(map[string]*providerNode)
r.rwm = &sync.RWMutex{}
r.zkConn, r.zkConnEvent, err = zk.Connect([]string{addr}, time.Second*10)
r.hooksMethod = make(map[string][]hooksMethod)
if err != nil {
dogo.Dglog.Error(err)
}
exists, _, err := r.zkConn.Exists(r.rootPath)
if err != nil {
dogo.Dglog.Error(err)
}
if !exists {
if _, err := r.zkConn.Create(r.rootPath, []byte{}, 0, zk.WorldACL(zk.PermAll)); err != nil {
dogo.Dglog.Errorf("Create path:'%s' returned error:%+v ", r.rootPath, err)
}
}
r.listenEvent(r.zkConnEvent)
// 断线重新连接成功后,重新注册服务
r.listenEventFunc(r.disRePush, zk.EventSession, zk.StateHasSession)
return r
}
func (this *register) listenEvent(event <-chan zk.Event) {
go func() {
for {
e := <-event
for _, hookFunc := range this.hooksMethod[fmt.Sprintf("%d_%d", e.Type, e.State)] {
hookFunc(this)
}
for _, hookFunc := range this.hooksMethod[fmt.Sprintf("%d_", e.Type)] {
hookFunc(this)
}
}
}()
}
func (this *register) listenEventFunc(hook hooksMethod, eventType zk.EventType, eventStat ...zk.State) {
hooksKey := fmt.Sprintf("%d_", eventType)
if len(eventStat) > 0 {
hooksKey = fmt.Sprintf("%d_%d", eventType, eventStat[0])
}
this.rwm.Lock()
this.hooksMethod[hooksKey] = append(this.hooksMethod[hooksKey], hook)
this.rwm.Unlock()
}
func (this *register) listenNode(callback func([]byte, string, providerEvent), isInit bool) {
var oldChilds = make(map[string][]byte)
for {
var curChilds = make(map[string][]byte)
var allChilds = make(map[string][]byte)
childs, _, event, err := this.zkConn.ChildrenW(this.rootPath)
for _, childName := range childs {
childData, _, childErr := this.zkConn.Get(this.rootPath + "/" + childName)
if childErr != nil {
dogo.Dglog.Warningf("Get path'%s' returned error:%+v\n", childName, childErr)
} else {
curChilds[childName] = childData
allChilds[childName] = childData
}
}
if len(oldChilds) > 0 {
for childName, childData := range oldChilds {
if _, exsits := curChilds[childName]; !exsits {
callback(childData, childName, PROVIDER_DELETE)
} else {
delete(curChilds, childName)
}
}
}
for childName, childData := range curChilds {
callback(childData, childName, PROVIDER_ADD)
}
if isInit {
break
}
e := <-event
dogo.Dglog.Infof("event:%+v, err:%+v \n", e, err)
oldChilds = allChilds
}
}
func (this *register) disRePush(r *register) {
for path, providerNode := range this.cacheSelfProviderNodeMap {
if isExists, _, err := this.zkConn.Exists(path); !isExists || err != nil {
dogo.Dglog.Warningf("repush provider node old path:'%s', err:%+v", path, err)
this.push(providerNode)
}
}
}
func (this *register) push(pNode *providerNode) {
nodePath := fmt.Sprintf("%s/%s_%s", this.rootPath, pNode.Name, pNode.Addr)
data, _ := json.Marshal(pNode)
if path, err := this.zkConn.CreateProtectedEphemeralSequential(nodePath, data, zk.WorldACL(zk.PermAll)); err != nil {
dogo.Dglog.Errorf("Create path:'%s' returned error:%+v ", nodePath, err)
} else {
this.rwm.Lock()
pNode.Id = path
this.cacheSelfProviderNodeMap[path] = pNode
this.rwm.Unlock()
}
}