Support ipv6 addresses in grpclb (#1303)

Add square brackets to ipv6 addresses, otherwise net.Dial() and
net.SplitHostPort() will return too many colons error.
This commit is contained in:
Menghan Li
2017-06-13 13:43:09 -07:00
committed by GitHub
parent 89caed9e62
commit 1ab4adf22d
2 changed files with 18 additions and 2 deletions

View File

@ -235,8 +235,15 @@ func (b *balancer) processServerList(l *lbpb.ServerList, seq int) {
)
for _, s := range servers {
md := metadata.Pairs("lb-token", s.LoadBalanceToken)
ip := net.IP(s.IpAddress)
ipStr := ip.String()
if ip.To4() == nil {
// Add square brackets to ipv6 addresses, otherwise net.Dial() and
// net.SplitHostPort() will return too many colons error.
ipStr = fmt.Sprintf("[%s]", ipStr)
}
addr := Address{
Addr: fmt.Sprintf("%s:%d", net.IP(s.IpAddress), s.Port),
Addr: fmt.Sprintf("%s:%d", ipStr, s.Port),
Metadata: &md,
}
sl = append(sl, &grpclbAddrInfo{

View File

@ -129,8 +129,9 @@ func (r *testNameResolver) inject(updates []*naming.Update) {
}
type serverNameCheckCreds struct {
expected string
mu sync.Mutex
sn string
expected string
}
func (c *serverNameCheckCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
@ -141,6 +142,8 @@ func (c *serverNameCheckCreds) ServerHandshake(rawConn net.Conn) (net.Conn, cred
return rawConn, nil, nil
}
func (c *serverNameCheckCreds) ClientHandshake(ctx context.Context, addr string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
c.mu.Lock()
defer c.mu.Unlock()
b := make([]byte, len(c.expected))
if _, err := rawConn.Read(b); err != nil {
fmt.Printf("Failed to read the server name from the server %v", err)
@ -153,14 +156,20 @@ func (c *serverNameCheckCreds) ClientHandshake(ctx context.Context, addr string,
return rawConn, nil, nil
}
func (c *serverNameCheckCreds) Info() credentials.ProtocolInfo {
c.mu.Lock()
defer c.mu.Unlock()
return credentials.ProtocolInfo{}
}
func (c *serverNameCheckCreds) Clone() credentials.TransportCredentials {
c.mu.Lock()
defer c.mu.Unlock()
return &serverNameCheckCreds{
expected: c.expected,
}
}
func (c *serverNameCheckCreds) OverrideServerName(s string) error {
c.mu.Lock()
defer c.mu.Unlock()
c.expected = s
return nil
}