mirror of
https://github.com/status-im/status-go.git
synced 2025-01-14 00:36:40 +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
135 lines
3.1 KiB
Go
135 lines
3.1 KiB
Go
package discovery
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
|
"github.com/status-im/status-go/common"
|
|
)
|
|
|
|
// NewMultiplexer creates Multiplexer instance.
|
|
func NewMultiplexer(discoveries []Discovery) Multiplexer {
|
|
return Multiplexer{discoveries}
|
|
}
|
|
|
|
// Multiplexer allows to use multiple discoveries behind single Discovery interface.
|
|
type Multiplexer struct {
|
|
discoveries []Discovery
|
|
}
|
|
|
|
// Running should return true if at least one discovery is running
|
|
func (m Multiplexer) Running() (rst bool) {
|
|
for i := range m.discoveries {
|
|
rst = rst || m.discoveries[i].Running()
|
|
}
|
|
return rst
|
|
}
|
|
|
|
// Start every discovery and stop every started in case if at least one fails.
|
|
func (m Multiplexer) Start() (err error) {
|
|
started := []int{}
|
|
for i := range m.discoveries {
|
|
if err = m.discoveries[i].Start(); err != nil {
|
|
break
|
|
}
|
|
started = append(started, i)
|
|
}
|
|
if err != nil {
|
|
for _, i := range started {
|
|
_ = m.discoveries[i].Stop()
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Stop every discovery.
|
|
func (m Multiplexer) Stop() (err error) {
|
|
messages := []string{}
|
|
for i := range m.discoveries {
|
|
if err = m.discoveries[i].Stop(); err != nil {
|
|
messages = append(messages, err.Error())
|
|
}
|
|
}
|
|
if len(messages) != 0 {
|
|
return fmt.Errorf("failed to stop discoveries: %s", strings.Join(messages, "; "))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Register passed topic and stop channel to every discovery and waits till it will return.
|
|
func (m Multiplexer) Register(topic string, stop chan struct{}) error {
|
|
errors := make(chan error, len(m.discoveries))
|
|
for i := range m.discoveries {
|
|
i := i
|
|
go func() {
|
|
defer common.LogOnPanic()
|
|
errors <- m.discoveries[i].Register(topic, stop)
|
|
}()
|
|
}
|
|
total := 0
|
|
messages := []string{}
|
|
for err := range errors {
|
|
total++
|
|
if err != nil {
|
|
messages = append(messages, err.Error())
|
|
}
|
|
if total == len(m.discoveries) {
|
|
break
|
|
}
|
|
}
|
|
if len(messages) != 0 {
|
|
return fmt.Errorf("failed to register %s: %s", topic, strings.Join(messages, "; "))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Discover shares topic and channles for receiving results. And multiplexer periods that are sent to period channel.
|
|
func (m Multiplexer) Discover(topic string, period <-chan time.Duration, found chan<- *discv5.Node, lookup chan<- bool) error {
|
|
var (
|
|
periods = make([]chan time.Duration, len(m.discoveries))
|
|
messages = []string{}
|
|
wg sync.WaitGroup
|
|
mu sync.Mutex
|
|
)
|
|
wg.Add(len(m.discoveries) + 1)
|
|
for i := range m.discoveries {
|
|
i := i
|
|
periods[i] = make(chan time.Duration, 2)
|
|
go func() {
|
|
defer common.LogOnPanic()
|
|
err := m.discoveries[i].Discover(topic, periods[i], found, lookup)
|
|
if err != nil {
|
|
mu.Lock()
|
|
messages = append(messages, err.Error())
|
|
mu.Unlock()
|
|
}
|
|
wg.Done()
|
|
}()
|
|
}
|
|
go func() {
|
|
defer common.LogOnPanic()
|
|
for {
|
|
newPeriod, ok := <-period
|
|
for i := range periods {
|
|
if !ok {
|
|
close(periods[i])
|
|
} else {
|
|
periods[i] <- newPeriod
|
|
}
|
|
}
|
|
if !ok {
|
|
wg.Done()
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
wg.Wait()
|
|
if len(messages) != 0 {
|
|
return fmt.Errorf("failed to discover topic %s: %s", topic, strings.Join(messages, "; "))
|
|
}
|
|
return nil
|
|
}
|