-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
83 lines (60 loc) · 2 KB
/
sync.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
package curator
type syncBuilder struct {
client *curatorFramework
backgrounding backgrounding
}
func (b *syncBuilder) ForPath(givenPath string) (string, error) {
adjustedPath := b.client.fixForNamespace(givenPath, false)
if b.backgrounding.inBackground {
go b.pathInBackground(adjustedPath, givenPath)
return givenPath, nil
} else {
return b.pathInForeground(adjustedPath)
}
}
func (b *syncBuilder) pathInBackground(path string, givenPath string) {
tracer := b.client.ZookeeperClient().StartTracer("syncBuilder.pathInBackground")
defer tracer.Commit()
syncPath, err := b.pathInForeground(path)
if b.backgrounding.callback != nil {
event := &curatorEvent{
eventType: SYNC,
err: err,
path: b.client.unfixForNamespace(syncPath),
context: b.backgrounding.context,
}
if err != nil {
event.path = givenPath
}
event.name = GetNodeFromPath(event.path)
b.backgrounding.callback(b.client, event)
}
}
func (b *syncBuilder) 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 {
return conn.Sync(path)
}
})
syncPath, _ := result.(string)
return b.client.unfixForNamespace(syncPath), err
}
func (b *syncBuilder) InBackground() SyncBuilder {
b.backgrounding = backgrounding{inBackground: true}
return b
}
func (b *syncBuilder) InBackgroundWithContext(context interface{}) SyncBuilder {
b.backgrounding = backgrounding{inBackground: true, context: context}
return b
}
func (b *syncBuilder) InBackgroundWithCallback(callback BackgroundCallback) SyncBuilder {
b.backgrounding = backgrounding{inBackground: true, callback: callback}
return b
}
func (b *syncBuilder) InBackgroundWithCallbackAndContext(callback BackgroundCallback, context interface{}) SyncBuilder {
b.backgrounding = backgrounding{inBackground: true, context: context, callback: callback}
return b
}