Skip to content

Commit 28d53d3

Browse files
authored
manager: get unicast ip only when the config one is not (#386)
Signed-off-by: xhe <[email protected]>
1 parent 3354c7a commit 28d53d3

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

lib/util/sys/sys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package sys
55

66
import "net"
77

8-
func GetLocalIP() string {
8+
func GetGlobalUnicastIP() string {
99
addrs, err := net.InterfaceAddrs()
1010
if err == nil {
1111
for _, address := range addrs {

pkg/manager/infosync/info.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,21 @@ func (is *InfoSyncer) getTopologyInfo(cfg *config.Config) (*TopologyInfo, error)
160160
s = ""
161161
}
162162
dir := path.Dir(s)
163-
ip := sys.GetLocalIP()
164-
_, port, err := net.SplitHostPort(cfg.Proxy.Addr)
163+
ip, port, err := net.SplitHostPort(cfg.Proxy.Addr)
165164
if err != nil {
166165
return nil, errors.WithStack(err)
167166
}
168167
_, statusPort, err := net.SplitHostPort(cfg.API.Addr)
169168
if err != nil {
170169
return nil, errors.WithStack(err)
171170
}
171+
// reporting a non unicast IP makes no sense, try to find one
172+
// loopback/linklocal-unicast are not global unicast IP, but are valid local unicast IP
173+
if pip := net.ParseIP(ip); ip == "" || pip.Equal(net.IPv4bcast) || pip.IsUnspecified() || pip.IsMulticast() {
174+
if v := sys.GetGlobalUnicastIP(); v != "" {
175+
ip = v
176+
}
177+
}
172178
return &TopologyInfo{
173179
Version: versioninfo.TiProxyVersion,
174180
GitHash: versioninfo.TiProxyGitHash,

pkg/manager/infosync/info_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"encoding/json"
99
"fmt"
10+
"net"
1011
"net/url"
1112
"path"
1213
"strings"
@@ -16,6 +17,7 @@ import (
1617
tidbinfo "github.com/pingcap/tidb/domain/infosync"
1718
"github.com/pingcap/tiproxy/lib/config"
1819
"github.com/pingcap/tiproxy/lib/util/logger"
20+
"github.com/pingcap/tiproxy/lib/util/sys"
1921
"github.com/pingcap/tiproxy/lib/util/waitgroup"
2022
"github.com/pingcap/tiproxy/pkg/manager/cert"
2123
"github.com/stretchr/testify/require"
@@ -178,6 +180,43 @@ func TestFetchTiDBTopology(t *testing.T) {
178180
}
179181
}
180182

183+
func TestGetTopology(t *testing.T) {
184+
ts := newEtcdTestSuite(t)
185+
t.Cleanup(ts.close)
186+
for _, cas := range []struct {
187+
ip string
188+
port string
189+
non_unicast bool
190+
}{
191+
{":34", "34", true},
192+
{"0.0.0.0:34", "34", true},
193+
{"255.255.255.255:34", "34", true},
194+
{"239.255.255.255:34", "34", true},
195+
{"[FF02::1:FF47]:34", "34", true},
196+
{"127.0.0.1:34", "34", false},
197+
{"[F02::1:FF47]:34", "34", false},
198+
{"192.0.0.1:6049", "6049", false},
199+
} {
200+
is, err := ts.is.getTopologyInfo(&config.Config{
201+
Proxy: config.ProxyServer{
202+
Addr: cas.ip,
203+
},
204+
API: config.API{
205+
Addr: cas.ip,
206+
},
207+
})
208+
require.NoError(t, err)
209+
ip, _, err := net.SplitHostPort(cas.ip)
210+
require.NoError(t, err)
211+
if cas.non_unicast {
212+
ip = sys.GetGlobalUnicastIP()
213+
}
214+
require.Equal(t, ip, is.IP)
215+
require.Equal(t, cas.port, is.Port)
216+
require.Equal(t, cas.port, is.StatusPort)
217+
}
218+
}
219+
181220
// Test that fetching retries when etcd server is down until the server is up again.
182221
func TestEtcdServerDown4Fetch(t *testing.T) {
183222
ts := newEtcdTestSuite(t)

0 commit comments

Comments
 (0)