diff --git a/core/p2p/node_config_internal_test.go b/core/p2p/node_config_internal_test.go new file mode 100644 index 0000000000..ffc6e6bc3b --- /dev/null +++ b/core/p2p/node_config_internal_test.go @@ -0,0 +1,32 @@ +package p2p + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("newNodeConfig", func() { + It("leaves resource limits disabled when LOCALAI_P2P_ENABLE_LIMITS is unset", func() { + GinkgoT().Setenv("LOCALAI_P2P_ENABLE_LIMITS", "") + + Expect(newNodeConfig("token").Limit.Enable).To(BeFalse()) + }) + + It("enables resource limits when LOCALAI_P2P_ENABLE_LIMITS is \"true\"", func() { + GinkgoT().Setenv("LOCALAI_P2P_ENABLE_LIMITS", "true") + + Expect(newNodeConfig("token").Limit.Enable).To(BeTrue()) + }) + + It("leaves resource limits disabled for any other value", func() { + for _, v := range []string{"false", "1", "yes"} { + GinkgoT().Setenv("LOCALAI_P2P_ENABLE_LIMITS", v) + + Expect(newNodeConfig("token").Limit.Enable).To(BeFalse(), "value %q", v) + } + }) + + It("keeps the network token", func() { + Expect(newNodeConfig("token").NetworkToken).To(Equal("token")) + }) +}) diff --git a/core/p2p/p2p.go b/core/p2p/p2p.go index d03a9c50c2..28525c40a3 100644 --- a/core/p2p/p2p.go +++ b/core/p2p/p2p.go @@ -375,11 +375,29 @@ func NewNode(token string) (*node.Node, error) { func newNodeOpts(token string) ([]node.Option, error) { llger := logger.New(log.LevelFatal) + + c := newNodeConfig(token) + + nodeOpts, _, err := c.ToOpts(llger) + if err != nil { + return nil, fmt.Errorf("parsing options: %w", err) + } + + nodeOpts = append(nodeOpts, services.Alive(30*time.Second, 900*time.Second, 15*time.Minute)...) + + return nodeOpts, nil +} + +// newNodeConfig builds the edgevpn node configuration from the LOCALAI_P2P_* +// environment variables. +func newNodeConfig(token string) config.Config { defaultInterval := 10 * time.Second // TODO: move this up, expose more config options when creating a node noDHT := os.Getenv("LOCALAI_P2P_DISABLE_DHT") == "true" - noLimits := os.Getenv("LOCALAI_P2P_ENABLE_LIMITS") != "true" + // Resource limits are opt-in: the libp2p resource manager is enabled only + // when this is set to "true", as documented. + enableLimits := os.Getenv("LOCALAI_P2P_ENABLE_LIMITS") == "true" var listenMaddrs []string var bootstrapPeers []string @@ -400,11 +418,11 @@ func newNodeOpts(token string) ([]node.Option, error) { if libp2ploglevel == "" { libp2ploglevel = "fatal" } - c := config.Config{ + return config.Config{ ListenMaddrs: listenMaddrs, DHTAnnounceMaddrs: dhtAnnounceMaddrs, Limit: config.ResourceLimit{ - Enable: noLimits, + Enable: enableLimits, MaxConns: 100, }, NetworkToken: token, @@ -435,15 +453,6 @@ func newNodeOpts(token string) ([]node.Option, error) { MaxConnections: 1000, }, } - - nodeOpts, _, err := c.ToOpts(llger) - if err != nil { - return nil, fmt.Errorf("parsing options: %w", err) - } - - nodeOpts = append(nodeOpts, services.Alive(30*time.Second, 900*time.Second, 15*time.Minute)...) - - return nodeOpts, nil } func stringsToMultiAddr(peers []string) []multiaddr.Multiaddr { diff --git a/core/p2p/p2p_suite_test.go b/core/p2p/p2p_suite_test.go new file mode 100644 index 0000000000..9e931fa5cc --- /dev/null +++ b/core/p2p/p2p_suite_test.go @@ -0,0 +1,13 @@ +package p2p_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestP2P(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "P2P test suite") +}