fix: dial peer async

This commit is contained in:
Richard Ramos 2021-09-30 19:17:17 -04:00
parent 51493d61bd
commit 6bb04e0858
5 changed files with 51 additions and 31 deletions

2
go.mod
View File

@ -47,7 +47,7 @@ require (
github.com/russolsen/same v0.0.0-20160222130632-f089df61f51d // indirect
github.com/russolsen/transit v0.0.0-20180705123435-0794b4c4505a
github.com/status-im/doubleratchet v3.0.0+incompatible
github.com/status-im/go-waku v0.0.0-20210927124718-6c4a74fb9cbf
github.com/status-im/go-waku v0.0.0-20210930231040-ecfdec1b0a3b
github.com/status-im/go-wakurelay-pubsub v0.4.3-0.20210729162817-adc68830282a
github.com/status-im/markdown v0.0.0-20201022101546-c0cbdd5763bf
github.com/status-im/migrate/v4 v4.6.2-status.2

4
go.sum
View File

@ -1102,8 +1102,8 @@ github.com/status-im/go-ethereum v1.10.4-status.3 h1:RF618iSCvqJtXu3ZSg7XNg6MJaS
github.com/status-im/go-ethereum v1.10.4-status.3/go.mod h1:GvIhpdCOgMHI6i5xVPEZOrv/qSMeOFHbZh77AoyZUoE=
github.com/status-im/go-multiaddr-ethv4 v1.2.0 h1:OT84UsUzTCwguqCpJqkrCMiL4VZ1SvUtH9a5MsZupBk=
github.com/status-im/go-multiaddr-ethv4 v1.2.0/go.mod h1:2VQ3C+9zEurcceasz12gPAtmEzCeyLUGPeKLSXYQKHo=
github.com/status-im/go-waku v0.0.0-20210927124718-6c4a74fb9cbf h1:r4TenmNYnine3l1qGFOQW74s+27M0HlLipQxxZ3PJbI=
github.com/status-im/go-waku v0.0.0-20210927124718-6c4a74fb9cbf/go.mod h1:XK6wGIMnxhpx9SQLDV9Qw0zfXTjd8jjw6DXGC0mKvA8=
github.com/status-im/go-waku v0.0.0-20210930231040-ecfdec1b0a3b h1:xh15zw5FWa/hg6COzB+3vfND07KxjW68MW7H66q9Fec=
github.com/status-im/go-waku v0.0.0-20210930231040-ecfdec1b0a3b/go.mod h1:PnUjF7K8KFennkcijb+DEoM+nDgudGmRd/C6CaEZZGg=
github.com/status-im/go-wakurelay-pubsub v0.4.3-0.20210729162817-adc68830282a h1:eCna/q/PuZVqtmOMBqytw9yzZwMNKpao4au0OJDvesI=
github.com/status-im/go-wakurelay-pubsub v0.4.3-0.20210729162817-adc68830282a/go.mod h1:LSCVYR7mnBBsxVJghrGpQ3yJAAATEe6XeQQqGCZhwrE=
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q=

View File

@ -352,11 +352,11 @@ func (w *WakuNode) HasHistory() bool {
return false
}
func (w *WakuNode) ListenAddresses() []string {
func (w *WakuNode) ListenAddresses() []ma.Multiaddr {
hostInfo, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", w.host.ID().Pretty()))
var result []string
var result []ma.Multiaddr
for _, addr := range w.host.Addrs() {
result = append(result, addr.Encapsulate(hostInfo).String())
result = append(result, addr.Encapsulate(hostInfo))
}
return result
}
@ -690,7 +690,16 @@ func (node *WakuNode) LightPush(ctx context.Context, message *pb.WakuMessage, to
}
}
func (w *WakuNode) DialPeer(address string) error {
func (w *WakuNode) DialPeerWithMultiAddress(ctx context.Context, address ma.Multiaddr) error {
info, err := peer.AddrInfoFromP2pAddr(address)
if err != nil {
return err
}
return w.connect(ctx, *info)
}
func (w *WakuNode) DialPeer(ctx context.Context, address string) error {
p, err := ma.NewMultiaddr(address)
if err != nil {
return err
@ -701,11 +710,11 @@ func (w *WakuNode) DialPeer(address string) error {
return err
}
return w.connect(*info)
return w.connect(ctx, *info)
}
func (w *WakuNode) connect(info peer.AddrInfo) error {
err := w.host.Connect(w.ctx, info)
func (w *WakuNode) connect(ctx context.Context, info peer.AddrInfo) error {
err := w.host.Connect(ctx, info)
if err != nil {
return err
}
@ -718,9 +727,9 @@ func (w *WakuNode) connect(info peer.AddrInfo) error {
return nil
}
func (w *WakuNode) DialPeerByID(peerID peer.ID) error {
func (w *WakuNode) DialPeerByID(ctx context.Context, peerID peer.ID) error {
info := w.host.Peerstore().PeerInfo(peerID)
return w.connect(info)
return w.connect(ctx, info)
}
func (w *WakuNode) ClosePeerByAddress(address string) error {
@ -802,18 +811,18 @@ func (w *WakuNode) startKeepAlive(t time.Duration) {
peerMap[p] = result
mu.Unlock()
go func(peer peer.ID) {
go func(peerID peer.ID) {
peerFound := false
w.peersMutex.Lock()
for p := range w.peers {
if p == peer {
if p == peerID {
peerFound = true
break
}
}
defer w.peersMutex.Unlock()
log.Debug("###PING before fetching result")
pingTicker := time.NewTicker(time.Duration(1) * time.Second)
isError := false
select {
@ -826,28 +835,30 @@ func (w *WakuNode) startKeepAlive(t time.Duration) {
if !peerFound && !isError {
//EventBus Emitter doesn't seem to work when there's no connection
w.pingEventsChan <- event.EvtPeerConnectednessChanged{
Peer: peer,
Peer: peerID,
Connectedness: network.Connected,
}
peerConns := w.host.Network().ConnsToPeer(peer)
peerConns := w.host.Network().ConnsToPeer(peerID)
if len(peerConns) > 0 {
// log.Info("###PING " + s + " IdentifyWait")
// logwriter.Write([]byte("###PING " + s + " IdentifyWait"))
//w.idService.IdentifyWait(peerConns[0])
} else {
w.DialPeerByID(peer)
go func(peerID peer.ID) {
ctx, cancel := context.WithTimeout(w.ctx, time.Duration(5)*time.Second)
defer cancel()
w.DialPeerByID(ctx, peerID)
}(peerID)
}
} else if peerFound && isError {
// log.Info("###PING " + s + " peer removed")
// logwriter.Write([]byte("###PING " + s + " peer removed"))
w.pingEventsChan <- event.EvtPeerConnectednessChanged{
Peer: peer,
Peer: peerID,
Connectedness: network.NotConnected,
}
}
mu.Lock()
delete(peerMap, peer)
delete(peerMap, peerID)
mu.Unlock()
}(p)
} else {

2
vendor/modules.txt vendored
View File

@ -426,7 +426,7 @@ github.com/spacemonkeygo/spacelog
github.com/status-im/doubleratchet
# github.com/status-im/go-multiaddr-ethv4 v1.2.0
github.com/status-im/go-multiaddr-ethv4
# github.com/status-im/go-waku v0.0.0-20210927124718-6c4a74fb9cbf
# github.com/status-im/go-waku v0.0.0-20210930231040-ecfdec1b0a3b
github.com/status-im/go-waku/waku/v2/metrics
github.com/status-im/go-waku/waku/v2/node
github.com/status-im/go-waku/waku/v2/protocol

View File

@ -62,6 +62,7 @@ import (
)
const messageQueueLimit = 1024
const requestTimeout = 5 * time.Second
type settings struct {
LightClient bool // Indicates if the node is a light client
@ -219,12 +220,16 @@ func New(nodeKey string, cfg *Config, logger *zap.Logger) (*Waku, error) {
func (w *Waku) addPeers(cfg *Config) {
if !cfg.LightClient {
for _, relaynode := range cfg.RelayNodes {
err := w.node.DialPeer(relaynode)
if err != nil {
log.Warn("could not dial peer", err)
} else {
log.Info("relay peer dialed successfully", relaynode)
}
go func(node string) {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
err := w.node.DialPeer(ctx, node)
if err != nil {
log.Warn("could not dial peer", err)
} else {
log.Info("relay peer dialed successfully", node)
}
}(relaynode)
}
}
@ -846,11 +851,15 @@ func (w *Waku) AddRelayPeer(address string) (string, error) {
}
func (w *Waku) DialPeer(address string) error {
return w.node.DialPeer(address)
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
return w.node.DialPeer(ctx, address)
}
func (w *Waku) DialPeerByID(peerID string) error {
return w.node.DialPeerByID(peer.ID(peerID))
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
return w.node.DialPeerByID(ctx, peer.ID(peerID))
}
func (w *Waku) DropPeer(peerID string) error {