-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,19 +7,20 @@ 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" | ||
) | ||
|
||
// peernet implements network.Network | ||
type peernet struct { | ||
mocknet *mocknet // parent | ||
|
||
peer peer.ID | ||
ps peerstore.Peerstore | ||
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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.