mirror of
https://github.com/status-im/status-go.git
synced 2025-01-16 01:35:10 +00:00
38308d48f2
* feat_: log error and stacktrace when panic in goroutine * test_: add test TestSafeGo * chore_: rename logAndCall to call * chore_: rename SafeGo to Go * chore_: make lint-fix * chore_: use t.Cleanup * chore_: Revert "chore_: use t.Cleanup" This reverts commit 4eb420d179cc0e208e84c13cb941e6b3d1ed9819. * chore_: Revert "chore_: make lint-fix" This reverts commit fcc995f157e671a4229b47419c3a0e4004b5fdab. * chore_: Revert "chore_: rename SafeGo to Go" This reverts commit a6d73d6df583f313032d79aac62f66328039cb55. * chore_: Revert "chore_: rename logAndCall to call" This reverts commit 8fbe993bedb9fbba67349a44f151e2dd5e3bc4cc. * chore_: Revert "test_: add test TestSafeGo" This reverts commit a1fa91839f3960398980c6bf456e6462ec944819. * chore_: Revert "feat_: log error and stacktrace when panic in goroutine" This reverts commit f612dd828fa2ce410d0e806fe773ecbe3e86a68a. * feat_: log error and stacktrace when panic in goroutine * chore_: make lint-fix * chore_: rename logAndCall to call * chore_: renaming LogOnPanic * chore_: update rest goroutine function calls * chore_: make lint-fix
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package helpers
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
"github.com/status-im/status-go/common"
|
|
)
|
|
|
|
var (
|
|
// ErrNoRunningNode node is not running.
|
|
ErrNoRunningNode = errors.New("there is no running node")
|
|
// ErrEmptyPeerURL provided peer URL is empty
|
|
ErrEmptyPeerURL = errors.New("empty peer url")
|
|
)
|
|
|
|
// waitForPeer waits for a peer to be added
|
|
func waitForPeer(p *p2p.Server, u string, e p2p.PeerEventType, t time.Duration, subscribed chan struct{}) error {
|
|
if p == nil {
|
|
return ErrNoRunningNode
|
|
}
|
|
if u == "" {
|
|
return ErrEmptyPeerURL
|
|
}
|
|
parsedPeer, err := enode.ParseV4(u)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ch := make(chan *p2p.PeerEvent)
|
|
subscription := p.SubscribeEvents(ch)
|
|
defer subscription.Unsubscribe()
|
|
close(subscribed)
|
|
|
|
for {
|
|
select {
|
|
case ev := <-ch:
|
|
if ev.Type == e && ev.Peer == parsedPeer.ID() {
|
|
return nil
|
|
}
|
|
case err := <-subscription.Err():
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case <-time.After(t):
|
|
return errors.New("wait for peer: timeout")
|
|
}
|
|
}
|
|
}
|
|
|
|
// WaitForPeerAsync waits for a peer to be added asynchronously
|
|
func WaitForPeerAsync(p *p2p.Server, u string, e p2p.PeerEventType, t time.Duration) <-chan error {
|
|
subscribed := make(chan struct{})
|
|
errCh := make(chan error)
|
|
go func() {
|
|
defer common.LogOnPanic()
|
|
errCh <- waitForPeer(p, u, e, t, subscribed)
|
|
}()
|
|
<-subscribed
|
|
return errCh
|
|
}
|