mirror of
https://github.com/status-im/status-go.git
synced 2025-01-12 15:45:07 +00:00
f2c6fef64c
This change allows to connect to the mail server that we were using before the app was restarted. Separate loop is listening for whisper events, and when we receive event that request was completed we will update time on a peer record. Records are stored in leveldb. Body of the record is marshaled using json. At this point the only field is a timestamp when record was used. This loop doesn't control connections, it only tracks what mail server we ended up using. It works asynchronously to connection management loop. Which tracks events that are related to connection state and expiry of the requests. When app starts we look into the database and select the most recently used record. This record is added to connection management loop first. So if this server is available we will stick to using it. If we weren't able to connect to the same server in configured timeout (5s) we will try to connect to any other server from list of active servers. closes: #1285
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package mailservers
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
)
|
|
|
|
var (
|
|
// ErrNoConnected returned when mail servers are not connected.
|
|
ErrNoConnected = errors.New("no connected mail servers")
|
|
)
|
|
|
|
// PeersProvider is an interface for requesting list of peers.
|
|
type PeersProvider interface {
|
|
Peers() []*p2p.Peer
|
|
}
|
|
|
|
// NewPeerStore returns an instance of PeerStore.
|
|
func NewPeerStore(cache *Cache) *PeerStore {
|
|
return &PeerStore{
|
|
nodes: map[enode.ID]*enode.Node{},
|
|
cache: cache,
|
|
}
|
|
}
|
|
|
|
// PeerStore stores list of selected mail servers and keeps N of them connected.
|
|
type PeerStore struct {
|
|
mu sync.RWMutex
|
|
nodes map[enode.ID]*enode.Node
|
|
|
|
cache *Cache
|
|
}
|
|
|
|
// Exist confirms that peers was added to a store.
|
|
func (ps *PeerStore) Exist(nodeID enode.ID) bool {
|
|
ps.mu.RLock()
|
|
defer ps.mu.RUnlock()
|
|
_, exist := ps.nodes[nodeID]
|
|
return exist
|
|
}
|
|
|
|
// Get returns instance of the node with requested ID or nil if ID is not found.
|
|
func (ps *PeerStore) Get(nodeID enode.ID) *enode.Node {
|
|
ps.mu.RLock()
|
|
defer ps.mu.RUnlock()
|
|
return ps.nodes[nodeID]
|
|
}
|
|
|
|
// Update updates peers locally.
|
|
func (ps *PeerStore) Update(nodes []*enode.Node) error {
|
|
ps.mu.Lock()
|
|
ps.nodes = map[enode.ID]*enode.Node{}
|
|
for _, n := range nodes {
|
|
ps.nodes[n.ID()] = n
|
|
}
|
|
ps.mu.Unlock()
|
|
return ps.cache.Replace(nodes)
|
|
}
|