mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-21 02:08:40 +02:00
Fix counter
This commit is contained in:
parent
2d401a7dce
commit
55e7ca96df
@ -216,7 +216,7 @@ func (e *ConnMgr) ActivatePeer(ctx context.Context, conn *peer.Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
if found := e.lazyConnMgr.ActivatePeer(e.lazyCtx, conn.GetKey()); found {
|
||||
if found := e.lazyConnMgr.ActivatePeer(conn.GetKey()); found {
|
||||
conn.Log.Infof("activated peer from inactive state")
|
||||
if err := conn.Open(ctx); err != nil {
|
||||
conn.Log.Errorf("failed to open connection: %v", err)
|
||||
@ -277,7 +277,7 @@ func (e *ConnMgr) addPeersToLazyConnManager() error {
|
||||
lazyPeerCfgs = append(lazyPeerCfgs, lazyPeerCfg)
|
||||
}
|
||||
|
||||
return e.lazyConnMgr.AddActivePeers(e.lazyCtx, lazyPeerCfgs)
|
||||
return e.lazyConnMgr.AddActivePeers(lazyPeerCfgs)
|
||||
}
|
||||
|
||||
func (e *ConnMgr) closeManager(ctx context.Context) {
|
||||
|
@ -60,14 +60,23 @@ func NewManager(iface WgInterface) *Manager {
|
||||
}
|
||||
|
||||
func (m *Manager) AddPeer(peerCfg *lazyconn.PeerConfig) {
|
||||
if _, exists := m.interestedPeers[peerCfg.PublicKey]; !exists {
|
||||
if _, exists := m.interestedPeers[peerCfg.PublicKey]; exists {
|
||||
return
|
||||
}
|
||||
|
||||
peerCfg.Log.Debugf("adding peer to inactivity manager")
|
||||
m.interestedPeers[peerCfg.PublicKey] = &peerInfo{
|
||||
log: peerCfg.Log,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) RemovePeer(peer string) {
|
||||
pi, ok := m.interestedPeers[peer]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
pi.log.Debugf("remove peer from inactivity manager")
|
||||
delete(m.interestedPeers, peer)
|
||||
}
|
||||
|
||||
@ -129,16 +138,17 @@ func (m *Manager) checkStats(now time.Time) ([]string, error) {
|
||||
}
|
||||
|
||||
// sometimes we measure false inactivity, so we need to check if we have activity in a row
|
||||
inactive := isInactive(stat, info)
|
||||
if inactive {
|
||||
if isInactive(stat, info) {
|
||||
info.inActivityInRow++
|
||||
} else {
|
||||
info.inActivityInRow = 0
|
||||
}
|
||||
|
||||
info.log.Infof("peer inactivity counter: %d", info.inActivityInRow)
|
||||
if info.inActivityInRow >= 3 {
|
||||
info.log.Infof("peer is inactive for %d checks, marking as inactive", info.inActivityInRow)
|
||||
idlePeers = append(idlePeers, peer)
|
||||
info.inActivityInRow = 0
|
||||
}
|
||||
info.lastIdleCheckAt = now
|
||||
info.lastRxBytesAtLastIdleCheck = stat.RxBytes
|
||||
@ -151,16 +161,16 @@ func isInactive(stat configurer.WGStats, info *peerInfo) bool {
|
||||
rxSyncPrevPeriod := stat.RxBytes - info.lastRxBytesAtLastIdleCheck
|
||||
switch rxSyncPrevPeriod {
|
||||
case 0:
|
||||
info.log.Tracef("peer inactive, received 0 bytes")
|
||||
info.log.Debugf("peer inactive, received 0 bytes")
|
||||
return true
|
||||
case keepAliveBytes:
|
||||
info.log.Tracef("peer inactive, only keep alive received, current RxBytes: %d", rxSyncPrevPeriod)
|
||||
info.log.Debugf("peer inactive, only keep alive received, current RxBytes: %d", rxSyncPrevPeriod)
|
||||
return true
|
||||
case handshakeInitBytes + keepAliveBytes:
|
||||
info.log.Tracef("peer inactive, only handshakeInitBytes + keepAliveBytes, current RxBytes: %d", rxSyncPrevPeriod)
|
||||
info.log.Debugf("peer inactive, only handshakeInitBytes + keepAliveBytes, current RxBytes: %d", rxSyncPrevPeriod)
|
||||
return true
|
||||
case handshakeRespBytes + keepAliveBytes:
|
||||
info.log.Tracef("peer inactive, only handshakeRespBytes + keepAliveBytes, current RxBytes: %d", rxSyncPrevPeriod)
|
||||
info.log.Debugf("peer inactive, only handshakeRespBytes + keepAliveBytes, current RxBytes: %d", rxSyncPrevPeriod)
|
||||
return true
|
||||
default:
|
||||
info.log.Infof("active, RxBytes: %d", rxSyncPrevPeriod)
|
||||
|
@ -218,8 +218,6 @@ func (m *Manager) AddPeer(peerCfg lazyconn.PeerConfig) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
m.inactivityManager.AddPeer(&peerCfg)
|
||||
|
||||
m.managedPeers[peerCfg.PublicKey] = &peerCfg
|
||||
m.managedPeersByConnID[peerCfg.PeerConnID] = &managedPeer{
|
||||
peerCfg: &peerCfg,
|
||||
@ -230,7 +228,7 @@ func (m *Manager) AddPeer(peerCfg lazyconn.PeerConfig) (bool, error) {
|
||||
|
||||
// AddActivePeers adds a list of peers to the lazy connection manager
|
||||
// suppose these peers was in connected or in connecting states
|
||||
func (m *Manager) AddActivePeers(ctx context.Context, peerCfg []lazyconn.PeerConfig) error {
|
||||
func (m *Manager) AddActivePeers(peerCfg []lazyconn.PeerConfig) error {
|
||||
m.managedPeersMu.Lock()
|
||||
defer m.managedPeersMu.Unlock()
|
||||
|
||||
@ -257,7 +255,7 @@ func (m *Manager) RemovePeer(peerID string) {
|
||||
|
||||
// ActivatePeer activates a peer connection when a signal message is received
|
||||
// Also activates all peers in the same HA groups as this peer
|
||||
func (m *Manager) ActivatePeer(ctx context.Context, peerID string) (found bool) {
|
||||
func (m *Manager) ActivatePeer(peerID string) (found bool) {
|
||||
m.managedPeersMu.Lock()
|
||||
defer m.managedPeersMu.Unlock()
|
||||
cfg, mp := m.getPeerForActivation(peerID)
|
||||
@ -387,7 +385,6 @@ func (m *Manager) addActivePeer(peerCfg *lazyconn.PeerConfig) error {
|
||||
expectedWatcher: watcherInactivity,
|
||||
}
|
||||
|
||||
peerCfg.Log.Infof("starting inactivity monitor on peer that has been removed from exclude list")
|
||||
m.inactivityManager.AddPeer(peerCfg)
|
||||
return nil
|
||||
}
|
||||
@ -502,7 +499,7 @@ func (m *Manager) onPeerConnected(peerConnID peerid.ConnID) {
|
||||
return
|
||||
}
|
||||
|
||||
mp.peerCfg.Log.Infof("peer connected, pausing inactivity monitor while connection is not disconnected")
|
||||
mp.peerCfg.Log.Infof("peer connected, starting inactivity monitor")
|
||||
m.inactivityManager.AddPeer(mp.peerCfg)
|
||||
}
|
||||
|
||||
@ -520,6 +517,4 @@ func (m *Manager) onPeerDisconnected(peerConnID peerid.ConnID) {
|
||||
}
|
||||
|
||||
// todo reset inactivity monitor
|
||||
|
||||
mp.peerCfg.Log.Infof("reset inactivity monitor timer")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user