onNodeConnected 返回节点建连回调:用 gRPC 对端 IP 更新 node.BaseURL。 仅更新 loopback 或空 BaseURL,保留管理员手动配置的域名。
(store nodes.Store)
| 80 | // onNodeConnected 返回节点建连回调:用 gRPC 对端 IP 更新 node.BaseURL。 |
| 81 | // 仅更新 loopback 或空 BaseURL,保留管理员手动配置的域名。 |
| 82 | func onNodeConnected(store nodes.Store) func(nodeID, peerIP string) { |
| 83 | return func(nodeID, peerIP string) { |
| 84 | if peerIP == "" { |
| 85 | return |
| 86 | } |
| 87 | node, err := store.Get(nodeID) |
| 88 | if err != nil { |
| 89 | return |
| 90 | } |
| 91 | existing := node.BaseURL |
| 92 | if existing != "" && !isLoopbackURL(existing) { |
| 93 | return |
| 94 | } |
| 95 | node.BaseURL = fmt.Sprintf("http://%s", peerIP) |
| 96 | if _, err := store.Upsert(node); err != nil { |
| 97 | log.Printf("nodehub: update node %s base_url: %v", nodeID, err) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func isLoopbackURL(u string) bool { |
| 103 | // 支持 http://host:port、http://host、https://[::1]:port 等格式 |
no test coverage detected