Skip to content

Commit

Permalink
RSDK-8832 - add docs around foreign service handler (#4381)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheukt committed Sep 24, 2024
1 parent 2374ffb commit 8f80637
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions module/modmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,9 @@ func (mgr *Manager) newOnUnexpectedExitHandler(mod *module) func(exitCode int) b
if orphanedResourceNames := mgr.attemptRestart(mgr.restartCtx, mod); orphanedResourceNames != nil {
if mgr.removeOrphanedResources != nil {
mgr.removeOrphanedResources(mgr.restartCtx, orphanedResourceNames)
rNames := make([]string, len(orphanedResourceNames))
for idx, rName := range orphanedResourceNames {
rNames[idx] = rName.String()
rNames := make([]string, 0, len(orphanedResourceNames))
for _, rName := range orphanedResourceNames {
rNames = append(rNames, rName.String())
}
mgr.logger.Debugw("Removed resources after failed module restart", "module", mod.cfg.Name, "resources", rNames)
}
Expand Down
4 changes: 2 additions & 2 deletions robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ func TypeAndMethodDescFromMethod(r Robot, method string) (*resource.RPCAPI, *des
if len(methodParts) != 3 {
return nil, nil, grpc.UnimplementedError
}
protoSvc := methodParts[1]
protoMethod := methodParts[2]
protoSvc := methodParts[1] // e.g. viam.component.arm.v1.ArmService
protoMethod := methodParts[2] // e.g. DoCommand

var foundType *resource.RPCAPI
for _, resAPI := range r.ResourceRPCAPIs() {
Expand Down
10 changes: 10 additions & 0 deletions robot/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,12 @@ func (svc *webService) initMux(options weboptions.Options) (*goji.Mux, error) {
return mux, nil
}

// foreignServiceHandler is a bidi-streaming RPC service handler to support custom APIs.
// It is invoked instead of returning the "unimplemented" gRPC error whenever a request is received for
// an unregistered service or method. These method could be registered on a remote viam-server or a module server
// so this handler will attempt to route the request to the correct next node in the chain.
func (svc *webService) foreignServiceHandler(srv interface{}, stream googlegrpc.ServerStream) error {
// method will be in the form of PackageName.ServiceName/MethodName
method, ok := googlegrpc.MethodFromServerStream(stream)
if !ok {
return grpc.UnimplementedError
Expand All @@ -939,10 +944,15 @@ func (svc *webService) foreignServiceHandler(srv interface{}, stream googlegrpc.

firstMsg := dynamic.NewMessage(methodDesc.GetInputType())

// The stream blocks until it receives a message and attempts to deserialize
// the message into firstMsg - it will error out if the received message cannot
// be marshalled into the expected type.
if err := stream.RecvMsg(firstMsg); err != nil {
return err
}

// We expect each message to contain a "name" argument which will allow us to route
// the message towards the correct destination.
resource, fqName, err := robot.ResourceFromProtoMessage(svc.r, firstMsg, subType.API)
if err != nil {
svc.logger.Errorw("unable to route foreign message", "error", err)
Expand Down

0 comments on commit 8f80637

Please sign in to comment.