forked from flier/curator.go
/
children.go
136 lines (101 loc) · 3.12 KB
/
children.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
package curator
import (
"github.com/samuel/go-zookeeper/zk"
)
type getChildrenBuilder struct {
client *curatorFramework
backgrounding backgrounding
stat *zk.Stat
watching watching
}
func (b *getChildrenBuilder) ForPath(givenPath string) ([]string, error) {
adjustedPath := b.client.fixForNamespace(givenPath, false)
if b.backgrounding.inBackground {
go b.pathInBackground(adjustedPath, givenPath)
return nil, nil
}
if children, err := b.pathInForeground(adjustedPath); err != nil {
return nil, err
} else {
return children, err
}
}
func (b *getChildrenBuilder) pathInBackground(adjustedPath, givenPath string) {
tracer := b.client.ZookeeperClient().StartTracer("getChildrenBuilder.pathInBackground")
defer tracer.Commit()
children, err := b.pathInForeground(adjustedPath)
if b.backgrounding.callback != nil {
event := &curatorEvent{
eventType: CHILDREN,
err: err,
path: b.client.unfixForNamespace(adjustedPath),
children: children,
stat: b.stat,
context: b.backgrounding.context,
}
if err != nil {
event.path = givenPath
}
event.name = GetNodeFromPath(event.path)
b.backgrounding.callback(b.client, event)
}
}
func (b *getChildrenBuilder) pathInForeground(path string) ([]string, error) {
zkClient := b.client.ZookeeperClient()
result, err := zkClient.NewRetryLoop().CallWithRetry(func() (interface{}, error) {
if conn, err := zkClient.Conn(); err != nil {
return nil, err
} else {
var children []string
var stat *zk.Stat
var events <-chan zk.Event
var err error
if b.watching.watched || b.watching.watcher != nil {
children, stat, events, err = conn.ChildrenW(path)
if events != nil && b.watching.watcher != nil {
go NewWatchers(b.watching.watcher).Watch(events)
}
} else {
children, stat, err = conn.Children(path)
}
if stat != nil {
if b.stat != nil {
*b.stat = *stat
} else {
b.stat = stat
}
}
return children, err
}
})
children, _ := result.([]string)
return children, err
}
func (b *getChildrenBuilder) StoringStatIn(stat *zk.Stat) GetChildrenBuilder {
b.stat = stat
return b
}
func (b *getChildrenBuilder) Watched() GetChildrenBuilder {
b.watching.watched = true
return b
}
func (b *getChildrenBuilder) UsingWatcher(watcher Watcher) GetChildrenBuilder {
b.watching.watcher = b.client.getNamespaceWatcher(watcher)
return b
}
func (b *getChildrenBuilder) InBackground() GetChildrenBuilder {
b.backgrounding = backgrounding{inBackground: true}
return b
}
func (b *getChildrenBuilder) InBackgroundWithContext(context interface{}) GetChildrenBuilder {
b.backgrounding = backgrounding{inBackground: true, context: context}
return b
}
func (b *getChildrenBuilder) InBackgroundWithCallback(callback BackgroundCallback) GetChildrenBuilder {
b.backgrounding = backgrounding{inBackground: true, callback: callback}
return b
}
func (b *getChildrenBuilder) InBackgroundWithCallbackAndContext(callback BackgroundCallback, context interface{}) GetChildrenBuilder {
b.backgrounding = backgrounding{inBackground: true, context: context, callback: callback}
return b
}