Browse Source

some dial seeds fixes

pull/456/head
Ethan Buchman 8 years ago
committed by rigelrozanski
parent
commit
0e7baf027b
3 changed files with 22 additions and 53 deletions
  1. +15
    -2
      netaddress.go
  2. +7
    -41
      switch.go
  3. +0
    -10
      switch_test.go

+ 15
- 2
netaddress.go View File

@ -5,11 +5,12 @@
package p2p
import (
"errors"
"net"
"strconv"
"time"
. "github.com/tendermint/go-common"
cmn "github.com/tendermint/go-common"
)
type NetAddress struct {
@ -61,6 +62,18 @@ func NewNetAddressString(addr string) (*NetAddress, error) {
return na, nil
}
func NewNetAddressStrings(addrs []string) ([]*NetAddress, error) {
netAddrs := make([]*NetAddress, len(addrs))
for i, addr := range addrs {
netAddr, err := NewNetAddressString(addr)
if err != nil {
return nil, errors.New(cmn.Fmt("Error in address %s: %v", addr, err))
}
netAddrs[i] = netAddr
}
return netAddrs, nil
}
func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
na := &NetAddress{
IP: ip,
@ -85,7 +98,7 @@ func (na *NetAddress) Less(other interface{}) bool {
if o, ok := other.(*NetAddress); ok {
return na.String() < o.String()
} else {
PanicSanity("Cannot compare unequal types")
cmn.PanicSanity("Cannot compare unequal types")
return false
}
}


+ 7
- 41
switch.go View File

@ -296,58 +296,24 @@ func (sw *Switch) startInitPeer(peer *Peer) {
sw.addPeerToReactors(peer) // run AddPeer on each reactor
}
//error type for seed errors
type SeedError struct {
seed string
err error
}
type SeedErrors []SeedError
func (se SeedErrors) Error() string {
var str string
for _, e := range se {
str += ("seed: " + e.seed + " error: " + e.err.Error() + "; ")
}
return str
}
// Dial a list of seeds in random order
// Dial a list of seeds asynchronously in random order
func (sw *Switch) DialSeeds(seeds []string) error {
ch := make(chan SeedError) //channel for collecting errors
passing := 0 //number of passing seeds
netAddrs, err := NewNetAddressStrings(seeds)
if err != nil {
return err
}
// permute the list, dial them in random order.
perm := rand.Perm(len(seeds))
for i := 0; i < len(perm); i++ {
go func(i int) {
time.Sleep(time.Duration(rand.Int63n(3000)) * time.Millisecond)
j := perm[i]
addr, err := NewNetAddressString(seeds[j])
if err != nil {
ch <- SeedError{seeds[j], err}
} else {
sw.dialSeed(addr)
passing++
}
sw.dialSeed(netAddrs[j])
}(i)
}
//collect any errors from the channel
var seedErrs SeedErrors
for {
seedErr := <-ch
seedErrs = append(seedErrs, seedErr)
if len(seedErrs)+passing == len(perm) {
break
}
}
return seedErrs
return nil
}
func (sw *Switch) dialSeed(addr *NetAddress) {


+ 0
- 10
switch_test.go View File

@ -8,8 +8,6 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
. "github.com/tendermint/go-common"
cfg "github.com/tendermint/go-config"
"github.com/tendermint/go-crypto"
@ -121,14 +119,6 @@ func TestSwitches(t *testing.T) {
t.Errorf("Expected exactly 1 peer in s2, got %v", s2.Peers().Size())
}
//Test DialSeeds
err := s1.DialSeeds([]string{s1.NodeInfo().Network}) //"0.0.0.0:46658"})
assert.Nil(t, err, "expected successful dial seeds")
//Test Bad Dial Seeds
err = s1.DialSeeds([]string{"0.0.0:46658"})
assert.NotNil(t, err, "expected unsuccessful dial seeds")
// Lets send some messages
ch0Msg := "channel zero"
ch1Msg := "channel foo"


Loading…
Cancel
Save