make sure unlock safe even if listeners panic (#383)

* make sure unlock safe even if listeners panic

* fix #378

* fix #378
master
Kevin Wan 4 years ago committed by GitHub
parent 1f1dcd16e6
commit 37c3b9f5c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -4,10 +4,12 @@ package proc
import "time"
// AddShutdownListener returns fn itself on windows, lets callers call fn on their own.
func AddShutdownListener(fn func()) func() {
return fn
}
// AddWrapUpListener returns fn itself on windows, lets callers call fn on their own.
func AddWrapUpListener(fn func()) func() {
return fn
}

Loading…
Cancel
Save