statistics on stale peers

This commit is contained in:
Matus Kysel 2024-08-22 09:23:58 +02:00
parent 3adcfabb41
commit 7dcd01e9be
No known key found for this signature in database
GPG Key ID: A2A6AB68CB1C0A94

@ -369,6 +369,7 @@ func newHandler(config *handlerConfig) (*handler, error) {
} }
h.txFetcher = fetcher.NewTxFetcher(h.txpool.Has, addTxs, fetchTx, h.removePeer) h.txFetcher = fetcher.NewTxFetcher(h.txpool.Has, addTxs, fetchTx, h.removePeer)
h.chainSync = newChainSyncer(h) h.chainSync = newChainSyncer(h)
h.printPeerStatus()
return h, nil return h, nil
} }
@ -1017,3 +1018,53 @@ func (h *handler) enableSyncedFeatures() {
// h.chain.TrieDB().SetBufferSize(pathdb.DefaultBufferSize) // h.chain.TrieDB().SetBufferSize(pathdb.DefaultBufferSize)
// } // }
} }
type PeerDummy struct {
ID string
Head common.Hash
TD *big.Int
TimeStamp time.Time
}
func (h *handler) printPeerStatus() {
go func() {
statusMap := make(map[string]PeerDummy)
for {
// run in timer very 1 minue of time
time.Sleep(1 * time.Minute)
// print peer status
h.peers.lock.RLock()
for _, peer := range h.peers.peers {
head, td := peer.Peer.Head()
if p, ok := statusMap[peer.Peer.ID()]; ok {
if p.Head == head && p.TD.Cmp(td) == 0 {
continue
}
p.Head = head
p.TD = td
p.TimeStamp = time.Now()
statusMap[peer.Peer.ID()] = p
} else {
statusMap[peer.Peer.ID()] = PeerDummy{
ID: peer.Peer.ID(),
Head: head,
TD: td,
TimeStamp: time.Now(),
}
}
}
h.peers.lock.RUnlock()
var count int
for _, peer := range statusMap {
if peer.TimeStamp.Before(time.Now().Add(-60 * time.Minute)) {
count++
log.Warn("XXX peer", peer.ID, "head", peer.Head, "TD", peer.TD, "TimeStamp", peer.TimeStamp)
}
}
if count > 0 {
log.Warn("Total peers: ", len(statusMap), "inactive peers: ", count)
}
}
}()
}