This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dial: return a nice custom dial error
* Limits the number of dial errors we keep * Exposes the dial errors * Avoids string formatting unless we actually need to. Helps address #119
- Loading branch information
Showing
6 changed files
with
105 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package swarm | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
peer "github.com/libp2p/go-libp2p-peer" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
// maxDialErrors is the maximum number of dial errors we record | ||
const maxDialErrors = 32 | ||
|
||
// DialError is the error type returned when dialing. | ||
type DialError struct { | ||
Peer peer.ID | ||
Errors []TransportError | ||
Cause error | ||
Skipped int | ||
} | ||
|
||
func (e *DialError) recordErr(addr ma.Multiaddr, err error) { | ||
if len(e.Errors) >= maxDialErrors { | ||
e.Skipped++ | ||
return | ||
} | ||
e.Errors = append(e.Errors, TransportError{ | ||
Address: addr, | ||
Cause: err, | ||
}) | ||
} | ||
|
||
func (e *DialError) Error() string { | ||
var builder strings.Builder | ||
fmt.Fprintf(&builder, "failed to dial %s:", e.Peer) | ||
if e.Cause != nil { | ||
fmt.Fprintf(&builder, " %s", e.Cause) | ||
} | ||
for _, te := range e.Errors { | ||
fmt.Fprintf(&builder, "\n * [%s] %s", te.Address, te.Cause) | ||
} | ||
if e.Skipped > 0 { | ||
fmt.Fprintf(&builder, "\n ... skipping %d errors ...", e.Skipped) | ||
} | ||
return builder.String() | ||
} | ||
|
||
// Unwrap implements https://godoc.org/golang.org/x/xerrors#Wrapper. | ||
func (e *DialError) Unwrap() error { | ||
// If we have a context error, that's the "ultimate" error. | ||
if e.Cause != nil { | ||
return e.Cause | ||
} | ||
return nil | ||
} | ||
|
||
var _ error = (*DialError)(nil) | ||
|
||
// TransportError is the error returned when dialing a specific address. | ||
type TransportError struct { | ||
Address ma.Multiaddr | ||
Cause error | ||
} | ||
|
||
func (e *TransportError) Error() string { | ||
return fmt.Sprintf("failed to dial %s: %s", e.Address, e.Cause) | ||
} | ||
|
||
var _ error = (*TransportError)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters