Remove unused ctx

This commit is contained in:
Zoltán Papp 2025-06-20 16:41:40 +02:00
parent 55e7ca96df
commit a22a6f6d26
3 changed files with 28 additions and 12 deletions

View File

@ -133,7 +133,7 @@ func (e *ConnMgr) SetExcludeList(ctx context.Context, peerIDs map[string]bool) {
excludedPeers = append(excludedPeers, lazyPeerCfg) excludedPeers = append(excludedPeers, lazyPeerCfg)
} }
added := e.lazyConnMgr.ExcludePeer(e.lazyCtx, excludedPeers) added := e.lazyConnMgr.ExcludePeer(excludedPeers)
for _, peerID := range added { for _, peerID := range added {
var peerConn *peer.Conn var peerConn *peer.Conn
var exists bool var exists bool

View File

@ -26,6 +26,8 @@ const (
checkInterval = keepAliveInterval // todo: 5 * time.Second checkInterval = keepAliveInterval // todo: 5 * time.Second
keepAliveCheckPeriod = keepAliveInterval keepAliveCheckPeriod = keepAliveInterval
inactivityThreshold = 3 // number of checks to consider peer inactive
) )
const ( const (
@ -98,14 +100,19 @@ func (m *Manager) Start(ctx context.Context) {
if len(idlePeers) == 0 { if len(idlePeers) == 0 {
continue continue
} }
m.notifyInactivePeers(ctx, idlePeers)
}
}
}
func (m *Manager) notifyInactivePeers(ctx context.Context, inactivePeers []string) {
select { select {
case m.InactivePeersChan <- idlePeers: case m.InactivePeersChan <- inactivePeers:
case <-ctx.Done(): case <-ctx.Done():
continue return
default: default:
continue return
}
}
} }
} }
@ -120,8 +127,8 @@ func (m *Manager) checkStats(now time.Time) ([]string, error) {
for peer, info := range m.interestedPeers { for peer, info := range m.interestedPeers {
stat, found := stats[peer] stat, found := stats[peer]
if !found { if !found {
// when peer is in connecting state
info.log.Warnf("peer not found in wg stats") info.log.Warnf("peer not found in wg stats")
continue
} }
// First measurement: initialize // First measurement: initialize
@ -137,7 +144,7 @@ func (m *Manager) checkStats(now time.Time) ([]string, error) {
continue 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) { if isInactive(stat, info) {
info.inActivityInRow++ info.inActivityInRow++
} else { } else {
@ -145,7 +152,7 @@ func (m *Manager) checkStats(now time.Time) ([]string, error) {
} }
info.log.Infof("peer inactivity counter: %d", info.inActivityInRow) 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) info.log.Infof("peer is inactive for %d checks, marking as inactive", info.inActivityInRow)
idlePeers = append(idlePeers, peer) idlePeers = append(idlePeers, peer)
info.inActivityInRow = 0 info.inActivityInRow = 0
@ -159,6 +166,14 @@ func (m *Manager) checkStats(now time.Time) ([]string, error) {
func isInactive(stat configurer.WGStats, info *peerInfo) bool { func isInactive(stat configurer.WGStats, info *peerInfo) bool {
rxSyncPrevPeriod := stat.RxBytes - info.lastRxBytesAtLastIdleCheck 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 { switch rxSyncPrevPeriod {
case 0: case 0:
info.log.Debugf("peer inactive, received 0 bytes") info.log.Debugf("peer inactive, received 0 bytes")

View File

@ -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 // 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. // 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 // 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() m.managedPeersMu.Lock()
defer m.managedPeersMu.Unlock() defer m.managedPeersMu.Unlock()
@ -517,4 +517,5 @@ func (m *Manager) onPeerDisconnected(peerConnID peerid.ConnID) {
} }
// todo reset inactivity monitor // todo reset inactivity monitor
mp.peerCfg.Log.Infof("--- peer disconnected, stopping inactivity monitor?")
} }