-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatcher_client.go
178 lines (150 loc) · 2.96 KB
/
watcher_client.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package prophet
import (
"sync"
"sync/atomic"
"time"
"github.com/fagongzi/goetty"
)
// Watcher watcher client
type Watcher struct {
sync.RWMutex
ops uint64
addrs []string
p *defaultProphet
conn goetty.IOSession
resetConnFunc func(int)
eventC chan *EventNotify
stopC chan struct{}
}
// NewWatcher returns a watcher for watch
func NewWatcher(addrs ...string) *Watcher {
w := &Watcher{
addrs: addrs,
stopC: make(chan struct{}, 1),
}
w.resetConnFunc = w.resetConn
return w
}
// NewWatcherWithProphet returns a watcher for watch
func NewWatcherWithProphet(p Prophet) *Watcher {
w := &Watcher{
p: p.(*defaultProphet),
stopC: make(chan struct{}, 1),
}
w.resetConnFunc = w.resetConnWithProphet
return w
}
// Watch watch event
func (w *Watcher) Watch(flag int) chan *EventNotify {
w.Lock()
defer w.Unlock()
go w.watchDog(flag)
w.eventC = make(chan *EventNotify)
return w.eventC
}
// Stop stop watch
func (w *Watcher) Stop() {
w.Lock()
defer w.Unlock()
w.stopC <- struct{}{}
w.conn.Close()
close(w.eventC)
}
// Reset reset and refresh
func (w *Watcher) Reset() {
w.Lock()
defer w.Unlock()
w.conn.Close()
}
func (w *Watcher) resetConn(flag int) {
for {
addr := w.next()
if addr == "" {
continue
}
c := &codec{}
conn := goetty.NewConnector(addr,
goetty.WithClientDecoder(goetty.NewIntLengthFieldBasedDecoder(c)),
goetty.WithClientEncoder(goetty.NewIntLengthFieldBasedEncoder(c)),
goetty.WithClientConnectTimeout(time.Second*10))
_, err := conn.Connect()
if err != nil {
time.Sleep(time.Millisecond * 200)
continue
}
err = conn.WriteAndFlush(&InitWatcher{
Flag: flag,
})
if err != nil {
time.Sleep(time.Millisecond * 200)
continue
}
w.conn = conn
return
}
}
func (w *Watcher) resetConnWithProphet(flag int) {
for {
conn := w.p.getLeaderClient()
err := conn.WriteAndFlush(&InitWatcher{
Flag: flag,
})
if err != nil {
conn.Close()
time.Sleep(time.Millisecond * 200)
continue
}
w.conn = conn
return
}
}
func (w *Watcher) watchDog(flag int) {
defer func() {
if r := recover(); r != nil {
go w.watchDog(flag)
}
}()
for {
select {
case <-w.stopC:
w.conn.Close()
return
default:
w.resetConnFunc(flag)
w.startReadLoop()
}
}
}
func (w *Watcher) startReadLoop() {
expectSeq := uint64(0)
for {
msg, err := w.conn.Read()
if err != nil {
w.conn.Close()
return
}
if evt, ok := msg.(*EventNotify); ok {
// we lost some event notify, close the conection, and retry
if expectSeq != evt.Seq {
log.Warningf("watch lost some event notify, expect seq %d, but %d, close and retry",
expectSeq,
evt.Seq)
w.conn.Close()
return
}
expectSeq = evt.Seq + 1
w.eventC <- evt
} else {
w.conn.Close()
time.Sleep(time.Second * 5)
return
}
}
}
func (w *Watcher) next() string {
l := uint64(len(w.addrs))
if 0 >= l {
return ""
}
return w.addrs[int(atomic.AddUint64(&w.ops, 1)%l)]
}