Skip to content

Commit 959d1af

Browse files
author
Shlomi Noach
committed
support ipv6 without port
1 parent a7cfaa4 commit 959d1af

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

go/mysql/instance_key.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const (
1919
var (
2020
ipv4HostPortRegexp = regexp.MustCompile("^([^:]+):([0-9]+)$")
2121
ipv4HostRegexp = regexp.MustCompile("^([^:]+)$")
22-
ipv6HostPortRegexp = regexp.MustCompile("^\\[(.+)\\]:([0-9]+)$") // e.g. [2001:db8:1f70::999:de8:7648:6e8]:3308
22+
ipv6HostPortRegexp = regexp.MustCompile("^\\[([:0-9a-fA-F]+)\\]:([0-9]+)$") // e.g. [2001:db8:1f70::999:de8:7648:6e8]:3308
23+
ipv6HostRegexp = regexp.MustCompile("^([:0-9a-fA-F]+)$") // e.g. 2001:db8:1f70::999:de8:7648:6e8
2324
)
2425

2526
// InstanceKey is an instance indicator, identified by hostname and port
@@ -42,6 +43,8 @@ func NewRawInstanceKey(hostPort string) (*InstanceKey, error) {
4243
} else if submatch := ipv6HostPortRegexp.FindStringSubmatch(hostPort); len(submatch) > 0 {
4344
hostname = submatch[1]
4445
port = submatch[2]
46+
} else if submatch := ipv6HostRegexp.FindStringSubmatch(hostPort); len(submatch) > 0 {
47+
hostname = submatch[1]
4548
} else {
4649
return nil, fmt.Errorf("Cannot parse address: %s", hostPort)
4750
}

go/mysql/instance_key_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ func TestParseInstanceKey(t *testing.T) {
4747
test.S(t).ExpectEquals(key.Hostname, "2001:db8:1f70::999:de8:7648:6e8")
4848
test.S(t).ExpectEquals(key.Port, 3308)
4949
}
50+
{
51+
key, err := ParseInstanceKey("::1")
52+
test.S(t).ExpectNil(err)
53+
test.S(t).ExpectEquals(key.Hostname, "::1")
54+
test.S(t).ExpectEquals(key.Port, 3306)
55+
}
56+
{
57+
key, err := ParseInstanceKey("0:0:0:0:0:0:0:0")
58+
test.S(t).ExpectNil(err)
59+
test.S(t).ExpectEquals(key.Hostname, "0:0:0:0:0:0:0:0")
60+
test.S(t).ExpectEquals(key.Port, 3306)
61+
}
62+
{
63+
_, err := ParseInstanceKey("[2001:xxxx:1f70::999:de8:7648:6e8]:3308")
64+
test.S(t).ExpectNotNil(err)
65+
}
5066
{
5167
_, err := ParseInstanceKey("10.0.0.4:")
5268
test.S(t).ExpectNotNil(err)

0 commit comments

Comments
 (0)