mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
server/proxy/tcpproxy: use net.JoinHostPort rather than formatAddr
formatAddr is just like net.JoinHostPort, I think we can use net.JoinHostPort directly, it's simpler and clearer. Signed-off-by: Jes Cok <xigua67damn@gmail.com>
This commit is contained in:
parent
6cbaa1f441
commit
a2156a0f9c
@ -19,7 +19,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -70,29 +69,13 @@ type TCPProxy struct {
|
|||||||
pickCount int // for round robin
|
pickCount int // for round robin
|
||||||
}
|
}
|
||||||
|
|
||||||
// The parameter host is returned by net.SplitHostPort previously,
|
|
||||||
// so it must be a valid host. This function is only to check whether
|
|
||||||
// it's an IPv6 IP address.
|
|
||||||
func isIPv6(host string) bool {
|
|
||||||
return strings.IndexRune(host, ':') != -1
|
|
||||||
}
|
|
||||||
|
|
||||||
// A literal IPv6 address in hostport must be enclosed in square
|
|
||||||
// brackets, as in "[::1]:80", "[::1%lo0]:80".
|
|
||||||
func formatAddr(host string, port uint16) string {
|
|
||||||
if isIPv6(host) {
|
|
||||||
return fmt.Sprintf("[%s]:%d", host, port)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s:%d", host, port)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tp *TCPProxy) Run() error {
|
func (tp *TCPProxy) Run() error {
|
||||||
tp.donec = make(chan struct{})
|
tp.donec = make(chan struct{})
|
||||||
if tp.MonitorInterval == 0 {
|
if tp.MonitorInterval == 0 {
|
||||||
tp.MonitorInterval = 5 * time.Minute
|
tp.MonitorInterval = 5 * time.Minute
|
||||||
}
|
}
|
||||||
for _, srv := range tp.Endpoints {
|
for _, srv := range tp.Endpoints {
|
||||||
addr := formatAddr(srv.Target, srv.Port)
|
addr := net.JoinHostPort(srv.Target, fmt.Sprintf("%d", srv.Port))
|
||||||
tp.remotes = append(tp.remotes, &remote{srv: srv, addr: addr})
|
tp.remotes = append(tp.remotes, &remote{srv: srv, addr: addr})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,34 +129,3 @@ func TestUserspaceProxyPriority(t *testing.T) {
|
|||||||
t.Errorf("got = %s, want %s", got, want)
|
t.Errorf("got = %s, want %s", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFormatAddr(t *testing.T) {
|
|
||||||
addrs := []struct {
|
|
||||||
host string
|
|
||||||
port uint16
|
|
||||||
expectedAddr string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
"192.168.1.10",
|
|
||||||
2379,
|
|
||||||
"192.168.1.10:2379",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"::1",
|
|
||||||
2379,
|
|
||||||
"[::1]:2379",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"2001:db8::ff00:42:8329",
|
|
||||||
80,
|
|
||||||
"[2001:db8::ff00:42:8329]:80",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, addr := range addrs {
|
|
||||||
actualAddr := formatAddr(addr.host, addr.port)
|
|
||||||
if actualAddr != addr.expectedAddr {
|
|
||||||
t.Errorf("actualAddr: %s, expectedAddr: %s", actualAddr, addr.expectedAddr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user