Skip to content

Commit

Permalink
add: new known error
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Nov 30, 2023
1 parent cca3cb0 commit 7bc88b3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
2 changes: 2 additions & 0 deletions db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var KnownErrors = map[string]string{
"can't assign requested address": models.NetErrorCantAssignRequestedAddress, // transient error
"cannot assign requested address": models.NetErrorCantAssignRequestedAddress, // transient error
"connection gated": models.NetErrorConnectionGated, // transient error
"connection closed immediately": models.NetErrorConnectionClosedImmediately,
}

var ErrorStr = map[string]string{}
Expand Down Expand Up @@ -74,6 +75,7 @@ var knownErrorsPrecedence = []string{
"failed to negotiate stream multiplexer",
"resource limit exceeded",
"Write on stream",
"connection closed immediately",
}

// NetError extracts the appropriate error type from the given error.
Expand Down
1 change: 1 addition & 0 deletions db/migrations/000026_add_net_errors.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ BEGIN;
ALTER TYPE net_error ADD VALUE 'connection_reset_by_peer';
ALTER TYPE net_error ADD VALUE 'cant_assign_requested_address';
ALTER TYPE net_error ADD VALUE 'connection_gated';
ALTER TYPE net_error ADD VALUE 'connection_closed_immediately';
ALTER TYPE net_error RENAME VALUE 'no_public_ip' TO 'no_ip_address';

CREATE OR REPLACE FUNCTION calc_max_failed_visits(
Expand Down
46 changes: 24 additions & 22 deletions db/models/boil_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions discv5/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/friendsofgo/errors"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/host/basic"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -211,26 +212,43 @@ func (c *Crawler) connect(ctx context.Context, pi peer.AddrInfo) error {
}

retry := 0
maxRetries := 1
maxRetries := 2
for {

timeout := time.Duration(c.cfg.DialTimeout.Nanoseconds() / int64(retry+1))

logEntry := log.WithFields(log.Fields{
"timeout": timeout.String(),
"remoteID": dialAddrInfo.ID.String(),
"retry": retry,
"maddrs": dialAddrInfo.Addrs,
})
logEntry.Debugln("Connecting to peer", dialAddrInfo.ID.ShortString())

timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
err := c.host.Connect(timeoutCtx, dialAddrInfo)
cancel()

if err == nil {
return nil
if c.host.Network().Connectedness(pi.ID) != network.Connected {
err = fmt.Errorf("connection closed immediately")
} else {
return nil
}
}

switch true {
case strings.Contains(err.Error(), db.ErrorStr[models.NetErrorNegotiateSecurityProtocol]):
case strings.Contains(err.Error(), db.ErrorStr[models.NetErrorConnectionRefused]):
case strings.Contains(err.Error(), db.ErrorStr[models.NetErrorConnectionResetByPeer]):
case strings.Contains(err.Error(), db.ErrorStr[models.NetErrorConnectionClosedImmediately]):
default:
logEntry.WithError(err).Debugln("Failed connecting to peer", dialAddrInfo.ID.ShortString())
return err
}

if retry == maxRetries {
logEntry.WithError(err).Debugln("Exceeded retries connecting to peer", dialAddrInfo.ID.ShortString())
return err
}

Expand Down
2 changes: 1 addition & 1 deletion discv5/driver_crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p PeerInfo) Addrs() []ma.Multiaddr {

func (p PeerInfo) Merge(other PeerInfo) PeerInfo {
p.maddrs = utils.MergeMaddrs(p.maddrs, other.maddrs)
return p // TODO: merge
return p
}

type CrawlDriverConfig struct {
Expand Down

0 comments on commit 7bc88b3

Please sign in to comment.