2018-12-12 09:39:00 +00:00
|
|
|
package mailservers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-12-12 09:39:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewLastUsedConnectionMonitor returns pointer to the instance of LastUsedConnectionMonitor.
|
2020-01-20 20:56:06 +00:00
|
|
|
func NewLastUsedConnectionMonitor(ps *PeerStore, cache *Cache, eventSub EnvelopeEventSubscriber) *LastUsedConnectionMonitor {
|
2018-12-12 09:39:00 +00:00
|
|
|
return &LastUsedConnectionMonitor{
|
2020-01-20 20:56:06 +00:00
|
|
|
ps: ps,
|
|
|
|
cache: cache,
|
|
|
|
eventSub: eventSub,
|
2018-12-12 09:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastUsedConnectionMonitor watches relevant events and reflects it in cache.
|
|
|
|
type LastUsedConnectionMonitor struct {
|
|
|
|
ps *PeerStore
|
|
|
|
cache *Cache
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
eventSub EnvelopeEventSubscriber
|
2018-12-12 09:39:00 +00:00
|
|
|
|
|
|
|
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() {
|
2019-11-23 17:57:05 +00:00
|
|
|
events := make(chan types.EnvelopeEvent, whisperEventsBuffer)
|
2020-01-20 20:56:06 +00:00
|
|
|
sub := mon.eventSub.SubscribeEnvelopeEvents(events)
|
2018-12-12 09:39:00 +00:00
|
|
|
defer sub.Unsubscribe()
|
|
|
|
defer mon.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-mon.quit:
|
|
|
|
return
|
|
|
|
case err := <-sub.Err():
|
2020-01-20 20:56:06 +00:00
|
|
|
log.Error("retry after error suscribing to eventSub events", "error", err)
|
2018-12-12 09:39:00 +00:00
|
|
|
return
|
|
|
|
case ev := <-events:
|
|
|
|
node := mon.ps.Get(ev.Peer)
|
|
|
|
if node == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
if ev.Event == types.EventMailServerRequestCompleted {
|
2018-12-12 09:39:00 +00:00
|
|
|
err := mon.updateRecord(ev.Peer)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("unable to update storage", "peer", ev.Peer, "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (mon *LastUsedConnectionMonitor) updateRecord(nodeID types.EnodeID) error {
|
2018-12-12 09:39:00 +00:00
|
|
|
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
|
|
|
|
}
|