Skip to content

Commit 72944f9

Browse files
committed
Temp commit
1 parent e4800fb commit 72944f9

31 files changed

+1212
-42
lines changed

client/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ go_library(
1313
"//proto:go_default_library",
1414
"//client/option:go_default_library",
1515
"//internal/skylb:go_default_library",
16+
"//naming:go_default_library",
1617
"@com_github_binchencoder_gateway_proto//data:go_default_library",
1718
"@com_github_binchencoder_letsgo//flags:go_default_library",
18-
"@com_github_binchencoder_letsgo//service/naming:go_default_library",
1919
"@org_golang_google_grpc//:go_default_library",
2020
],
2121
)

client/client.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package client
2+
3+
import (
4+
"google.golang.org/grpc"
5+
6+
vexpb "github.com/binchencoder/gateway-proto/data"
7+
"github.com/binchencoder/skylb-apiv2/client/option"
8+
"github.com/binchencoder/skylb-apiv2/internal/skylb"
9+
pb "github.com/binchencoder/skylb-apiv2/proto"
10+
)
11+
12+
// TODO(zhwang): remove this file once we migrate all references to the
13+
// new API.
14+
15+
// ServiceCli defines the interface through which the client app obtains
16+
// gRPC load balancing support from SkyLB.
17+
//
18+
// Deprecated: use ServiceLocator instead.
19+
type ServiceCli interface {
20+
// Resolve resolves a service spec.
21+
// It needs to be called for every service used by the client.
22+
Resolve(spec *pb.ServiceSpec, opts ...option.ResolveOption)
23+
24+
// EnableHistogram enables historgram in client api metrics.
25+
//
26+
// Even if there are multiple ServiceCli instances, EnableHistogram
27+
// only needs to be called once, on any of those instances.
28+
EnableHistogram()
29+
30+
// EnableResolveFullEps enables to resolve full endpoints.
31+
// Deprecated: This method will be removed in future.
32+
EnableResolveFullEps()
33+
34+
// EnableFailFast makes service client doesn't wait for service to become
35+
// available in Start().
36+
EnableFailFast()
37+
38+
// DisableResolveFullEps disables resolving full endpoints.
39+
// Deprecated: This method will be removed in future.
40+
DisableResolveFullEps()
41+
42+
// AddUnaryInterceptor adds a unary client interceptor to the client.
43+
AddUnaryInterceptor(incept grpc.UnaryClientInterceptor)
44+
45+
// Start starts the service resolver and returns the grpc connection for
46+
// each service through the callback function.
47+
//
48+
// Start can only be called once for each ServiceCli instance in the whole
49+
// lifecycle of an application.
50+
Start(callback func(spec *pb.ServiceSpec, conn *grpc.ClientConn), options ...grpc.DialOption)
51+
52+
// Shutdown turns the service client down. After shutdown, all grpc.Balancer
53+
// objects returned from Resolve() call can not be used any more.
54+
Shutdown()
55+
}
56+
57+
// NewServiceCli returns a new service client. NewServiceCli() should be called
58+
// once in the whole lifecycle of an application.
59+
func NewServiceCli(clientServiceId vexpb.ServiceId) ServiceCli {
60+
return skylb.NewServiceClient(clientServiceId, map[string]string(DebugSvcEndpoints))
61+
}

client/example_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package client
2+
3+
import (
4+
"google.golang.org/grpc"
5+
6+
vexpb "github.com/binchencoder/gateway-proto/data"
7+
"github.com/binchencoder/skylb-apiv2/handlers"
8+
)
9+
10+
func ExampleNewServiceLocator() {
11+
// Create a service locator.
12+
sl := NewServiceLocator(vexpb.ServiceId_SHARED_TEST_CLIENT_SERVICE)
13+
14+
// Resolve services.
15+
grpcHandler := handlers.NewGrpcLoadBalanceHandler(
16+
NewDefaultServiceSpec(vexpb.ServiceId_SHARED_TEST_SERVER_SERVICE),
17+
func(conn *grpc.ClientConn) {
18+
// hold the connecton for use later.
19+
},
20+
)
21+
sl.Resolve(grpcHandler)
22+
23+
// Start the locator.
24+
sl.Start()
25+
26+
// Use the connection to create grpc clients.
27+
28+
// Shutdown before exit.
29+
sl.Shutdown()
30+
}

client/locator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55

66
vexpb "github.com/binchencoder/gateway-proto/data"
77
"github.com/binchencoder/letsgo/flags"
8-
"github.com/binchencoder/letsgo/service/naming"
98
"github.com/binchencoder/skylb-apiv2/client/option"
9+
"github.com/binchencoder/skylb-apiv2/naming"
1010

1111
"github.com/binchencoder/skylb-apiv2/internal/skylb"
1212
pb "github.com/binchencoder/skylb-apiv2/proto"

client/option/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
4+
5+
go_library(
6+
name = "go_default_library",
7+
srcs = glob(
8+
["*.go"],
9+
exclude = ["*_test.go"],
10+
),
11+
importpath = "github.com/binchencoder/skylb-apiv2/client/option",
12+
deps = [
13+
"//proto:go_default_library",
14+
"@com_github_binchencoder_gateway_proto//data:go_default_library",
15+
"@com_github_opentracing_opentracing_go//:go_default_library",
16+
"@org_golang_google_grpc//:go_default_library",
17+
"@org_golang_google_grpc//resolver:go_default_library",
18+
],
19+
)

client/option/options.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package option
2+
3+
import (
4+
"errors"
5+
6+
opentracing "github.com/opentracing/opentracing-go"
7+
"google.golang.org/grpc/balancer"
8+
"google.golang.org/grpc/resolver"
9+
10+
vexpb "github.com/binchencoder/gateway-proto/data"
11+
pb "github.com/binchencoder/skylb-apiv2/proto"
12+
)
13+
14+
var (
15+
ErrBalancerNameMatchMiss = errors.New("balancer name match miss")
16+
)
17+
18+
// SkyLbKeeper defines the interface for a SkyLB keeper.
19+
type SkyLbKeeper interface {
20+
// RegisterService registers the service with the given spec to the keeper
21+
// and returns a channel for the service instance updates.
22+
RegisterService(spec *pb.ServiceSpec) <-chan []*resolver.Address
23+
24+
// Start starts the keeper with the given client service ID and name.
25+
Start(csId vexpb.ServiceId, csName string, resolveFullEps bool)
26+
27+
// Shutdown shuts down the keeper.
28+
Shutdown()
29+
30+
// WaitUntilReady blocks the caller until the keeper receives the initial
31+
// endpoints for all service specs.
32+
WaitUntilReady()
33+
}
34+
35+
// LoadBalanceHandler defines the interface to handle the notification logic
36+
// for different clients in SkyLB API load balancing.
37+
type LoadBalanceHandler interface {
38+
// Returns the service spec for this handler.
39+
ServiceSpec() *pb.ServiceSpec
40+
41+
// BeforeResolve is called before SkyLB API resolves the given spec.
42+
BeforeResolve(spec *pb.ServiceSpec, ropts *ResolveOptions)
43+
44+
// AfterResolve is called after SkyLB API resolved the given spec.
45+
AfterResolve(spec *pb.ServiceSpec, csId vexpb.ServiceId, csName string, keeper SkyLbKeeper, tracer opentracing.Tracer, failFast bool)
46+
47+
// OnShutdown is called when the SkyLB API is shutting down.
48+
OnShutdown()
49+
}
50+
51+
// // BalancerCreator is a function which get a grpc Balancer.
52+
type BalancerCreator func(balancerName string) balancer.Builder
53+
54+
// // ResolveOptions configure a resolve call.
55+
type ResolveOptions struct {
56+
balancerCreator BalancerCreator
57+
}
58+
59+
// BalancerCreator returns the load balancer creator.
60+
func (ropts *ResolveOptions) BalancerCreator() BalancerCreator {
61+
return ropts.balancerCreator
62+
}
63+
64+
// // ResolveOption configures how we set up the resolve call.
65+
type ResolveOption func(*ResolveOptions)
66+
67+
// // WithBalancerCreator returns a ResolveOption which sets a load
68+
// // balancer creator.
69+
// func WithBalancerCreator(balancerName string) ResolveOption {
70+
// builder := balancer.Get(balancerName)
71+
// if nil == builder {
72+
// panic(ErrNoBalancer)
73+
// }
74+
75+
// return func(o *ResolveOptions) {
76+
// o.balancerCreator = bc
77+
// }
78+
79+
// return grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`)
80+
// }

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ require (
1515
google.golang.org/protobuf v1.27.1
1616
)
1717

18+
require (
19+
github.com/davecgh/go-spew v1.1.1 // indirect
20+
github.com/pmezard/go-difflib v1.0.0 // indirect
21+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
22+
)
23+
1824
require (
1925
github.com/VividCortex/gohistogram v1.0.0 // indirect
2026
github.com/beorn7/perks v1.0.1 // indirect
@@ -31,6 +37,7 @@ require (
3137
github.com/prometheus/client_model v0.2.0 // indirect
3238
github.com/prometheus/common v0.10.0 // indirect
3339
github.com/prometheus/procfs v0.1.3 // indirect
40+
github.com/stretchr/testify v1.7.1
3441
github.com/uber/jaeger-client-go v2.24.0+incompatible // indirect
3542
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
3643
go.uber.org/atomic v1.6.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
295295
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
296296
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
297297
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
298+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
299+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
298300
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
299301
github.com/uber/jaeger-client-go v2.24.0+incompatible h1:CGchgJcHsDd2jWnaL4XngByMrXoGHh3n8oCqAKx0uMo=
300302
github.com/uber/jaeger-client-go v2.24.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=

handlers/grpc.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ func (glbh *GrpcLoadBalanceHandler) AddUnaryInterceptor(incept grpc.UnaryClientI
3838
glbh.unaryInterceptors = append(glbh.unaryInterceptors, incept)
3939
}
4040

41+
func (glbh *GrpcLoadBalanceHandler) BeforeResolve(spec *pb.ServiceSpec, ropts *option.ResolveOptions) {
42+
// var balancer grpc.Balancer
43+
// bc := ropts.BalancerCreator()
44+
// if bc == nil {
45+
// if *cflags.EnableHealthCheck {
46+
// balancer = jb.RoundRobin(r)
47+
// } else {
48+
// balancer = grpc.RoundRobin(r)
49+
// }
50+
// glog.Infof("Skylb client created round-robin load balancer.")
51+
// } else {
52+
// balancer = bc(r)
53+
54+
// glog.Infof("Skylb client created %v load balancer.", reflect.TypeOf(balancer))
55+
// }
56+
// glbh.lbs[spec.String()] = balancer
57+
58+
// if lb, ok := balancer.(jb.DebugBalancer); ok {
59+
// glog.Infof("StartDebugPrint for %v", reflect.TypeOf(lb))
60+
// lb.StartDebugPrint(*cflags.DebugSvcInterval)
61+
// }
62+
}
63+
4164
func (glbh *GrpcLoadBalanceHandler) AfterResolve(spec *pb.ServiceSpec, csId vexpb.ServiceId, csName string, keeper option.SkyLbKeeper, tracer opentracing.Tracer, failFast bool) {
4265
openTracingInterceptor := otgrpc.OpenTracingClientInterceptor(tracer)
4366

internal/rpccli/rpc.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/binchencoder/letsgo/strings"
1616
"github.com/binchencoder/skylb-apiv2/internal/flags"
1717
pb "github.com/binchencoder/skylb-apiv2/proto"
18+
"github.com/binchencoder/skylb-apiv2/resolver"
1819
)
1920

2021
func init() {
@@ -61,7 +62,8 @@ func NewGrpcClient(ctx context.Context) (pb.SkylbClient, error) {
6162
}
6263
glog.Infof("Connecting SkyLB instance %s on port %s", ep, port)
6364

64-
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", ep, port), grpc.WithInsecure(), grpc.WithTimeout(time.Second), grpc.WithBlock())
65+
conn, err := grpc.Dial(resolver.BuildSkyLBTarget(fmt.Sprintf("%s:%s", ep, port)),
66+
grpc.WithInsecure(), grpc.WithTimeout(time.Second), grpc.WithBlock())
6567
if err != nil {
6668
glog.Errorf("Failed to dial to SkyLB instance %s, %+v.", ep, err)
6769
return nil, err

0 commit comments

Comments
 (0)