mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 07:06:42 +00:00
add peerpool
This commit is contained in:
parent
7f22abaead
commit
a6a006c5e3
2 changed files with 54 additions and 1 deletions
53
p2p/protocols/peerpool.go
Normal file
53
p2p/protocols/peerpool.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package protocols
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
|
||||
"github.com/ethereum/go-ethereum/p2p/adapters"
|
||||
"github.com/ethereum/go-ethereum/p2p/discover"
|
||||
)
|
||||
|
||||
type TestPeer interface {
|
||||
ID() discover.NodeID
|
||||
Drop(error)
|
||||
}
|
||||
|
||||
// TestPeerPool is an example peerPool to demonstrate registration of peer connections
|
||||
type TestPeerPool struct {
|
||||
lock sync.Mutex
|
||||
peers map[discover.NodeID]TestPeer
|
||||
}
|
||||
|
||||
func NewTestPeerPool() *TestPeerPool {
|
||||
return &TestPeerPool{peers: make(map[discover.NodeID]TestPeer)}
|
||||
}
|
||||
|
||||
func (self *TestPeerPool) Add(p TestPeer) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
glog.V(logger.Detail).Infof("pp add peer %v", p.ID())
|
||||
self.peers[p.ID()] = p
|
||||
|
||||
}
|
||||
|
||||
func (self *TestPeerPool) Remove(p TestPeer) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
delete(self.peers, p.ID())
|
||||
}
|
||||
|
||||
func (self *TestPeerPool) Has(n *adapters.NodeId) bool {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
_, ok := self.peers[n.NodeID]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (self *TestPeerPool) Get(n *adapters.NodeId) TestPeer {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
return self.peers[n.NodeID]
|
||||
}
|
||||
|
|
@ -110,7 +110,7 @@ func newBzzTester(t *testing.T, addr *peerAddr, pp PeerPool, ct *protocols.CodeM
|
|||
p.Register(&protocols.Disconnect{}, func(e interface{}) error { pp.Remove(p) })
|
||||
return services(p)
|
||||
}
|
||||
s := p2ptest.NewProtocolTester(t, NodeId(addr), 1, newTestBzzProtocol(addr, pp, ct, extarservices))
|
||||
s := p2ptest.NewProtocolTester(t, NodeId(addr), 1, newTestBzzProtocol(addr, pp, ct, extraservices))
|
||||
return &bzzTester{
|
||||
addr: addr,
|
||||
// flushCode: 4,
|
||||
|
|
|
|||
Loading…
Reference in a new issue