From c9bf57490897f63cf44a61c6cf0f2c12676441a3 Mon Sep 17 00:00:00 2001 From: chenbin1314 Date: Sat, 26 Mar 2022 21:21:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9E=E6=8E=A5skylb=20server=E4=B8=8D?= =?UTF-8?q?=E9=87=87=E7=94=A8=E9=9A=8F=E6=9C=BA,=20=E4=BC=9A=E9=87=87?= =?UTF-8?q?=E7=94=A8grpc=E8=B4=9F=E8=BD=BD=E5=9D=87=E8=A1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + internal/rpccli/BUILD.bazel | 1 + internal/rpccli/rpc.go | 75 ++++++++++++++++++++++++------------- internal/skylb/client.go | 7 +++- repositories.bzl | 2 +- 5 files changed, 57 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 34c36b5..ed04829 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ logs/ # Bazel. bazel-bin bazel-genfiles +bazel-skylb-api bazel-skylb-apiv2 bazel-out bazel-testlogs diff --git a/internal/rpccli/BUILD.bazel b/internal/rpccli/BUILD.bazel index ac9742c..0a006a4 100644 --- a/internal/rpccli/BUILD.bazel +++ b/internal/rpccli/BUILD.bazel @@ -17,5 +17,6 @@ go_library( "@com_github_golang_glog//:go_default_library", "@org_golang_x_net//context:go_default_library", "@org_golang_google_grpc//:go_default_library", + "@org_golang_google_grpc//credentials/insecure:go_default_library", ], ) diff --git a/internal/rpccli/rpc.go b/internal/rpccli/rpc.go index c7119bf..9f8e66b 100644 --- a/internal/rpccli/rpc.go +++ b/internal/rpccli/rpc.go @@ -11,6 +11,7 @@ import ( "github.com/golang/glog" "golang.org/x/net/context" "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" "github.com/binchencoder/letsgo/strings" "github.com/binchencoder/skylb-api/internal/flags" @@ -30,10 +31,42 @@ func NewGrpcClient(ctx context.Context) (pb.SkylbClient, error) { } eps := strings.CsvToSlice(*flags.SkylbEndpoints) - if len(eps) == 0 { - glog.Fatalln("Valid flag --skylb-endpoints is required.") + // if len(eps) == 0 { + // glog.Fatalln("Valid flag --skylb-endpoints is required.") + // } + + // err, ep, port := randomSkylbEndpoint(eps) + // if err != nil { + // panic(err) + // } + // addrs := fmt.Sprintf("%s:%s", ep, port) + + for _, ep := range eps { + checkValidHost(ep) + } + addrs := *flags.SkylbEndpoints + + conn, err := grpc.Dial(resolver.DirectTarget(addrs), + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithTimeout(time.Second), grpc.WithBlock()) + if err != nil { + glog.Errorf("Failed to dial to SkyLB instance %s, %+v.", addrs, err) + return nil, err } + go func(ctx context.Context, conn *grpc.ClientConn) { + <-ctx.Done() + if err := conn.Close(); err != nil { + glog.Errorf("Failed to close gRPC connection, %+v", err) + } + glog.V(1).Infoln("SkyLB client connection closed.") + }(ctx, conn) + + glog.V(1).Infoln("SkyLB client connection created.") + return pb.NewSkylbClient(conn), nil +} + +func randomSkylbEndpoint(eps []string) (error, string, string) { // Randomly pick one SkyLB gRPC instance to connect. var idx int if len(eps) > 1 { @@ -42,19 +75,15 @@ func NewGrpcClient(ctx context.Context) (pb.SkylbClient, error) { endpoint := eps[idx] glog.Infof("Picked SKyLB gRPC instance %s", endpoint) - parts := stdstr.SplitN(endpoint, ":", 2) - ep, port := parts[0], parts[1] - addrs, err := net.LookupHost(ep) - if err != nil { - glog.Errorf("Failed to lookup SkyLB endpoint %s, %v.\n", endpoint, err) - return nil, err - } + addrs := checkValidHost(endpoint) glog.Infof("Resolved SkyLB instances %s", addrs) // TODO(chenbin) 这里不要随机取 + parts := stdstr.SplitN(endpoint, ":", 2) + ep, port := parts[0], parts[1] switch len(addrs) { case 0: - return nil, fmt.Errorf("No SkyLB instances found") + return fmt.Errorf("No SkyLB instances found"), "", "" case 1: ep = addrs[0] default: @@ -63,21 +92,15 @@ func NewGrpcClient(ctx context.Context) (pb.SkylbClient, error) { } glog.Infof("Connecting SkyLB instance %s on port %s", ep, port) - conn, err := grpc.Dial(resolver.DirectTarget(fmt.Sprintf("%s:%s", ep, port)), - grpc.WithInsecure(), grpc.WithTimeout(time.Second), grpc.WithBlock()) - if err != nil { - glog.Errorf("Failed to dial to SkyLB instance %s, %+v.", ep, err) - return nil, err - } - - go func(ctx context.Context, conn *grpc.ClientConn) { - <-ctx.Done() - if err := conn.Close(); err != nil { - glog.Errorf("Failed to close gRPC connection, %+v", err) - } - glog.V(1).Infoln("SkyLB client connection closed.") - }(ctx, conn) + return nil, ep, port +} - glog.V(1).Infoln("SkyLB client connection created.") - return pb.NewSkylbClient(conn), nil +func checkValidHost(endpoint string) (addres []string) { + parts := stdstr.SplitN(endpoint, ":", 2) + if addrs, err := net.LookupHost(parts[0]); err != nil { + glog.Errorf("Failed to lookup SkyLB endpoint %s, %v.\n", endpoint, err) + panic(err) + } else { + return addrs + } } diff --git a/internal/skylb/client.go b/internal/skylb/client.go index 64395e2..2454e43 100644 --- a/internal/skylb/client.go +++ b/internal/skylb/client.go @@ -2,6 +2,7 @@ package skylb import ( "fmt" + "net" "strconv" "strings" "time" @@ -68,8 +69,10 @@ func (sc *serviceClient) resolve(spec *pb.ServiceSpec) { if len(parts) != 2 { panic(fmt.Sprintf("Service instance endpoint should in format host:port, got %s", ep)) } - _, err := strconv.Atoi(parts[1]) - if err != nil { + if _, err := strconv.Atoi(parts[1]); err != nil { + panic(err) + } + if _, err := net.LookupHost(parts[0]); err != nil { panic(err) } } diff --git a/repositories.bzl b/repositories.bzl index adbf05b..5f04938 100644 --- a/repositories.bzl +++ b/repositories.bzl @@ -16,7 +16,7 @@ def go_repositories(): go_repository( name = "com_github_binchencoder_letsgo", importpath = "github.com/binchencoder/letsgo", - sum = "h1:hEDDOeGdX9R/JPYMdVo9N/9iQa5BeBLluTssrNYy/ng=", + sum = "h1:wYZv8TO4TGO2U8HjEO5Odf8OYWQjfrXS8ddfGZWQfHI=", version = "v0.0.5", )