Skip to content

Commit ce81867

Browse files
committed
Temp commit
1 parent efc9692 commit ce81867

26 files changed

+512
-669
lines changed

client/client.go

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
package client
22

33
import (
4+
"flag"
5+
46
"google.golang.org/grpc"
57

68
vexpb "github.com/binchencoder/gateway-proto/data"
7-
"github.com/binchencoder/skylb-apiv2/client/option"
9+
"github.com/binchencoder/letsgo/flags"
810
"github.com/binchencoder/skylb-apiv2/internal/skylb"
11+
"github.com/binchencoder/skylb-apiv2/naming"
912
pb "github.com/binchencoder/skylb-apiv2/proto"
1013
)
1114

12-
// TODO(zhwang): remove this file once we migrate all references to the
13-
// new API.
15+
const (
16+
DefaultNameSpace = "default"
17+
DefaultPortName = "grpc"
18+
)
19+
20+
var (
21+
DebugSvcEndpoints = flags.StringMap{}
22+
)
23+
24+
func init() {
25+
flag.Var(&DebugSvcEndpoints, "debug-svc-endpoint", "The debug service endpoint. If not empty, disable SkyLB resolving for that service.")
26+
}
1427

1528
// ServiceCli defines the interface through which the client app obtains
1629
// gRPC load balancing support from SkyLB.
17-
//
18-
// Deprecated: use ServiceLocator instead.
1930
type ServiceCli interface {
2031
// Resolve resolves a service spec.
2132
// It needs to be called for every service used by the client.
22-
Resolve(spec *pb.ServiceSpec, opts ...option.ResolveOption)
33+
Resolve(spec *pb.ServiceSpec, opts ...grpc.DialOption)
2334

2435
// EnableHistogram enables historgram in client api metrics.
2536
//
@@ -39,13 +50,39 @@ type ServiceCli interface {
3950
//
4051
// Start can only be called once for each ServiceCli instance in the whole
4152
// lifecycle of an application.
42-
Start(callback func(spec *pb.ServiceSpec, conn *grpc.ClientConn), options ...grpc.DialOption)
53+
Start(callback func(spec *pb.ServiceSpec, conn *grpc.ClientConn))
4354

4455
// Shutdown turns the service client down. After shutdown, all grpc.Balancer
4556
// objects returned from Resolve() call can not be used any more.
4657
Shutdown()
4758
}
4859

60+
// NewServiceSpec returns a new ServiceSpec struct with the given parameters.
61+
func NewServiceSpec(namespace string, serviceId vexpb.ServiceId, portName string) *pb.ServiceSpec {
62+
if namespace == "" {
63+
namespace = DefaultNameSpace
64+
}
65+
if portName == "" {
66+
portName = DefaultPortName
67+
}
68+
serviceName, err := naming.ServiceIdToName(serviceId)
69+
if err != nil {
70+
panic("Unknown service ID.")
71+
}
72+
73+
return &pb.ServiceSpec{
74+
Namespace: namespace,
75+
ServiceName: serviceName,
76+
PortName: portName,
77+
}
78+
}
79+
80+
// NewDefaultServiceSpec returns a new ServiceSpec struct with the default
81+
// namespace and default port name.
82+
func NewDefaultServiceSpec(serviceId vexpb.ServiceId) *pb.ServiceSpec {
83+
return NewServiceSpec(DefaultNameSpace, serviceId, DefaultPortName)
84+
}
85+
4986
// NewServiceCli returns a new service client. NewServiceCli() should be called
5087
// once in the whole lifecycle of an application.
5188
func NewServiceCli(clientServiceId vexpb.ServiceId) ServiceCli {

client/example_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@ import (
44
"google.golang.org/grpc"
55

66
vexpb "github.com/binchencoder/gateway-proto/data"
7-
"github.com/binchencoder/skylb-apiv2/handlers"
7+
pb "github.com/binchencoder/skylb-apiv2/proto"
88
)
99

1010
func ExampleNewServiceLocator() {
11-
// Create a service locator.
12-
sl := NewServiceLocator(vexpb.ServiceId_SHARED_TEST_CLIENT_SERVICE)
11+
// Create a service client.
12+
cli := NewServiceCli(vexpb.ServiceId_SHARED_TEST_CLIENT_SERVICE)
1313

1414
// 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)
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+
cli.Resolve(NewDefaultServiceSpec(vexpb.ServiceId_SHARED_TEST_SERVER_SERVICE))
2224

2325
// Start the locator.
24-
sl.Start()
26+
// sl.Start()
27+
28+
cli.Start(func(spec *pb.ServiceSpec, conn *grpc.ClientConn) {
29+
// hold the connecton for use later.
30+
})
2531

2632
// Use the connection to create grpc clients.
2733

2834
// Shutdown before exit.
29-
sl.Shutdown()
35+
cli.Shutdown()
3036
}

client/locator.go

Lines changed: 0 additions & 92 deletions
This file was deleted.

client/option/options.go

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package option
33
import (
44
"errors"
55

6-
opentracing "github.com/opentracing/opentracing-go"
7-
"google.golang.org/grpc/balancer"
8-
"google.golang.org/grpc/resolver"
9-
106
vexpb "github.com/binchencoder/gateway-proto/data"
117
pb "github.com/binchencoder/skylb-apiv2/proto"
8+
"google.golang.org/grpc/resolver"
129
)
1310

1411
var (
@@ -17,9 +14,9 @@ var (
1714

1815
// SkyLbKeeper defines the interface for a SkyLB keeper.
1916
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
17+
// RegisterServiceCliConn registers the service resolver.ClientConn
18+
// with the given spec to the keeper
19+
RegisterServiceCliConn(spec *pb.ServiceSpec, cliConn resolver.ClientConn)
2320

2421
// Start starts the keeper with the given client service ID and name.
2522
Start(csId vexpb.ServiceId, csName string, resolveFullEps bool)
@@ -34,38 +31,37 @@ type SkyLbKeeper interface {
3431

3532
// LoadBalanceHandler defines the interface to handle the notification logic
3633
// for different clients in SkyLB API load balancing.
37-
type LoadBalanceHandler interface {
38-
// Returns the service spec for this handler.
39-
ServiceSpec() *pb.ServiceSpec
34+
// type LoadBalanceHandler interface {
35+
// // Returns the service spec for this handler.
36+
// ServiceSpec() *pb.ServiceSpec
4037

41-
// BeforeResolve is called before SkyLB API resolves the given spec.
42-
BeforeResolve(spec *pb.ServiceSpec, ropts *ResolveOptions)
38+
// // BeforeResolve is called before SkyLB API resolves the given spec.
39+
// BeforeResolve(spec *pb.ServiceSpec, ropts *ResolveOptions)
4340

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)
41+
// // AfterResolve is called after SkyLB API resolved the given spec.
42+
// AfterResolve(spec *pb.ServiceSpec, csId vexpb.ServiceId, csName string, keeper SkyLbKeeper, tracer opentracing.Tracer, failFast bool)
4643

47-
// OnShutdown is called when the SkyLB API is shutting down.
48-
OnShutdown()
49-
}
44+
// // OnShutdown is called when the SkyLB API is shutting down.
45+
// OnShutdown()
46+
// }
5047

5148
// // BalancerCreator is a function which get a grpc Balancer.
52-
type BalancerCreator func(balancerName string) balancer.Builder
49+
// type BalancerCreator func(balancerName string) balancer.Builder
5350

54-
// // ResolveOptions configure a resolve call.
55-
type ResolveOptions struct {
56-
balancerCreator BalancerCreator
57-
}
51+
// ResolveOptions configure a resolve call.
52+
// type ResolveOptions struct {
53+
// balancerCreator BalancerCreator
54+
// }
5855

5956
// BalancerCreator returns the load balancer creator.
60-
func (ropts *ResolveOptions) BalancerCreator() BalancerCreator {
61-
return ropts.balancerCreator
62-
}
57+
// func (ropts *ResolveOptions) BalancerCreator() BalancerCreator {
58+
// return ropts.balancerCreator
59+
// }
6360

64-
// // ResolveOption configures how we set up the resolve call.
65-
type ResolveOption func(*ResolveOptions)
61+
// ResolveOption configures how we set up the resolve call.
62+
// type ResolveOption func(*ResolveOptions)
6663

67-
// // WithBalancerCreator returns a ResolveOption which sets a load
68-
// // balancer creator.
64+
// WithBalancerCreator returns a ResolveOption which sets a load balancer creator.
6965
// func WithBalancerCreator(balancerName string) ResolveOption {
7066
// builder := balancer.Get(balancerName)
7167
// if nil == builder {

handlers/BUILD.bazel

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)