mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
Move Subscribe function from StreamerPeer to Streamer
We manage a map in Streamer from the peer nodeids to the StreamerPeer instances. Subscribe is on StreamerPeer and receives a peer nodeId
This commit is contained in:
parent
01fd455d6b
commit
debf3f6932
2 changed files with 35 additions and 10 deletions
|
|
@ -127,12 +127,14 @@ func (self WantedKeysMsg) String() string {
|
||||||
type Streamer struct {
|
type Streamer struct {
|
||||||
incomingLock sync.RWMutex
|
incomingLock sync.RWMutex
|
||||||
outgoingLock sync.RWMutex
|
outgoingLock sync.RWMutex
|
||||||
|
peersLock sync.RWMutex
|
||||||
outgoing map[string]func(*StreamerPeer, []byte) (OutgoingStreamer, error)
|
outgoing map[string]func(*StreamerPeer, []byte) (OutgoingStreamer, error)
|
||||||
incoming map[string]func(*StreamerPeer, []byte) (IncomingStreamer, error)
|
incoming map[string]func(*StreamerPeer, []byte) (IncomingStreamer, error)
|
||||||
|
|
||||||
dbAccess *DbAccess
|
dbAccess *DbAccess
|
||||||
overlay Overlay
|
overlay Overlay
|
||||||
receiveC chan *ChunkDeliveryMsg
|
receiveC chan *ChunkDeliveryMsg
|
||||||
|
peers map[discover.NodeID]*StreamerPeer
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStreamer is Streamer constructor
|
// NewStreamer is Streamer constructor
|
||||||
|
|
@ -143,6 +145,7 @@ func NewStreamer(overlay Overlay, dbAccess *DbAccess) *Streamer {
|
||||||
dbAccess: dbAccess,
|
dbAccess: dbAccess,
|
||||||
overlay: overlay,
|
overlay: overlay,
|
||||||
receiveC: make(chan *ChunkDeliveryMsg, 10),
|
receiveC: make(chan *ChunkDeliveryMsg, 10),
|
||||||
|
peers: make(map[discover.NodeID]*StreamerPeer),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -235,6 +238,7 @@ type StreamerPeer struct {
|
||||||
// NewStreamerPeer is the constructor for StreamerPeer
|
// NewStreamerPeer is the constructor for StreamerPeer
|
||||||
func NewStreamerPeer(p Peer, streamer *Streamer) *StreamerPeer {
|
func NewStreamerPeer(p Peer, streamer *Streamer) *StreamerPeer {
|
||||||
self := &StreamerPeer{
|
self := &StreamerPeer{
|
||||||
|
Peer: p,
|
||||||
pq: pq.New(int(PriorityQueue), PriorityQueueCap),
|
pq: pq.New(int(PriorityQueue), PriorityQueueCap),
|
||||||
streamer: streamer,
|
streamer: streamer,
|
||||||
outgoing: make(map[string]*outgoingStreamer),
|
outgoing: make(map[string]*outgoingStreamer),
|
||||||
|
|
@ -416,16 +420,24 @@ func (self *incomingStreamer) nextBatch(from uint64) (nextFrom uint64, nextTo ui
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe initiates the streamer
|
// Subscribe initiates the streamer
|
||||||
func (self *StreamerPeer) Subscribe(s string, t []byte, from, to uint64, priority uint8, live bool) error {
|
func (self *Streamer) Subscribe(peerId discover.NodeID, s string, t []byte, from, to uint64, priority uint8, live bool) error {
|
||||||
f, err := self.streamer.GetIncomingStreamer(s)
|
f, err := self.GetIncomingStreamer(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
is, err := f(self, t)
|
|
||||||
|
self.peersLock.RLock()
|
||||||
|
peer := self.peers[peerId]
|
||||||
|
self.peersLock.RUnlock()
|
||||||
|
if peer == nil {
|
||||||
|
return fmt.Errorf("peer not found %v", peerId)
|
||||||
|
}
|
||||||
|
|
||||||
|
is, err := f(peer, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = self.setIncomingStreamer(s, is, priority, live)
|
err = peer.setIncomingStreamer(s, is, priority, live)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -437,7 +449,7 @@ func (self *StreamerPeer) Subscribe(s string, t []byte, from, to uint64, priorit
|
||||||
To: to,
|
To: to,
|
||||||
Priority: priority,
|
Priority: priority,
|
||||||
}
|
}
|
||||||
self.SendPriority(msg, priority)
|
peer.SendPriority(msg, priority)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -617,7 +629,18 @@ func (s *Streamer) Run(p *bzzPeer) error {
|
||||||
// Priority: uint8(Top),
|
// Priority: uint8(Top),
|
||||||
// })
|
// })
|
||||||
// subscribe to request handling ; only with non-light nodes
|
// subscribe to request handling ; only with non-light nodes
|
||||||
sp.Subscribe(retrieveRequestStream, nil, 0, 0, Top, true)
|
|
||||||
|
s.peersLock.Lock()
|
||||||
|
s.peers[sp.ID()] = sp
|
||||||
|
s.peersLock.Unlock()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
s.peersLock.Lock()
|
||||||
|
delete(s.peers, sp.ID())
|
||||||
|
s.peersLock.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
s.Subscribe(sp.ID(), retrieveRequestStream, nil, 0, 0, Top, true)
|
||||||
defer close(sp.quit)
|
defer close(sp.quit)
|
||||||
return sp.Run(sp.HandleMsg)
|
return sp.Run(sp.HandleMsg)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
||||||
"github.com/ethereum/go-ethereum/swarm/storage"
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -199,20 +200,21 @@ func parseSyncLabel(t []byte) (string, uint8) {
|
||||||
|
|
||||||
// StartSyncing is called on the StreamerPeer to start the syncing process
|
// StartSyncing is called on the StreamerPeer to start the syncing process
|
||||||
// the idea is that it is called only after kademlia is close to healthy
|
// the idea is that it is called only after kademlia is close to healthy
|
||||||
func StartSyncing(s *StreamerPeer, po uint8, nn bool) {
|
func StartSyncing(s *Streamer, peerId discover.NodeID, po uint8, nn bool) {
|
||||||
lastPO := po
|
lastPO := po
|
||||||
if nn {
|
if nn {
|
||||||
lastPO = maxPO
|
lastPO = maxPO
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := po; i <= lastPO; i++ {
|
for i := po; i <= lastPO; i++ {
|
||||||
s.Subscribe("SYNC", newSyncLabel("LIVE", po), 0, 0, High, true)
|
s.Subscribe(peerId, "SYNC", newSyncLabel("LIVE", po), 0, 0, High, true)
|
||||||
s.Subscribe("SYNC", newSyncLabel("HISTORY", po), 0, 0, Mid, false)
|
s.Subscribe(peerId, "SYNC", newSyncLabel("HISTORY", po), 0, 0, Mid, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterIncomingSyncers(streamer *Streamer, db *DbAccess) {
|
func RegisterIncomingSyncers(streamer *Streamer, db *DbAccess) {
|
||||||
streamer.RegisterIncomingStreamer("SYNC", func(p *StreamerPeer, t []byte) (IncomingStreamer, error) {
|
streamer.RegisterIncomingStreamer("SYNC", func(p *StreamerPeer, t []byte) (IncomingStreamer, error) {
|
||||||
syncType, po := parseSyncLabel(t)
|
syncType, _ := parseSyncLabel(t)
|
||||||
switch syncType {
|
switch syncType {
|
||||||
case "LIVE":
|
case "LIVE":
|
||||||
return NewIncomingSwarmSyncer(p, nil, nil)
|
return NewIncomingSwarmSyncer(p, nil, nil)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue