|
9 | 9 | "errors"
|
10 | 10 | "fmt"
|
11 | 11 | "io"
|
| 12 | + "net" |
12 | 13 | "net/http"
|
13 | 14 | "os"
|
14 | 15 | "path/filepath"
|
@@ -214,13 +215,14 @@ func (n *Node) Stop(ctx context.Context) error {
|
214 | 215 | // Sets networking configuration for the node.
|
215 | 216 | // Convenience method for setting networking flags.
|
216 | 217 | func (n *Node) SetNetworkingConfig(bootstrapIDs []string, bootstrapIPs []string) {
|
217 |
| - var ( |
218 |
| - // Use dynamic port allocation. |
219 |
| - httpPort uint16 = 0 |
220 |
| - stakingPort uint16 = 0 |
221 |
| - ) |
222 |
| - n.Flags[config.HTTPPortKey] = httpPort |
223 |
| - n.Flags[config.StakingPortKey] = stakingPort |
| 218 | + if _, ok := n.Flags[config.HTTPPortKey]; !ok { |
| 219 | + // Default to dynamic port allocation |
| 220 | + n.Flags[config.HTTPPortKey] = 0 |
| 221 | + } |
| 222 | + if _, ok := n.Flags[config.StakingPortKey]; !ok { |
| 223 | + // Default to dynamic port allocation |
| 224 | + n.Flags[config.StakingPortKey] = 0 |
| 225 | + } |
224 | 226 | n.Flags[config.BootstrapIDsKey] = strings.Join(bootstrapIDs, ",")
|
225 | 227 | n.Flags[config.BootstrapIPsKey] = strings.Join(bootstrapIPs, ",")
|
226 | 228 | }
|
@@ -348,3 +350,16 @@ func (n *Node) EnsureNodeID() error {
|
348 | 350 |
|
349 | 351 | return nil
|
350 | 352 | }
|
| 353 | + |
| 354 | +// Saves the currently allocated API port to the node's configuration |
| 355 | +// for use across restarts. Reusing the port ensures consistent |
| 356 | +// labeling of metrics. |
| 357 | +func (n *Node) SaveAPIPort() error { |
| 358 | + hostPort := strings.TrimPrefix(n.URI, "http://") |
| 359 | + _, port, err := net.SplitHostPort(hostPort) |
| 360 | + if err != nil { |
| 361 | + return err |
| 362 | + } |
| 363 | + n.Flags[config.HTTPPortKey] = port |
| 364 | + return nil |
| 365 | +} |
0 commit comments