-
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
WIP: Feat/memory transport #3022
Open
pyropy
wants to merge
13
commits into
libp2p:master
Choose a base branch
from
pyropy:feat/memory-transport
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+998
−32
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
39983c2
Add memory transport
pyropy 37899d3
Daily commit
pyropy 0df38f9
Daily commit
pyropy 67da192
Upgrade go-multiaddr
pyropy e2a5865
Daily commit
pyropy 079bd3e
Use plain channels to send data between streams
pyropy e3203f2
Daily commit
pyropy 4164b48
Merge branch 'master' into feat/memory-transport
pyropy b96f386
Daily commit
pyropy 5236ff2
Daily commit
pyropy f511c2d
Revert to stream using channels instead of io.Pipe
pyropy 35ebf85
Update errors when remote stream is closed
pyropy 2e3378f
Add read and write timeouts
pyropy 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
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 |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package memory | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"sync" | ||
"sync/atomic" | ||
|
||
ic "github.com/libp2p/go-libp2p/core/crypto" | ||
"github.com/libp2p/go-libp2p/core/network" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
tpt "github.com/libp2p/go-libp2p/core/transport" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
type conn struct { | ||
id int64 | ||
rconn *conn | ||
|
||
scope network.ConnManagementScope | ||
listener *listener | ||
transport *transport | ||
|
||
localPeer peer.ID | ||
localMultiaddr ma.Multiaddr | ||
|
||
remotePeerID peer.ID | ||
remotePubKey ic.PubKey | ||
remoteMultiaddr ma.Multiaddr | ||
|
||
mu sync.Mutex | ||
|
||
closed atomic.Bool | ||
closeOnce sync.Once | ||
|
||
streamC chan *stream | ||
streams map[int64]network.MuxedStream | ||
} | ||
|
||
var _ tpt.CapableConn = &conn{} | ||
|
||
func newConnection( | ||
t *transport, | ||
s *stream, | ||
localPeer peer.ID, | ||
localMultiaddr ma.Multiaddr, | ||
remotePubKey ic.PubKey, | ||
remotePeer peer.ID, | ||
remoteMultiaddr ma.Multiaddr, | ||
) *conn { | ||
c := &conn{ | ||
id: connCounter.Add(1), | ||
transport: t, | ||
localPeer: localPeer, | ||
localMultiaddr: localMultiaddr, | ||
remotePubKey: remotePubKey, | ||
remotePeerID: remotePeer, | ||
remoteMultiaddr: remoteMultiaddr, | ||
streamC: make(chan *stream, 1), | ||
streams: make(map[int64]network.MuxedStream), | ||
} | ||
|
||
c.addStream(s.id, s) | ||
return c | ||
} | ||
|
||
func (c *conn) Close() error { | ||
c.closeOnce.Do(func() { | ||
c.closed.Store(true) | ||
go c.rconn.Close() | ||
c.teardown() | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
func (c *conn) IsClosed() bool { | ||
return c.closed.Load() | ||
} | ||
|
||
func (c *conn) OpenStream(ctx context.Context) (network.MuxedStream, error) { | ||
sl, sr := newStreamPair() | ||
sl.conn = c | ||
c.addStream(sl.id, sl) | ||
log.Println("opening stream", sl.id, sr.id) | ||
|
||
c.rconn.streamC <- sr | ||
return sl, nil | ||
} | ||
|
||
func (c *conn) AcceptStream() (network.MuxedStream, error) { | ||
in := <-c.streamC | ||
in.conn = c | ||
c.addStream(in.id, in) | ||
return in, nil | ||
} | ||
|
||
func (c *conn) LocalPeer() peer.ID { return c.localPeer } | ||
|
||
// RemotePeer returns the peer ID of the remote peer. | ||
func (c *conn) RemotePeer() peer.ID { return c.remotePeerID } | ||
|
||
// RemotePublicKey returns the public pkey of the remote peer. | ||
func (c *conn) RemotePublicKey() ic.PubKey { return c.remotePubKey } | ||
|
||
// LocalMultiaddr returns the local Multiaddr associated | ||
func (c *conn) LocalMultiaddr() ma.Multiaddr { return c.localMultiaddr } | ||
|
||
// RemoteMultiaddr returns the remote Multiaddr associated | ||
func (c *conn) RemoteMultiaddr() ma.Multiaddr { return c.remoteMultiaddr } | ||
|
||
func (c *conn) Transport() tpt.Transport { | ||
return c.transport | ||
} | ||
|
||
func (c *conn) Scope() network.ConnScope { | ||
return c.scope | ||
} | ||
|
||
// ConnState is the state of security connection. | ||
func (c *conn) ConnState() network.ConnectionState { | ||
return network.ConnectionState{Transport: "memory"} | ||
} | ||
|
||
func (c *conn) addStream(id int64, stream network.MuxedStream) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.streams[id] = stream | ||
} | ||
|
||
func (c *conn) removeStream(id int64) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
delete(c.streams, id) | ||
} | ||
|
||
func (c *conn) teardown() { | ||
for id, s := range c.streams { | ||
log.Println("tearing down stream", id) | ||
s.Reset() | ||
} | ||
|
||
// TODO: remove self from listener | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
package memory | ||
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package memory | ||
|
||
import ( | ||
ic "github.com/libp2p/go-libp2p/core/crypto" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
ma "github.com/multiformats/go-multiaddr" | ||
mafmt "github.com/multiformats/go-multiaddr-fmt" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
var ( | ||
connCounter atomic.Int64 | ||
streamCounter atomic.Int64 | ||
listenerCounter atomic.Int64 | ||
dialMatcher = mafmt.Base(ma.P_MEMORY) | ||
memhub = newHub() | ||
) | ||
|
||
type hub struct { | ||
mu sync.RWMutex | ||
closeOnce sync.Once | ||
pubKeys map[peer.ID]ic.PubKey | ||
listeners map[string]*listener | ||
} | ||
|
||
func newHub() *hub { | ||
return &hub{ | ||
pubKeys: make(map[peer.ID]ic.PubKey), | ||
listeners: make(map[string]*listener), | ||
} | ||
} | ||
|
||
func (h *hub) addListener(addr string, l *listener) { | ||
h.mu.Lock() | ||
defer h.mu.Unlock() | ||
|
||
h.listeners[addr] = l | ||
} | ||
|
||
func (h *hub) removeListener(addr string, l *listener) { | ||
h.mu.Lock() | ||
defer h.mu.Unlock() | ||
|
||
delete(h.listeners, addr) | ||
} | ||
|
||
func (h *hub) getListener(addr string) (*listener, bool) { | ||
h.mu.RLock() | ||
defer h.mu.RUnlock() | ||
|
||
l, ok := h.listeners[addr] | ||
return l, ok | ||
} | ||
|
||
func (h *hub) addPubKey(p peer.ID, pk ic.PubKey) { | ||
h.mu.Lock() | ||
defer h.mu.Unlock() | ||
|
||
h.pubKeys[p] = pk | ||
} | ||
|
||
func (h *hub) getPubKey(p peer.ID) (ic.PubKey, bool) { | ||
h.mu.RLock() | ||
defer h.mu.RUnlock() | ||
|
||
pk, ok := h.pubKeys[p] | ||
return pk, ok | ||
} | ||
|
||
func (h *hub) close() { | ||
h.closeOnce.Do(func() { | ||
h.mu.Lock() | ||
defer h.mu.Unlock() | ||
|
||
for _, l := range h.listeners { | ||
l.Close() | ||
} | ||
}) | ||
} |
Oops, something went wrong.
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.
Write these before transport integration tests.