-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net/mock: mimic Swarm's event and notification behavior in MockNet #2287
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/peerstore" | ||
bhost "github.com/libp2p/go-libp2p/p2p/host/basic" | ||
"github.com/libp2p/go-libp2p/p2p/host/eventbus" | ||
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
|
@@ -108,14 +109,16 @@ func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) { | |
} | ||
|
||
func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps peerstore.Peerstore) (host.Host, error) { | ||
n, err := newPeernet(mn, p, ps) | ||
bus := eventbus.NewBus() | ||
n, err := newPeernet(mn, p, ps, bus) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts := &bhost.HostOpts{ | ||
NegotiationTimeout: -1, | ||
DisableSignedPeerRecord: true, | ||
EventBus: bus, | ||
} | ||
|
||
h, err := bhost.NewHost(n, opts) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to use lightweight blankhost here isntead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally I'd prefer this to behave as similar to a normal node as possible. So I'd lean towards keeping this as the basic host. Is there something in basichost that's too heavy for you? I wonder if it would make more sense to make that lighter instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing specific, just wonderting. BasicHost also works |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,10 @@ import ( | |
"math/rand" | ||
"sync" | ||
|
||
"github.com/libp2p/go-libp2p/core/event" | ||
"github.com/libp2p/go-libp2p/core/network" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/peerstore" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
|
@@ -20,6 +20,7 @@ type peernet struct { | |
|
||
peer peer.ID | ||
ps peerstore.Peerstore | ||
emitter event.Emitter | ||
|
||
// conns are actual live connections between peers. | ||
// many conns could run over each link. | ||
|
@@ -37,11 +38,17 @@ type peernet struct { | |
} | ||
|
||
// newPeernet constructs a new peernet | ||
func newPeernet(m *mocknet, p peer.ID, ps peerstore.Peerstore) (*peernet, error) { | ||
func newPeernet(m *mocknet, p peer.ID, ps peerstore.Peerstore, bus event.Bus) (*peernet, error) { | ||
emitter, err := bus.Emitter(&event.EvtPeerConnectednessChanged{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
n := &peernet{ | ||
mocknet: m, | ||
peer: p, | ||
ps: ps, | ||
emitter: emitter, | ||
|
||
connsByPeer: map[peer.ID]map[*conn]struct{}{}, | ||
connsByLink: map[*link]map[*conn]struct{}{}, | ||
|
@@ -57,6 +64,7 @@ func (pn *peernet) Close() error { | |
for _, c := range pn.allConns() { | ||
c.Close() | ||
} | ||
pn.emitter.Close() | ||
return pn.ps.Close() | ||
} | ||
|
||
|
@@ -192,13 +200,16 @@ func (pn *peernet) addConn(c *conn) { | |
pn.notifyAll(func(n network.Notifiee) { | ||
n.Connected(pn, c) | ||
}) | ||
|
||
pn.emitter.Emit(event.EvtPeerConnectednessChanged{ | ||
Peer: c.remote, | ||
Connectedness: network.Connected, | ||
}) | ||
} | ||
|
||
// removeConn removes a given conn | ||
func (pn *peernet) removeConn(c *conn) { | ||
pn.Lock() | ||
defer pn.Unlock() | ||
|
||
cs, found := pn.connsByLink[c.link] | ||
if !found || len(cs) < 1 { | ||
panic(fmt.Sprintf("attempting to remove a conn that doesnt exist %p", c.link)) | ||
|
@@ -210,6 +221,22 @@ func (pn *peernet) removeConn(c *conn) { | |
panic(fmt.Sprintf("attempting to remove a conn that doesnt exist %v", c.remote)) | ||
} | ||
delete(cs, c) | ||
pn.Unlock() | ||
|
||
// notify asynchronously to mimic Swarm | ||
// FIXME: IIRC, we wanted to make notify for Close synchronous | ||
Comment on lines
+226
to
+227
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
go func() { | ||
c.notifLk.Lock() | ||
defer c.notifLk.Unlock() | ||
pn.notifyAll(func(n network.Notifiee) { | ||
n.Disconnected(c.net, c) | ||
}) | ||
}() | ||
|
||
c.net.emitter.Emit(event.EvtPeerConnectednessChanged{ | ||
Peer: c.remote, | ||
Connectedness: network.NotConnected, | ||
}) | ||
} | ||
|
||
// LocalPeer the network's LocalPeer | ||
|
@@ -355,18 +382,11 @@ func (pn *peernet) StopNotify(f network.Notifiee) { | |
// notifyAll runs the notification function on all Notifiees | ||
func (pn *peernet) notifyAll(notification func(f network.Notifiee)) { | ||
pn.notifmu.Lock() | ||
var wg sync.WaitGroup | ||
// notify synchronously to mimic Swarm | ||
for n := range pn.notifs { | ||
// make sure we dont block | ||
// and they dont block each other. | ||
wg.Add(1) | ||
go func(n network.Notifiee) { | ||
defer wg.Done() | ||
notification(n) | ||
}(n) | ||
notification(n) | ||
} | ||
pn.notifmu.Unlock() | ||
wg.Wait() | ||
} | ||
|
||
func (pn *peernet) ResourceManager() network.ResourceManager { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved for nicer logic grouping.