feat: add shutdown api (#151)

This commit is contained in:
Hugome 2021-03-25 07:45:54 +01:00 committed by GitHub
parent 7a89cf3c41
commit 562562aaef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 31 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
pb "github.com/dapr/go-sdk/dapr/proto/runtime/v1"
)
@ -90,6 +91,9 @@ type Client interface {
// DeleteBulkState deletes content for multiple keys from store.
DeleteBulkStateItems(ctx context.Context, storeName string, items []*DeleteStateItem) error
// Shutdown the sidecar.
Shutdown(ctx context.Context) error
// WithTraceID adds existing trace ID to the outgoing context.
WithTraceID(ctx context.Context, id string) context.Context
@ -194,3 +198,12 @@ func (c *GRPCClient) withAuthToken(ctx context.Context) context.Context {
}
return metadata.NewOutgoingContext(ctx, metadata.Pairs(apiTokenKey, string(c.authToken)))
}
// Shutdown the sidecar.
func (c *GRPCClient) Shutdown(ctx context.Context) error {
_, err := c.protoClient.Shutdown(c.withAuthToken(ctx), &emptypb.Empty{})
if err != nil {
return errors.Wrap(err, "error shutting down the sidecar")
}
return nil
}

View File

@ -63,6 +63,16 @@ func TestNewClient(t *testing.T) {
})
}
func TestShutdown(t *testing.T) {
ctx := context.Background()
t.Run("shutdown", func(t *testing.T) {
err := testClient.Shutdown(ctx)
assert.NoError(t, err)
})
}
func getTestClient(ctx context.Context) (client Client, closer func()) {
s := grpc.NewServer()
pb.RegisterDaprServer(s, &testDaprServer{
@ -220,3 +230,7 @@ func (s *testDaprServer) RegisterActorTimer(context.Context, *pb.RegisterActorTi
func (s *testDaprServer) UnregisterActorTimer(context.Context, *pb.UnregisterActorTimerRequest) (*empty.Empty, error) {
return nil, errors.New("actors not implemented in go SDK")
}
func (s *testDaprServer) Shutdown(ctx context.Context, req *empty.Empty) (*empty.Empty, error) {
return &empty.Empty{}, nil
}

View File

@ -2561,7 +2561,7 @@ var file_dapr_proto_runtime_v1_dapr_proto_rawDesc = []byte{
0x61, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18,
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61,
0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65,
0x32, 0xc7, 0x0f, 0x0a, 0x04, 0x44, 0x61, 0x70, 0x72, 0x12, 0x64, 0x0a, 0x0d, 0x49, 0x6e, 0x76,
0x32, 0x85, 0x10, 0x0a, 0x04, 0x44, 0x61, 0x70, 0x72, 0x12, 0x64, 0x0a, 0x0d, 0x49, 0x6e, 0x76,
0x6f, 0x6b, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x2b, 0x2e, 0x64, 0x61, 0x70,
0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x2e,
0x76, 0x31, 0x2e, 0x49, 0x6e, 0x76, 0x6f, 0x6b, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65,
@ -2685,14 +2685,18 @@ var file_dapr_proto_runtime_v1_dapr_proto_rawDesc = []byte{
0x74, 0x6f, 0x2e, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x53, 0x65,
0x74, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x42, 0x6c, 0x0a, 0x0a, 0x69, 0x6f,
0x2e, 0x64, 0x61, 0x70, 0x72, 0x2e, 0x76, 0x31, 0x42, 0x0a, 0x44, 0x61, 0x70, 0x72, 0x50, 0x72,
0x6f, 0x74, 0x6f, 0x73, 0x5a, 0x34, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x2f, 0x64, 0x61, 0x70, 0x72, 0x2f, 0x67, 0x6f, 0x2d, 0x73, 0x64, 0x6b, 0x2f, 0x64, 0x61, 0x70,
0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x2f,
0x76, 0x31, 0x3b, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0xaa, 0x02, 0x1b, 0x44, 0x61, 0x70,
0x72, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2e, 0x41, 0x75, 0x74, 0x6f, 0x67, 0x65, 0x6e,
0x2e, 0x47, 0x72, 0x70, 0x63, 0x2e, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x3c, 0x0a, 0x08, 0x53, 0x68,
0x75, 0x74, 0x64, 0x6f, 0x77, 0x6e, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x16,
0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66,
0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x42, 0x6c, 0x0a, 0x0a, 0x69, 0x6f, 0x2e, 0x64,
0x61, 0x70, 0x72, 0x2e, 0x76, 0x31, 0x42, 0x0a, 0x44, 0x61, 0x70, 0x72, 0x50, 0x72, 0x6f, 0x74,
0x6f, 0x73, 0x5a, 0x34, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64,
0x61, 0x70, 0x72, 0x2f, 0x67, 0x6f, 0x2d, 0x73, 0x64, 0x6b, 0x2f, 0x64, 0x61, 0x70, 0x72, 0x2f,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x2f, 0x76, 0x31,
0x3b, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0xaa, 0x02, 0x1b, 0x44, 0x61, 0x70, 0x72, 0x2e,
0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2e, 0x41, 0x75, 0x74, 0x6f, 0x67, 0x65, 0x6e, 0x2e, 0x47,
0x72, 0x70, 0x63, 0x2e, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -2816,28 +2820,30 @@ var file_dapr_proto_runtime_v1_dapr_proto_depIdxs = []int32{
27, // 46: dapr.proto.runtime.v1.Dapr.InvokeActor:input_type -> dapr.proto.runtime.v1.InvokeActorRequest
54, // 47: dapr.proto.runtime.v1.Dapr.GetMetadata:input_type -> google.protobuf.Empty
32, // 48: dapr.proto.runtime.v1.Dapr.SetMetadata:input_type -> dapr.proto.runtime.v1.SetMetadataRequest
55, // 49: dapr.proto.runtime.v1.Dapr.InvokeService:output_type -> dapr.proto.common.v1.InvokeResponse
5, // 50: dapr.proto.runtime.v1.Dapr.GetState:output_type -> dapr.proto.runtime.v1.GetStateResponse
3, // 51: dapr.proto.runtime.v1.Dapr.GetBulkState:output_type -> dapr.proto.runtime.v1.GetBulkStateResponse
54, // 52: dapr.proto.runtime.v1.Dapr.SaveState:output_type -> google.protobuf.Empty
54, // 53: dapr.proto.runtime.v1.Dapr.DeleteState:output_type -> google.protobuf.Empty
54, // 54: dapr.proto.runtime.v1.Dapr.DeleteBulkState:output_type -> google.protobuf.Empty
54, // 55: dapr.proto.runtime.v1.Dapr.ExecuteStateTransaction:output_type -> google.protobuf.Empty
54, // 56: dapr.proto.runtime.v1.Dapr.PublishEvent:output_type -> google.protobuf.Empty
11, // 57: dapr.proto.runtime.v1.Dapr.InvokeBinding:output_type -> dapr.proto.runtime.v1.InvokeBindingResponse
13, // 58: dapr.proto.runtime.v1.Dapr.GetSecret:output_type -> dapr.proto.runtime.v1.GetSecretResponse
16, // 59: dapr.proto.runtime.v1.Dapr.GetBulkSecret:output_type -> dapr.proto.runtime.v1.GetBulkSecretResponse
54, // 60: dapr.proto.runtime.v1.Dapr.RegisterActorTimer:output_type -> google.protobuf.Empty
54, // 61: dapr.proto.runtime.v1.Dapr.UnregisterActorTimer:output_type -> google.protobuf.Empty
54, // 62: dapr.proto.runtime.v1.Dapr.RegisterActorReminder:output_type -> google.protobuf.Empty
54, // 63: dapr.proto.runtime.v1.Dapr.UnregisterActorReminder:output_type -> google.protobuf.Empty
24, // 64: dapr.proto.runtime.v1.Dapr.GetActorState:output_type -> dapr.proto.runtime.v1.GetActorStateResponse
54, // 65: dapr.proto.runtime.v1.Dapr.ExecuteActorStateTransaction:output_type -> google.protobuf.Empty
28, // 66: dapr.proto.runtime.v1.Dapr.InvokeActor:output_type -> dapr.proto.runtime.v1.InvokeActorResponse
29, // 67: dapr.proto.runtime.v1.Dapr.GetMetadata:output_type -> dapr.proto.runtime.v1.GetMetadataResponse
54, // 68: dapr.proto.runtime.v1.Dapr.SetMetadata:output_type -> google.protobuf.Empty
49, // [49:69] is the sub-list for method output_type
29, // [29:49] is the sub-list for method input_type
54, // 49: dapr.proto.runtime.v1.Dapr.Shutdown:input_type -> google.protobuf.Empty
55, // 50: dapr.proto.runtime.v1.Dapr.InvokeService:output_type -> dapr.proto.common.v1.InvokeResponse
5, // 51: dapr.proto.runtime.v1.Dapr.GetState:output_type -> dapr.proto.runtime.v1.GetStateResponse
3, // 52: dapr.proto.runtime.v1.Dapr.GetBulkState:output_type -> dapr.proto.runtime.v1.GetBulkStateResponse
54, // 53: dapr.proto.runtime.v1.Dapr.SaveState:output_type -> google.protobuf.Empty
54, // 54: dapr.proto.runtime.v1.Dapr.DeleteState:output_type -> google.protobuf.Empty
54, // 55: dapr.proto.runtime.v1.Dapr.DeleteBulkState:output_type -> google.protobuf.Empty
54, // 56: dapr.proto.runtime.v1.Dapr.ExecuteStateTransaction:output_type -> google.protobuf.Empty
54, // 57: dapr.proto.runtime.v1.Dapr.PublishEvent:output_type -> google.protobuf.Empty
11, // 58: dapr.proto.runtime.v1.Dapr.InvokeBinding:output_type -> dapr.proto.runtime.v1.InvokeBindingResponse
13, // 59: dapr.proto.runtime.v1.Dapr.GetSecret:output_type -> dapr.proto.runtime.v1.GetSecretResponse
16, // 60: dapr.proto.runtime.v1.Dapr.GetBulkSecret:output_type -> dapr.proto.runtime.v1.GetBulkSecretResponse
54, // 61: dapr.proto.runtime.v1.Dapr.RegisterActorTimer:output_type -> google.protobuf.Empty
54, // 62: dapr.proto.runtime.v1.Dapr.UnregisterActorTimer:output_type -> google.protobuf.Empty
54, // 63: dapr.proto.runtime.v1.Dapr.RegisterActorReminder:output_type -> google.protobuf.Empty
54, // 64: dapr.proto.runtime.v1.Dapr.UnregisterActorReminder:output_type -> google.protobuf.Empty
24, // 65: dapr.proto.runtime.v1.Dapr.GetActorState:output_type -> dapr.proto.runtime.v1.GetActorStateResponse
54, // 66: dapr.proto.runtime.v1.Dapr.ExecuteActorStateTransaction:output_type -> google.protobuf.Empty
28, // 67: dapr.proto.runtime.v1.Dapr.InvokeActor:output_type -> dapr.proto.runtime.v1.InvokeActorResponse
29, // 68: dapr.proto.runtime.v1.Dapr.GetMetadata:output_type -> dapr.proto.runtime.v1.GetMetadataResponse
54, // 69: dapr.proto.runtime.v1.Dapr.SetMetadata:output_type -> google.protobuf.Empty
54, // 70: dapr.proto.runtime.v1.Dapr.Shutdown:output_type -> google.protobuf.Empty
50, // [50:71] is the sub-list for method output_type
29, // [29:50] is the sub-list for method input_type
29, // [29:29] is the sub-list for extension type_name
29, // [29:29] is the sub-list for extension extendee
0, // [0:29] is the sub-list for field type_name

View File

@ -60,6 +60,8 @@ type DaprClient interface {
GetMetadata(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*GetMetadataResponse, error)
// Sets value in extended metadata of the sidecar
SetMetadata(ctx context.Context, in *SetMetadataRequest, opts ...grpc.CallOption) (*emptypb.Empty, error)
// Shutdown the sidecar
Shutdown(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error)
}
type daprClient struct {
@ -250,6 +252,15 @@ func (c *daprClient) SetMetadata(ctx context.Context, in *SetMetadataRequest, op
return out, nil
}
func (c *daprClient) Shutdown(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error) {
out := new(emptypb.Empty)
err := c.cc.Invoke(ctx, "/dapr.proto.runtime.v1.Dapr/Shutdown", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// DaprServer is the server API for Dapr service.
// All implementations must embed UnimplementedDaprServer
// for forward compatibility
@ -294,6 +305,8 @@ type DaprServer interface {
GetMetadata(context.Context, *emptypb.Empty) (*GetMetadataResponse, error)
// Sets value in extended metadata of the sidecar
SetMetadata(context.Context, *SetMetadataRequest) (*emptypb.Empty, error)
// Shutdown the sidecar
Shutdown(context.Context, *emptypb.Empty) (*emptypb.Empty, error)
mustEmbedUnimplementedDaprServer()
}
@ -361,6 +374,9 @@ func (UnimplementedDaprServer) GetMetadata(context.Context, *emptypb.Empty) (*Ge
func (UnimplementedDaprServer) SetMetadata(context.Context, *SetMetadataRequest) (*emptypb.Empty, error) {
return nil, status.Errorf(codes.Unimplemented, "method SetMetadata not implemented")
}
func (UnimplementedDaprServer) Shutdown(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
return nil, status.Errorf(codes.Unimplemented, "method Shutdown not implemented")
}
func (UnimplementedDaprServer) mustEmbedUnimplementedDaprServer() {}
// UnsafeDaprServer may be embedded to opt out of forward compatibility for this service.
@ -734,6 +750,24 @@ func _Dapr_SetMetadata_Handler(srv interface{}, ctx context.Context, dec func(in
return interceptor(ctx, in, info, handler)
}
func _Dapr_Shutdown_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(emptypb.Empty)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(DaprServer).Shutdown(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/dapr.proto.runtime.v1.Dapr/Shutdown",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(DaprServer).Shutdown(ctx, req.(*emptypb.Empty))
}
return interceptor(ctx, in, info, handler)
}
// Dapr_ServiceDesc is the grpc.ServiceDesc for Dapr service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
@ -821,6 +855,10 @@ var Dapr_ServiceDesc = grpc.ServiceDesc{
MethodName: "SetMetadata",
Handler: _Dapr_SetMetadata_Handler,
},
{
MethodName: "Shutdown",
Handler: _Dapr_Shutdown_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "dapr/proto/runtime/v1/dapr.proto",