mirror of
https://github.com/status-im/status-go.git
synced 2025-01-31 00:48:01 +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
88 lines
2.0 KiB
Go
88 lines
2.0 KiB
Go
package mailservers
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/status-im/status-go/common"
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
)
|
|
|
|
// NewLastUsedConnectionMonitor returns pointer to the instance of LastUsedConnectionMonitor.
|
|
func NewLastUsedConnectionMonitor(ps *PeerStore, cache *Cache, eventSub EnvelopeEventSubscriber) *LastUsedConnectionMonitor {
|
|
return &LastUsedConnectionMonitor{
|
|
ps: ps,
|
|
cache: cache,
|
|
eventSub: eventSub,
|
|
}
|
|
}
|
|
|
|
// LastUsedConnectionMonitor watches relevant events and reflects it in cache.
|
|
type LastUsedConnectionMonitor struct {
|
|
ps *PeerStore
|
|
cache *Cache
|
|
|
|
eventSub EnvelopeEventSubscriber
|
|
|
|
quit chan struct{}
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
// Start spins a separate goroutine to watch connections.
|
|
func (mon *LastUsedConnectionMonitor) Start() {
|
|
mon.quit = make(chan struct{})
|
|
mon.wg.Add(1)
|
|
go func() {
|
|
defer common.LogOnPanic()
|
|
events := make(chan types.EnvelopeEvent, whisperEventsBuffer)
|
|
sub := mon.eventSub.SubscribeEnvelopeEvents(events)
|
|
defer sub.Unsubscribe()
|
|
defer mon.wg.Done()
|
|
for {
|
|
select {
|
|
case <-mon.quit:
|
|
return
|
|
case err := <-sub.Err():
|
|
log.Error("retry after error suscribing to eventSub events", "error", err)
|
|
return
|
|
case ev := <-events:
|
|
node := mon.ps.Get(ev.Peer)
|
|
if node == nil {
|
|
continue
|
|
}
|
|
if ev.Event == types.EventMailServerRequestCompleted {
|
|
err := mon.updateRecord(ev.Peer)
|
|
if err != nil {
|
|
log.Error("unable to update storage", "peer", ev.Peer, "error", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (mon *LastUsedConnectionMonitor) updateRecord(nodeID types.EnodeID) error {
|
|
node := mon.ps.Get(nodeID)
|
|
if node == nil {
|
|
return nil
|
|
}
|
|
return mon.cache.UpdateRecord(PeerRecord{node: node, LastUsed: time.Now()})
|
|
}
|
|
|
|
// Stop closes channel to signal a quit and waits until all goroutines are stoppped.
|
|
func (mon *LastUsedConnectionMonitor) Stop() {
|
|
if mon.quit == nil {
|
|
return
|
|
}
|
|
select {
|
|
case <-mon.quit:
|
|
return
|
|
default:
|
|
}
|
|
close(mon.quit)
|
|
mon.wg.Wait()
|
|
mon.quit = nil
|
|
}
|