Networking code
This commit is contained in:
parent
6d69ca36a7
commit
dcf4fad971
28
peer.go
28
peer.go
|
@ -177,18 +177,7 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
|
||||||
|
|
||||||
// Set up the connection in another goroutine so we don't block the main thread
|
// Set up the connection in another goroutine so we don't block the main thread
|
||||||
go func() {
|
go func() {
|
||||||
var (
|
conn, err := p.Connect(addr)
|
||||||
err error
|
|
||||||
conn net.Conn
|
|
||||||
)
|
|
||||||
|
|
||||||
for attempts := 0; attempts < 5; attempts++ {
|
|
||||||
conn, err = net.DialTimeout("tcp", addr, 10*time.Second)
|
|
||||||
if err != nil {
|
|
||||||
peerlogger.Debugf("Peer connection failed. Retrying (%d/5)\n", attempts+1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
peerlogger.Debugln("Connection to peer failed. Giving up.", err)
|
peerlogger.Debugln("Connection to peer failed. Giving up.", err)
|
||||||
p.Stop()
|
p.Stop()
|
||||||
|
@ -206,6 +195,21 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *Peer) Connect(addr string) (conn net.Conn, err error) {
|
||||||
|
for attempts := 0; attempts < 5; attempts++ {
|
||||||
|
conn, err = net.DialTimeout("tcp", addr, 10*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
peerlogger.Debugf("Peer connection failed. Retrying (%d/5)\n", attempts+1)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Success
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
func (p *Peer) PingTime() string {
|
func (p *Peer) PingTime() string {
|
||||||
return p.pingTime.String()
|
return p.pingTime.String()
|
||||||
|
|
Loading…
Reference in New Issue