Skip to content

Commit 856c6cb

Browse files
committed
fix(p2p): allocate tunnels only when needed
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 1ed5af1 commit 856c6cb

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

core/cli/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (r *RunCMD) Run(ctx *cliContext.Context) error {
135135

136136
os.Setenv("LLAMACPP_GRPC_SERVERS", tunnelEnvVar)
137137
log.Debug().Msgf("setting LLAMACPP_GRPC_SERVERS to %s", tunnelEnvVar)
138-
}); err != nil {
138+
}, true); err != nil {
139139
return err
140140
}
141141
}
@@ -153,7 +153,7 @@ func (r *RunCMD) Run(ctx *cliContext.Context) error {
153153
return err
154154
}
155155

156-
if err := p2p.ServiceDiscoverer(context.Background(), node, token, p2p.NetworkID(r.Peer2PeerNetworkID, p2p.FederatedID), nil); err != nil {
156+
if err := p2p.ServiceDiscoverer(context.Background(), node, token, p2p.NetworkID(r.Peer2PeerNetworkID, p2p.FederatedID), nil, false); err != nil {
157157
return err
158158
}
159159
}

core/p2p/federated_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (f *FederatedServer) Start(ctx context.Context) error {
2929

3030
if err := ServiceDiscoverer(ctx, n, f.p2ptoken, f.service, func(servicesID string, tunnel NodeData) {
3131
log.Debug().Msgf("Discovered node: %s", tunnel.ID)
32-
}); err != nil {
32+
}, true); err != nil {
3333
return err
3434
}
3535

core/p2p/p2p.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ func allocateLocalService(ctx context.Context, node *node.Node, listenAddr, serv
139139

140140
// This is the main of the server (which keeps the env variable updated)
141141
// This starts a goroutine that keeps LLAMACPP_GRPC_SERVERS updated with the discovered services
142-
func ServiceDiscoverer(ctx context.Context, n *node.Node, token, servicesID string, discoveryFunc func(serviceID string, node NodeData)) error {
142+
func ServiceDiscoverer(ctx context.Context, n *node.Node, token, servicesID string, discoveryFunc func(serviceID string, node NodeData), allocate bool) error {
143143
if servicesID == "" {
144144
servicesID = defaultServicesID
145145
}
146-
tunnels, err := discoveryTunnels(ctx, n, token, servicesID)
146+
tunnels, err := discoveryTunnels(ctx, n, token, servicesID, allocate)
147147
if err != nil {
148148
return err
149149
}
@@ -170,7 +170,7 @@ func ServiceDiscoverer(ctx context.Context, n *node.Node, token, servicesID stri
170170
return nil
171171
}
172172

173-
func discoveryTunnels(ctx context.Context, n *node.Node, token, servicesID string) (chan NodeData, error) {
173+
func discoveryTunnels(ctx context.Context, n *node.Node, token, servicesID string, allocate bool) (chan NodeData, error) {
174174
tunnels := make(chan NodeData)
175175

176176
err := n.Start(ctx)
@@ -209,7 +209,9 @@ func discoveryTunnels(ctx context.Context, n *node.Node, token, servicesID strin
209209
zlog.Error().Msg("cannot unmarshal node data")
210210
continue
211211
}
212-
ensureService(ctx, n, nd, k)
212+
if allocate {
213+
ensureService(ctx, n, nd, k)
214+
}
213215
muservice.Lock()
214216
if _, ok := service[nd.Name]; ok {
215217
tunnels <- service[nd.Name].NodeData

core/p2p/p2p_disabled.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (f *FederatedServer) Start(ctx context.Context) error {
1818
return fmt.Errorf("not implemented")
1919
}
2020

21-
func ServiceDiscoverer(ctx context.Context, node *node.Node, token, servicesID string, fn func(string, NodeData)) error {
21+
func ServiceDiscoverer(ctx context.Context, node *node.Node, token, servicesID string, fn func(string, NodeData), allocate bool) error {
2222
return fmt.Errorf("not implemented")
2323
}
2424

0 commit comments

Comments
 (0)