Strip port from server name in grpclb (#2066)
The grpclb server expects server name to not have port number
This commit is contained in:
11
grpclb.go
11
grpclb.go
@ -19,6 +19,7 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -126,6 +127,16 @@ func (b *lbBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) bal
|
|||||||
target = targetSplitted[1]
|
target = targetSplitted[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove port number from target if it exists.
|
||||||
|
if host, _, err := net.SplitHostPort(target); err == nil {
|
||||||
|
// When err is not nil, target remains unchanged.
|
||||||
|
//
|
||||||
|
// Possible non-nil err values:
|
||||||
|
// - missing port in address: no port to strip
|
||||||
|
// - other errors: failed to parse
|
||||||
|
target = host
|
||||||
|
}
|
||||||
|
|
||||||
lb := &lbBalancer{
|
lb := &lbBalancer{
|
||||||
cc: newLBCacheClientConn(cc),
|
cc: newLBCacheClientConn(cc),
|
||||||
target: target,
|
target: target,
|
||||||
|
@ -313,7 +313,7 @@ func newLoadBalancer(numberOfBackends int) (tss *testServers, cleanup func(), er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGRPCLB(t *testing.T) {
|
func testGRPCLBSimple(t *testing.T, target string) {
|
||||||
defer leakcheck.Check(t)
|
defer leakcheck.Check(t)
|
||||||
|
|
||||||
r, cleanup := manual.GenerateAndRegisterManualResolver()
|
r, cleanup := manual.GenerateAndRegisterManualResolver()
|
||||||
@ -341,7 +341,7 @@ func TestGRPCLB(t *testing.T) {
|
|||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
|
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+target,
|
||||||
grpc.WithTransportCredentials(&creds), grpc.WithDialer(fakeNameDialer))
|
grpc.WithTransportCredentials(&creds), grpc.WithDialer(fakeNameDialer))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to dial to the backend %v", err)
|
t.Fatalf("Failed to dial to the backend %v", err)
|
||||||
@ -360,6 +360,17 @@ func TestGRPCLB(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGRPCLB(t *testing.T) {
|
||||||
|
testGRPCLBSimple(t, beServerName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestGRPCLBWithPort is same as TestGRPCLB, expect that the dialing target
|
||||||
|
// contains fake port number ":8080". The purpose of this test is to make sure
|
||||||
|
// that grpclb strips port number from the dialing target in init request.
|
||||||
|
func TestGRPCLBWithPort(t *testing.T) {
|
||||||
|
testGRPCLBSimple(t, beServerName+":8080")
|
||||||
|
}
|
||||||
|
|
||||||
// The remote balancer sends response with duplicates to grpclb client.
|
// The remote balancer sends response with duplicates to grpclb client.
|
||||||
func TestGRPCLBWeighted(t *testing.T) {
|
func TestGRPCLBWeighted(t *testing.T) {
|
||||||
defer leakcheck.Check(t)
|
defer leakcheck.Check(t)
|
||||||
|
Reference in New Issue
Block a user