fix(p2p): read LOCALAI_P2P_ENABLE_LIMITS as documented

Root cause: #5889 changed the comparison to `!= "true"` while the result
still feeds ResourceLimit.Enable directly, so the libp2p resource manager
was enabled when the variable was unset and disabled when it was set to
"true" -- the opposite of the docs, and of the debug example that the same
PR added.

Extract newNodeConfig from newNodeOpts so the environment parsing can be
covered by a test.

Fixes #13031

Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Vivek1898 <viveksingh27795@gmail.com>
This commit is contained in:
Vivek1898 committed 2026-11-02 22:31:21 +05:30
1 parent 16acc8926b
commit 404f86194c
3 files changed
+66 -12

No files matched your search

+32
View File
@@ -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"))
})
})
+21 -12
View File
@@ -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 {
+13
View File
@@ -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")
}