Skip to content

Commit a41185a

Browse files
committed
feat: add new builder
# Overview This commit introduces a new builder concept to allow users more control in building a go-libp2p node. The builder package itself serves as an example of how to configure and connect all the go-libp2p parts into a host. However, there is nothing special about the builder package, and advanced users will make their own builder Config. It's build around a small and simple DI library I wrote. The DI library has no dependencies, and could be implemented from scratch in a couple of hours. Refer to the godoc at https://pkg.go.dev/git.sr.ht/~marcopolo/di for more details. In the future, we may split the builder package further to allow reuse of common components without depending on every component. For example, we may move the swarm+host constructor logic into a separate package from the transport constructor logic. That way a user could reuse the swarm+host constructors without depending on the transport constructors. Today, users can still minimize their dependencies by creating their own Builder config. They may use the existing one as a template and prune the things they don't need. I'd like to get feedback from users on this feature before investing more time into it. # History package builder was born out of frustration with Fx and the convoluted nature of building a Go libp2p host. Especially when one wants to step away from the default path. Issues with the correct libp2p.New constructor and Fx: - It's a bad combination of Options functions with a Config struct and Fx options. - The reason for that is that Fx, doesn't provide a good way to have a default value for some setting. For example, there's no way to provide a default resource manager that gets overwritten if a user provides their own resource manager. There are plenty of hacks to workaround this, but they inevitably introduce strange new concepts to the user (e.g. abusing Fx labels, groups, or weird constructors.) - It's hard to pull out one part if you only need that part. For example, if you want the logic around AutoNAT without pulling in any dependency on WebRTC, it's currently hard. - Fx Options type erase the thing they provide. Take `Config.QUICReuse` as an example. The type of that field is `[]fx.Option`. Literally any fx option can go in there. - Fx is a fairly large dependency. The builder package takes a different approach focuses on the following goals: - Ergonomic defaults. - Clear layout of required objects. - Let users to select only the services and components they need. - Let users get references to instantiated services. Don't hide everything in the basic host.
1 parent f2964a5 commit a41185a

File tree

5 files changed

+1000
-0
lines changed

5 files changed

+1000
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ retract v0.26.1 // Tag was applied incorrectly due to a bug in the release workf
77
retract v0.36.0 // Accidentally modified the tag.
88

99
require (
10+
git.sr.ht/~marcopolo/di v0.0.4
1011
github.com/benbjohnson/clock v1.3.5
1112
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c
1213
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBr
77
dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4=
88
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
99
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
10+
git.sr.ht/~marcopolo/di v0.0.4 h1:BJOTly/cSV7lXvSurTg+derTF+gS2qgz31aN1OUxxtQ=
11+
git.sr.ht/~marcopolo/di v0.0.4/go.mod h1:lLURtWN1LBR3r9P+VA9O3SCJ7hBxYDv55YMLP997M7Q=
1012
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
1113
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
1214
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=

0 commit comments

Comments
 (0)