-
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
refactor for transport changes #299
Conversation
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.
While I usually don't like dynamic typing or duck typing, I figured that the usability benefit of duck typing in the libp2p constructor significantly outweighs the drawbacks. Anyways, errors should be caught as soon as the application starts.
type AddrsFactory = bhost.AddrsFactory | ||
|
||
// NATManagerC is a NATManager constructor. | ||
type NATManagerC func(inet.Network) bhost.NATManager |
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.
Could also call this NATManagerConstructor
(hence the C
) or NATManagerFactory
. However, while technically exported, users should never need to actually use this type (or any of the other *C
types).
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.
Still better than in Java world.
@@ -0,0 +1,173 @@ | |||
package config |
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.
I moved this logic into a separate package for organization (it was growing a bit large). I left all the actual options in the main libp2p package.
config/config.go
Outdated
type Config struct { | ||
Transports []TptC | ||
Muxers []MsMuxC | ||
SecurityTransports []MsSecC |
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.
One can now easily plug in new security transports.
config/config.go
Outdated
// Config describes a set of settings for a libp2p node | ||
type Config struct { | ||
Transports []TptC | ||
Muxers []MsMuxC |
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.
Instead of taking a single transport, we now take an array of named multiplexer transports. Why not a map? We need the order (priority).
libp2p.go
Outdated
if err != nil { | ||
return nil, err | ||
// EnableRelay configures libp2p to enable the relay transport. | ||
func EnableRelay(options ...circuit.RelayOpt) Option { |
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.
This isn't the most user-friendly interface. It also kind of sucks that one has to import the relay package to call this.
@@ -270,10 +243,6 @@ func (h *BasicHost) newStreamHandler(s inet.Stream) { | |||
} | |||
|
|||
s.SetProtocol(protocol.ID(protoID)) | |||
|
|||
if h.bwc != nil { | |||
s = mstream.WrapStream(s, h.bwc) |
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.
The swarm now takes care of this before it even hits the identify service.
) | ||
|
||
func init() { | ||
// change the garbage collect timeout for testing. | ||
ps.GarbageCollectTimeout = 10 * time.Millisecond |
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.
poof
|
||
// ConnManager is a libp2p connection manager | ||
ConnManager ifconnmgr.ConnManager | ||
|
||
// Relay indicates whether the host should use circuit relay transport | ||
EnableRelay bool |
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.
Handled by the libp2p constructor.
|
||
// BandwidthReporter is used for collecting aggregate metrics of the | ||
// bandwidth used by various protocols. | ||
BandwidthReporter metrics.Reporter |
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.
Was only needed by the identity service because the old swarm didn't enable the bandwidth reporter early enough. That's no longer the case so the host doesn't need this now.
@@ -536,11 +497,6 @@ func (h *BasicHost) Close() error { | |||
return h.proc.Close() | |||
} | |||
|
|||
// GetBandwidthReporter exposes the Host's bandiwth metrics reporter | |||
func (h *BasicHost) GetBandwidthReporter() metrics.Reporter { |
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.
This isn't a part of the Host interface and I can't find anything that actually uses it. Really, users should just hang onto the bandwidth reporter that they passed in.
ae4c193
to
df17003
Compare
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.
this overall looks good, but i skimmed through the reflection details.
libp2p.go
Outdated
|
||
return tpt | ||
// NATPortMap configures libp2p to use the default NATManager. The default | ||
// NATManager will attempt to punch holes in your NAT. |
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.
this comment is technically incorrect -- doesn't the nat manager attempt to open ports using UPnP?
It doesn't really use hole punching.
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.
Yes, good point.
config/config.go
Outdated
if ps == nil { | ||
ps = pstore.NewPeerstore() | ||
} | ||
if !cfg.Insecure { |
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.
Why do we keep lugging around the insecure option?
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.
We use it for debugging. It's actually really useful. However, it's designed to be an all or nothing "disable all security" option.
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.
fair enough! I can see it being useful in analysing wire protocols without encryption.
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.
LGTM (reviewed in a video call together)
@whyrusleeping massively reduced the code duplication for the reflection stuff. It turned out to be much easier than I had thought. I left the optional error in the transport constructor return type but broke the code int a bunch of smaller, readable functions. |
12d3618
to
fd3f3d9
Compare
I was looking to bubble go-reuseport with a fixed |
Feel free (that's why I'm merging these all at the same time). |
99b1900
to
29228f2
Compare
config/config.go
Outdated
ConnManager ifconnmgr.ConnManager | ||
AddrsFactory bhost.AddrsFactory | ||
Filters *filter.Filters | ||
NATManager NATManagerC |
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.
Some spacing ques would be good here. Like Relay
and RelayOpts
can have their own section.
config/config.go
Outdated
func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { | ||
// Check this early. Prevents us from even *starting* without verifying this. | ||
if pnet.ForcePrivateNetwork && cfg.Protector == nil { | ||
log.Error("tried to dial with no Private Network Protector but usage" + |
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.
dial
is not accurate here
config/config.go
Outdated
return nil, err | ||
} | ||
|
||
// Create a new blank peerstore if none was passed in |
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.
Comment not accurate, either change behaviour or the comment.
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.
Good catch. I moved the defaults to a special FallbackDefaults
option.
config/muxer.go
Outdated
}, nil | ||
} | ||
|
||
fn, err := makeConstructor(m, muxType, muxArgTypes) |
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.
IMO s/fn/ctor/
package.json
Outdated
{ | ||
"author": "whyrusleeping", | ||
"hash": "QmYkxWtBWqkj6SWCiFN2a3xnnk5womAQYd9Kh68LJJR7jz", | ||
"name": "go-ws-transport", |
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.
do we have to depend on a transport?
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.
It's a default transport so yes unless we want to make it not a default transport. Note: we used to depend on it in go-libp2p-swarm but I moved all default transport construction here.
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.
One invalid comment, and a nitpick.
I am not a fan of voodoo magic but I don't think there is a better way.
I completely agree. My first attempt involved defining some nice interfaces but it turned into a mess. |
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.
SGWM
e3579af
to
cfe6ab5
Compare
Also, make the libp2p constructor fully useful. There should now be no need to manually construct a swarm/host.
Also, make the libp2p constructor fully useful. There should now be no need to manually construct a swarm/host. That's actually where most of the new code comes from (hence the +450 lines of (actual) code). This allows me to actually use the libp2p constructor in go-ipfs.
DO NOT MERGE (yet)
Part of: #297