|
|
@ -18,6 +18,7 @@ type (
|
|
|
|
disconnected bool
|
|
|
|
disconnected bool
|
|
|
|
currentState connectivity.State
|
|
|
|
currentState connectivity.State
|
|
|
|
listeners []func()
|
|
|
|
listeners []func()
|
|
|
|
|
|
|
|
// lock only guards listeners, because only listens can be accessed by other goroutines.
|
|
|
|
lock sync.Mutex
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -32,27 +33,33 @@ func (sw *stateWatcher) addListener(l func()) {
|
|
|
|
sw.lock.Unlock()
|
|
|
|
sw.lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (sw *stateWatcher) watch(conn etcdConn) {
|
|
|
|
func (sw *stateWatcher) notifyListeners() {
|
|
|
|
sw.currentState = conn.GetState()
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
if conn.WaitForStateChange(context.Background(), sw.currentState) {
|
|
|
|
|
|
|
|
newState := conn.GetState()
|
|
|
|
|
|
|
|
sw.lock.Lock()
|
|
|
|
sw.lock.Lock()
|
|
|
|
sw.currentState = newState
|
|
|
|
defer sw.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, l := range sw.listeners {
|
|
|
|
|
|
|
|
l()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch newState {
|
|
|
|
func (sw *stateWatcher) updateState(conn etcdConn) {
|
|
|
|
|
|
|
|
sw.currentState = conn.GetState()
|
|
|
|
|
|
|
|
switch sw.currentState {
|
|
|
|
case connectivity.TransientFailure, connectivity.Shutdown:
|
|
|
|
case connectivity.TransientFailure, connectivity.Shutdown:
|
|
|
|
sw.disconnected = true
|
|
|
|
sw.disconnected = true
|
|
|
|
case connectivity.Ready:
|
|
|
|
case connectivity.Ready:
|
|
|
|
if sw.disconnected {
|
|
|
|
if sw.disconnected {
|
|
|
|
sw.disconnected = false
|
|
|
|
sw.disconnected = false
|
|
|
|
for _, l := range sw.listeners {
|
|
|
|
sw.notifyListeners()
|
|
|
|
l()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sw.lock.Unlock()
|
|
|
|
func (sw *stateWatcher) watch(conn etcdConn) {
|
|
|
|
|
|
|
|
sw.currentState = conn.GetState()
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
if conn.WaitForStateChange(context.Background(), sw.currentState) {
|
|
|
|
|
|
|
|
sw.updateState(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|