Skip to content

Commit

Permalink
Prune nodes which had at least one attempt, and it was not successfull (
Browse files Browse the repository at this point in the history
  • Loading branch information
svarogg authored Feb 1, 2022
1 parent 43f1ada commit 0a8e1a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
5 changes: 2 additions & 3 deletions dnsseed.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func creep() {
var knownPeers []*appmessage.NetAddress

if len(ActiveConfig().KnownPeers) != 0 {

for _, p := range strings.Split(ActiveConfig().KnownPeers, ",") {
addressStr := strings.Split(p, ":")
if len(addressStr) != 2 {
Expand Down Expand Up @@ -138,8 +137,9 @@ func creep() {
}

func pollPeer(netAdapter *standalone.MinimalNetAdapter, addr *appmessage.NetAddress) error {
peerAddress := net.JoinHostPort(addr.IP.String(), strconv.Itoa(int(addr.Port)))
defer amgr.Attempt(addr.IP)

peerAddress := net.JoinHostPort(addr.IP.String(), strconv.Itoa(int(addr.Port)))
routes, err := netAdapter.Connect(peerAddress)
if err != nil {
return errors.Wrapf(err, "could not connect to %s", peerAddress)
Expand All @@ -162,7 +162,6 @@ func pollPeer(netAdapter *standalone.MinimalNetAdapter, addr *appmessage.NetAddr
log.Infof("Peer %s sent %d addresses, %d new",
peerAddress, len(msgAddresses.AddressList), added)

amgr.Attempt(addr.IP)
amgr.Good(addr.IP, nil)

return nil
Expand Down
23 changes: 15 additions & 8 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,24 @@ func (m *Manager) prunePeers() {
var count int
now := time.Now()
m.mtx.Lock()

lastSeenAbovePruneExpire := func(node *Node) bool {
return now.Sub(node.LastSeen) > pruneExpireTimeout
}
hadAttemptsButNoSuccess := func(node *Node) bool {
return !node.LastAttempt.IsZero() && node.LastSuccess.IsZero()
}
hadSuccessButLongTimeAgo := func(node *Node) bool {
return !node.LastSuccess.IsZero() && now.Sub(node.LastSuccess) > pruneExpireTimeout
}

for k, node := range m.nodes {
if now.Sub(node.LastSeen) > pruneExpireTimeout {
delete(m.nodes, k)
count++
continue
}
if !node.LastSuccess.IsZero() &&
now.Sub(node.LastSuccess) > pruneExpireTimeout {
if lastSeenAbovePruneExpire(node) ||
hadAttemptsButNoSuccess(node) ||
hadSuccessButLongTimeAgo(node) {

delete(m.nodes, k)
count++
continue
}
}
l := len(m.nodes)
Expand Down

0 comments on commit 0a8e1a7

Please sign in to comment.