fix: OnNewPeer needs to use a wait group with OnNewPeerListen
This commit is contained in:
parent
a7f7963f1c
commit
2a0a817006
|
@ -18,6 +18,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -207,11 +208,16 @@ func (p *P2PImpl) ConnectToNode(connectionUris []*url.URL, retried bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
|
func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
|
||||||
challenge := protocol.GenerateChallenge()
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
challenge := protocol.GenerateChallenge()
|
||||||
peer.SetChallenge(challenge)
|
peer.SetChallenge(challenge)
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
p.OnNewPeerListen(peer, verifyId)
|
p.OnNewPeerListen(peer, verifyId)
|
||||||
|
}()
|
||||||
|
|
||||||
handshakeOpenMsg, err := protocol.NewHandshakeOpen(challenge, p.networkID).ToMessage()
|
handshakeOpenMsg, err := protocol.NewHandshakeOpen(challenge, p.networkID).ToMessage()
|
||||||
|
|
||||||
|
@ -223,6 +229,8 @@ func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wg.Wait() // Wait for OnNewPeerListen goroutine to finish
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {
|
func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {
|
||||||
|
|
Loading…
Reference in New Issue