diff --git a/client/internal/connect.go b/client/internal/connect.go index 3aca0bab9..47c63e6d0 100644 --- a/client/internal/connect.go +++ b/client/internal/connect.go @@ -164,6 +164,7 @@ func RunClient(ctx context.Context, config *Config, statusRecorder *peer.Status, state.Set(StatusConnected) <-engineCtx.Done() + statusRecorder.ClientTeardown() backOff.Reset() diff --git a/client/internal/peer/listener.go b/client/internal/peer/listener.go index c8dc0fe70..c601fe534 100644 --- a/client/internal/peer/listener.go +++ b/client/internal/peer/listener.go @@ -5,6 +5,7 @@ type Listener interface { OnConnected() OnDisconnected() OnConnecting() + OnDisconnecting() OnAddressChanged(string, string) OnPeersListChanged(int) } diff --git a/client/internal/peer/notifier.go b/client/internal/peer/notifier.go index efc9e47ad..4e618d2f8 100644 --- a/client/internal/peer/notifier.go +++ b/client/internal/peer/notifier.go @@ -8,6 +8,7 @@ const ( stateDisconnected = iota stateConnected stateConnecting + stateDisconnecting ) type notifier struct { @@ -57,8 +58,12 @@ func (n *notifier) updateServerStates(mgmState bool, signalState bool) { } n.currentServerState = newState - n.lastNotification = n.calculateState(newState, n.currentClientState) + if n.lastNotification == stateDisconnecting { + return + } + + n.lastNotification = n.calculateState(newState, n.currentClientState) go n.notifyAll(n.lastNotification) } @@ -78,6 +83,14 @@ func (n *notifier) clientStop() { go n.notifyAll(n.lastNotification) } +func (n *notifier) clientTearDown() { + n.serverStateLock.Lock() + defer n.serverStateLock.Unlock() + n.currentClientState = false + n.lastNotification = stateDisconnecting + go n.notifyAll(n.lastNotification) +} + func (n *notifier) isServerStateChanged(newState bool) bool { return n.currentServerState != newState } @@ -99,6 +112,8 @@ func (n *notifier) notifyListener(l Listener, state int) { l.OnConnected() case stateConnecting: l.OnConnecting() + case stateDisconnecting: + l.OnDisconnecting() } } diff --git a/client/internal/peer/status.go b/client/internal/peer/status.go index 1ecdff301..62841d6fc 100644 --- a/client/internal/peer/status.go +++ b/client/internal/peer/status.go @@ -288,6 +288,11 @@ func (d *Status) ClientStop() { d.notifier.clientStop() } +// ClientTeardown will notify all listeners about the service is under teardown +func (d *Status) ClientTeardown() { + d.notifier.clientTearDown() +} + // AddConnectionListener add a listener to the notifier func (d *Status) AddConnectionListener(listener Listener) { d.notifier.addListener(listener)