Skip to content

Commit

Permalink
Expose underlying gRPC client and server objects
Browse files Browse the repository at this point in the history
Fixes dapr#204

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
  • Loading branch information
ItalyPaleAle committed Aug 8, 2022
1 parent 7c38f5a commit 865a1a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
9 changes: 6 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,21 @@ type GRPCClient struct {
ctxCancelFunc context.CancelFunc
protoClient pb.DaprClient
authToken string
mux sync.Mutex
}

// Close cleans up all resources created by the client.
func (c *GRPCClient) Close() {
c.ctxCancelFunc()
if c.connection != nil {
c.connection.Close()
c.connection = nil
}
}

// WithAuthToken sets Dapr API token on the instantiated client.
// Allows empty string to reset token on existing client.
func (c *GRPCClient) WithAuthToken(token string) {
c.mux.Lock()
c.authToken = token
c.mux.Unlock()
}

// WithTraceID adds existing trace ID to the outgoing context.
Expand Down Expand Up @@ -346,3 +344,8 @@ func (c *GRPCClient) Shutdown(ctx context.Context) error {
func (c *GRPCClient) GrpcClient() pb.DaprClient {
return c.protoClient
}

// GrpcClientConn returns the grpc.ClientConn object used by this client.
func (c *GRPCClient) GrpcClientConn() *grpc.ClientConn {
return c.connection
}
35 changes: 28 additions & 7 deletions service/grpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ func NewServiceWithListener(lis net.Listener) common.Service {
}

func newService(lis net.Listener) *Server {
return &Server{
s := &Server{
listener: lis,
invokeHandlers: make(map[string]common.ServiceInvocationHandler),
topicRegistrar: make(internal.TopicRegistrar),
bindingHandlers: make(map[string]common.BindingInvocationHandler),
authToken: os.Getenv(common.AppAPITokenEnvVar),
}

gs := grpc.NewServer()
pb.RegisterAppCallbackServer(gs, s)
s.grpcServer = gs

return s
}

// Server is the gRPC service implementation for Dapr.
Expand All @@ -74,18 +80,33 @@ func (s *Server) RegisterActorImplFactory(f actor.Factory, opts ...config.Option

// Start registers the server and starts it.
func (s *Server) Start() error {
gs := grpc.NewServer()
pb.RegisterAppCallbackServer(gs, s)
s.grpcServer = gs
return gs.Serve(s.listener)
if s.grpcServer == nil {
return errors.New("cannot restart a stopped gRPC server")
}
return s.grpcServer.Serve(s.listener)
}

// Stop stops the previously started service.
// Stop stops the previously-started service.
func (s *Server) Stop() error {
return s.listener.Close()
if s.grpcServer == nil {
return nil
}
s.grpcServer.Stop()
s.grpcServer = nil
return nil
}

// GrecefulStop stops the previously-started service gracefully.
func (s *Server) GracefulStop() error {
if s.grpcServer == nil {
return nil
}
s.grpcServer.GracefulStop()
s.grpcServer = nil
return nil
}

// GrpcServer returns the grpc.Server object managed by the server.
func (s *Server) GrpcServer() *grpc.Server {
return s.grpcServer
}

0 comments on commit 865a1a3

Please sign in to comment.