diff --git a/client/internal/conn_mgr.go b/client/internal/conn_mgr.go index ce4e9b7d4..3ba9c9607 100644 --- a/client/internal/conn_mgr.go +++ b/client/internal/conn_mgr.go @@ -133,7 +133,7 @@ func (e *ConnMgr) SetExcludeList(ctx context.Context, peerIDs map[string]bool) { excludedPeers = append(excludedPeers, lazyPeerCfg) } - added := e.lazyConnMgr.ExcludePeer(e.lazyCtx, excludedPeers) + added := e.lazyConnMgr.ExcludePeer(excludedPeers) for _, peerID := range added { var peerConn *peer.Conn var exists bool diff --git a/client/internal/lazyconn/inactivity/manager.go b/client/internal/lazyconn/inactivity/manager.go index 50a16aebb..34340cd7f 100644 --- a/client/internal/lazyconn/inactivity/manager.go +++ b/client/internal/lazyconn/inactivity/manager.go @@ -26,6 +26,8 @@ const ( checkInterval = keepAliveInterval // todo: 5 * time.Second keepAliveCheckPeriod = keepAliveInterval + + inactivityThreshold = 3 // number of checks to consider peer inactive ) const ( @@ -98,17 +100,22 @@ func (m *Manager) Start(ctx context.Context) { if len(idlePeers) == 0 { continue } - select { - case m.InactivePeersChan <- idlePeers: - case <-ctx.Done(): - continue - default: - continue - } + + m.notifyInactivePeers(ctx, idlePeers) } } } +func (m *Manager) notifyInactivePeers(ctx context.Context, inactivePeers []string) { + select { + case m.InactivePeersChan <- inactivePeers: + case <-ctx.Done(): + return + default: + return + } +} + func (m *Manager) checkStats(now time.Time) ([]string, error) { stats, err := m.iface.GetStats() if err != nil { @@ -120,8 +127,8 @@ func (m *Manager) checkStats(now time.Time) ([]string, error) { for peer, info := range m.interestedPeers { stat, found := stats[peer] if !found { + // when peer is in connecting state info.log.Warnf("peer not found in wg stats") - continue } // First measurement: initialize @@ -137,7 +144,7 @@ func (m *Manager) checkStats(now time.Time) ([]string, error) { continue } - // sometimes we measure false inactivity, so we need to check if we have activity in a row + // sometimes we measure false inactivity, so we need to check if we have inactivity in a row if isInactive(stat, info) { info.inActivityInRow++ } else { @@ -145,7 +152,7 @@ func (m *Manager) checkStats(now time.Time) ([]string, error) { } info.log.Infof("peer inactivity counter: %d", info.inActivityInRow) - if info.inActivityInRow >= 3 { + if info.inActivityInRow >= inactivityThreshold { info.log.Infof("peer is inactive for %d checks, marking as inactive", info.inActivityInRow) idlePeers = append(idlePeers, peer) info.inActivityInRow = 0 @@ -159,6 +166,14 @@ func (m *Manager) checkStats(now time.Time) ([]string, error) { func isInactive(stat configurer.WGStats, info *peerInfo) bool { rxSyncPrevPeriod := stat.RxBytes - info.lastRxBytesAtLastIdleCheck + + // when the peer reconnected we lose the rx bytes from between the reset and the last check. + // We will suppose the peer was active + if rxSyncPrevPeriod < 0 { + info.log.Debugf("rxBytes decreased, resetting last rxBytes at last idle check") + return false + } + switch rxSyncPrevPeriod { case 0: info.log.Debugf("peer inactive, received 0 bytes") diff --git a/client/internal/lazyconn/manager/manager.go b/client/internal/lazyconn/manager/manager.go index bb753d49e..c6e51cee2 100644 --- a/client/internal/lazyconn/manager/manager.go +++ b/client/internal/lazyconn/manager/manager.go @@ -157,7 +157,7 @@ func (m *Manager) Start(ctx context.Context) { // Adds them back to the managed list and start the inactivity listener if they are removed from the exclude list. In // this case, we suppose that the connection status is connected or connecting. // If the peer is not exists yet in the managed list then the responsibility is the upper layer to call the AddPeer function -func (m *Manager) ExcludePeer(ctx context.Context, peerConfigs []lazyconn.PeerConfig) []string { +func (m *Manager) ExcludePeer(peerConfigs []lazyconn.PeerConfig) []string { m.managedPeersMu.Lock() defer m.managedPeersMu.Unlock() @@ -517,4 +517,5 @@ func (m *Manager) onPeerDisconnected(peerConnID peerid.ConnID) { } // todo reset inactivity monitor + mp.peerCfg.Log.Infof("--- peer disconnected, stopping inactivity monitor?") }