Skip to content

Commit ba8417e

Browse files
committed
lint
1 parent bafbe9a commit ba8417e

File tree

6 files changed

+47
-35
lines changed

6 files changed

+47
-35
lines changed

components/camera/client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ func setupRealRobot(
623623
t *testing.T,
624624
robotConfig *config.Config,
625625
logger logging.Logger,
626-
) (context.Context, robot.LocalRobot, string, web.Service) {
626+
) (context.Context, robot.LocalRobot, string, web.ServiceI) {
627627
t.Helper()
628628

629629
ctx := context.Background()
@@ -648,7 +648,7 @@ func setupRealRobotWithOptions(
648648
robotConfig *config.Config,
649649
logger logging.Logger,
650650
options weboptions.Options,
651-
) (context.Context, robot.LocalRobot, web.Service) {
651+
) (context.Context, robot.LocalRobot, web.ServiceI) {
652652
t.Helper()
653653

654654
ctx := context.Background()

robot/impl/local_robot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type localRobot struct {
8383
configRevisionMu sync.RWMutex
8484

8585
// internal services that are in the graph but we also hold onto
86-
webSvc *web.WebService
86+
webSvc *web.Service
8787
frameSvc framesystem.Service
8888

8989
// map keyed by Module.Name. This is necessary to get the package manager to use a new folder

robot/web/graphviz_c.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
"time"
1111

1212
"github.com/goccy/go-graphviz"
13+
1314
"go.viam.com/rdk/robot"
1415
)
1516

16-
func (svc *WebService) handleVisualizeResourceGraph(w http.ResponseWriter, r *http.Request) {
17+
func (svc *Service) handleVisualizeResourceGraph(w http.ResponseWriter, r *http.Request) {
1718
localRobot, isLocal := svc.r.(robot.LocalRobot)
1819
if !isLocal {
1920
return

robot/web/stream/stream_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
// streaming capabilities.
3333
//
3434
//nolint:lll
35-
func setupRealRobot(t *testing.T, robotConfig *config.Config, logger logging.Logger) (context.Context, robot.LocalRobot, string, web.Service) {
35+
func setupRealRobot(t *testing.T, robotConfig *config.Config, logger logging.Logger) (context.Context, robot.LocalRobot, string, web.ServiceI) {
3636
t.Helper()
3737

3838
ctx := context.Background()

robot/web/web.go

+36-25
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ var API = resource.APINamespaceRDKInternal.WithServiceType(SubtypeName)
5959
// InternalServiceName is used to refer to/depend on this service internally.
6060
var InternalServiceName = resource.NewName(API, "builtin")
6161

62-
type WebService struct {
62+
// Service is can bind to a socket and will register gRPC handlers.
63+
type Service struct {
6364
resource.Named
6465

6566
mu sync.Mutex
@@ -81,8 +82,8 @@ type WebService struct {
8182
counter *RequestCounters
8283
}
8384

84-
// A Service controls the web server for a robot.
85-
type Service interface {
85+
// A ServiceI controls the web server for a robot.
86+
type ServiceI interface {
8687
resource.Resource
8788

8889
// Start starts the web server
@@ -108,12 +109,13 @@ var internalWebServiceName = resource.NewName(
108109
"builtin",
109110
)
110111

111-
func (svc *WebService) Name() resource.Name {
112+
// Name returns a resource name object for the web service.
113+
func (svc *Service) Name() resource.Name {
112114
return internalWebServiceName
113115
}
114116

115117
// Start starts the web server, will return an error if server is already up.
116-
func (svc *WebService) Start(ctx context.Context, o weboptions.Options) error {
118+
func (svc *Service) Start(ctx context.Context, o weboptions.Options) error {
117119
svc.mu.Lock()
118120
defer svc.mu.Unlock()
119121
if svc.isRunning {
@@ -163,21 +165,21 @@ func RunWebWithConfig(ctx context.Context, r robot.LocalRobot, cfg *config.Confi
163165
}
164166

165167
// Address returns the address the service is listening on.
166-
func (svc *WebService) Address() string {
168+
func (svc *Service) Address() string {
167169
svc.mu.Lock()
168170
defer svc.mu.Unlock()
169171
return svc.addr
170172
}
171173

172174
// ModuleAddress returns the unix socket path the module server is listening on.
173-
func (svc *WebService) ModuleAddress() string {
175+
func (svc *Service) ModuleAddress() string {
174176
svc.mu.Lock()
175177
defer svc.mu.Unlock()
176178
return svc.modAddr
177179
}
178180

179181
// StartModule starts the grpc module server.
180-
func (svc *WebService) StartModule(ctx context.Context) error {
182+
func (svc *Service) StartModule(ctx context.Context) error {
181183
svc.mu.Lock()
182184
defer svc.mu.Unlock()
183185
if svc.modServer != nil {
@@ -256,7 +258,7 @@ func (svc *WebService) StartModule(ctx context.Context) error {
256258
return nil
257259
}
258260

259-
func (svc *WebService) refreshResources() error {
261+
func (svc *Service) refreshResources() error {
260262
resources := make(map[resource.Name]resource.Resource)
261263
for _, name := range svc.r.ResourceNames() {
262264
resource, err := svc.r.ResourceByName(name)
@@ -270,7 +272,7 @@ func (svc *WebService) refreshResources() error {
270272

271273
// updateResources gets every existing resource on the robot's resource graph and updates ResourceAPICollection object
272274
// with the correct resources, include deleting ones which have been removed from the resource graph.
273-
func (svc *WebService) updateResources(resources map[resource.Name]resource.Resource) error {
275+
func (svc *Service) updateResources(resources map[resource.Name]resource.Resource) error {
274276
groupedResources := make(map[resource.API]map[resource.Name]resource.Resource)
275277
for n, v := range resources {
276278
r, ok := groupedResources[n.API]
@@ -326,13 +328,13 @@ func (svc *WebService) updateResources(resources map[resource.Name]resource.Reso
326328
}
327329

328330
// Stop stops the main web service prior to actually closing (it leaves the module server running.)
329-
func (svc *WebService) Stop() {
331+
func (svc *Service) Stop() {
330332
svc.mu.Lock()
331333
defer svc.mu.Unlock()
332334
svc.stopWeb()
333335
}
334336

335-
func (svc *WebService) stopWeb() {
337+
func (svc *Service) stopWeb() {
336338
if svc.cancelFunc != nil {
337339
svc.cancelFunc()
338340
}
@@ -341,7 +343,7 @@ func (svc *WebService) stopWeb() {
341343
}
342344

343345
// Close closes a webService via calls to its Cancel func.
344-
func (svc *WebService) Close(ctx context.Context) error {
346+
func (svc *Service) Close(ctx context.Context) error {
345347
svc.mu.Lock()
346348
defer svc.mu.Unlock()
347349
svc.stopWeb()
@@ -355,7 +357,7 @@ func (svc *WebService) Close(ctx context.Context) error {
355357

356358
// runWeb takes the given robot and options and runs the web server. This function will
357359
// block until the context is done.
358-
func (svc *WebService) runWeb(ctx context.Context, options weboptions.Options) (err error) {
360+
func (svc *Service) runWeb(ctx context.Context, options weboptions.Options) (err error) {
359361
if options.Network.BindAddress != "" && options.Network.Listener != nil {
360362
return errors.New("may only set one of network bind address or listener")
361363
}
@@ -499,16 +501,23 @@ func (svc *WebService) runWeb(ctx context.Context, options weboptions.Options) (
499501
return err
500502
}
501503

502-
// Requests for resources are expected to be a gRPC object that includes a `GetName` method.
504+
// Namer is used to get a resource name from incoming requests for countingfor request. Requests for
505+
// resources are expected to be a gRPC object that includes a `GetName` method.
503506
type Namer interface {
504507
GetName() string
505508
}
506509

510+
// RequestCounters maps string keys to atomic ints that get bumped on every incoming gRPC request
511+
// for components.
507512
type RequestCounters struct {
508513
counts sync.Map
509514
}
510515

511-
func (mc *RequestCounters) UnaryInterceptor(ctx context.Context, req any, info *googlegrpc.UnaryServerInfo, handler googlegrpc.UnaryHandler) (resp any, err error) {
516+
// UnaryInterceptor returns an incoming server interceptor that will pull method information and
517+
// optionally resource information to bump the request counters.
518+
func (mc *RequestCounters) UnaryInterceptor(
519+
ctx context.Context, req any, info *googlegrpc.UnaryServerInfo, handler googlegrpc.UnaryHandler,
520+
) (resp any, err error) {
512521
// Handle `info.FullMethod` values such as:
513522
// - `/viam.component.motor.v1.MotorService/IsMoving`
514523
// - `/viam.robot.v1.RobotService/SendSessionHeartbeat`
@@ -538,6 +547,7 @@ func (mc *RequestCounters) UnaryInterceptor(ctx context.Context, req any, info *
538547
return handler(ctx, req)
539548
}
540549

550+
// Stats satisfies the ftdc.Statser interface and will return a copy of the counters.
541551
func (mc *RequestCounters) Stats() any {
542552
ret := make(map[string]int64)
543553
mc.counts.Range(func(key, value any) bool {
@@ -549,7 +559,7 @@ func (mc *RequestCounters) Stats() any {
549559
}
550560

551561
// Initialize RPC Server options.
552-
func (svc *WebService) initRPCOptions(listenerTCPAddr *net.TCPAddr, options weboptions.Options) ([]rpc.ServerOption, error) {
562+
func (svc *Service) initRPCOptions(listenerTCPAddr *net.TCPAddr, options weboptions.Options) ([]rpc.ServerOption, error) {
553563
hosts := options.GetHosts(listenerTCPAddr)
554564

555565
webrtcOptions := rpc.WebRTCServerOptions{
@@ -639,7 +649,7 @@ func (svc *WebService) initRPCOptions(listenerTCPAddr *net.TCPAddr, options webo
639649
}
640650

641651
// Initialize authentication handler options.
642-
func (svc *WebService) initAuthHandlers(listenerTCPAddr *net.TCPAddr, options weboptions.Options) ([]rpc.ServerOption, error) {
652+
func (svc *Service) initAuthHandlers(listenerTCPAddr *net.TCPAddr, options weboptions.Options) ([]rpc.ServerOption, error) {
643653
rpcOpts := []rpc.ServerOption{}
644654

645655
if options.Managed && len(options.Auth.Handlers) == 1 {
@@ -718,7 +728,7 @@ func (svc *WebService) initAuthHandlers(listenerTCPAddr *net.TCPAddr, options we
718728
}
719729

720730
// Register every API resource grpc service here.
721-
func (svc *WebService) initAPIResourceCollections(ctx context.Context, mod bool) error {
731+
func (svc *Service) initAPIResourceCollections(ctx context.Context, mod bool) error {
722732
// TODO (RSDK-144): only register necessary services
723733
apiRegs := resource.RegisteredAPIs()
724734
for s, rs := range apiRegs {
@@ -740,7 +750,7 @@ func (svc *WebService) initAPIResourceCollections(ctx context.Context, mod bool)
740750
}
741751

742752
// Initialize HTTP server.
743-
func (svc *WebService) initHTTPServer(listenerTCPAddr *net.TCPAddr, options weboptions.Options) (*http.Server, error) {
753+
func (svc *Service) initHTTPServer(listenerTCPAddr *net.TCPAddr, options weboptions.Options) (*http.Server, error) {
744754
mux := svc.initMux(options)
745755

746756
httpServer, err := utils.NewPossiblySecureHTTPServer(mux, utils.HTTPServerOptions{
@@ -757,7 +767,7 @@ func (svc *WebService) initHTTPServer(listenerTCPAddr *net.TCPAddr, options webo
757767
}
758768

759769
// Initialize multiplexer between http handlers.
760-
func (svc *WebService) initMux(options weboptions.Options) *goji.Mux {
770+
func (svc *Service) initMux(options weboptions.Options) *goji.Mux {
761771
mux := goji.NewMux()
762772
// Note: used by viam-agent for health checks
763773
mux.HandleFunc(pat.New("/"), func(w http.ResponseWriter, _ *http.Request) {
@@ -814,7 +824,7 @@ func (svc *WebService) initMux(options weboptions.Options) *goji.Mux {
814824
// It is invoked instead of returning the "unimplemented" gRPC error whenever a request is received for
815825
// an unregistered service or method. These method could be registered on a remote viam-server or a module server
816826
// so this handler will attempt to route the request to the correct next node in the chain.
817-
func (svc *WebService) foreignServiceHandler(srv interface{}, stream googlegrpc.ServerStream) error {
827+
func (svc *Service) foreignServiceHandler(srv interface{}, stream googlegrpc.ServerStream) error {
818828
// method will be in the form of PackageName.ServiceName/MethodName
819829
method, ok := googlegrpc.MethodFromServerStream(stream)
820830
if !ok {
@@ -998,7 +1008,7 @@ type stats struct {
9981008
}
9991009

10001010
// Stats returns ftdc data on behalf of the rpcServer and other web services.
1001-
func (svc *WebService) Stats() any {
1011+
func (svc *Service) Stats() any {
10021012
// RSDK-9369: It's not ideal to block in `Stats`. But we don't today expect this to be
10031013
// problematic, and alternatives are more complex/expensive.
10041014
svc.mu.Lock()
@@ -1007,7 +1017,8 @@ func (svc *WebService) Stats() any {
10071017
return stats{RPCServer: svc.rpcServer.Stats()}
10081018
}
10091019

1010-
func (svc *WebService) RequestCounters() *RequestCounters {
1020+
// RequestCounters returns the request counters for this web service.
1021+
func (svc *Service) RequestCounters() *RequestCounters {
10111022
return svc.counter
10121023
}
10131024

@@ -1020,7 +1031,7 @@ type RestartStatusResponse struct {
10201031
}
10211032

10221033
// Handles the `/restart_status` endpoint.
1023-
func (svc *WebService) handleRestartStatus(w http.ResponseWriter, r *http.Request) {
1034+
func (svc *Service) handleRestartStatus(w http.ResponseWriter, r *http.Request) {
10241035
localRobot, isLocal := svc.r.(robot.LocalRobot)
10251036
if !isLocal {
10261037
return

robot/web/web_c.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import (
1818
)
1919

2020
// New returns a new web service for the given robot.
21-
func New(r robot.Robot, logger logging.Logger, opts ...Option) *WebService {
21+
func New(r robot.Robot, logger logging.Logger, opts ...Option) *Service {
2222
var wOpts options
2323
for _, opt := range opts {
2424
opt.apply(&wOpts)
2525
}
26-
webSvc := &WebService{
26+
webSvc := &Service{
2727
Named: InternalServiceName.AsNamed(),
2828
r: r,
2929
logger: logger,
@@ -38,7 +38,7 @@ func New(r robot.Robot, logger logging.Logger, opts ...Option) *WebService {
3838
}
3939

4040
// Reconfigure pulls resources and updates the stream server audio and video streams with the new resources.
41-
func (svc *WebService) Reconfigure(ctx context.Context, deps resource.Dependencies, _ resource.Config) error {
41+
func (svc *Service) Reconfigure(ctx context.Context, deps resource.Dependencies, _ resource.Config) error {
4242
svc.mu.Lock()
4343
defer svc.mu.Unlock()
4444
if err := svc.updateResources(deps); err != nil {
@@ -50,13 +50,13 @@ func (svc *WebService) Reconfigure(ctx context.Context, deps resource.Dependenci
5050
return svc.streamServer.AddNewStreams(svc.cancelCtx)
5151
}
5252

53-
func (svc *WebService) closeStreamServer() {
53+
func (svc *Service) closeStreamServer() {
5454
if err := svc.streamServer.Close(); err != nil {
5555
svc.logger.Errorw("error closing stream server", "error", err)
5656
}
5757
}
5858

59-
func (svc *WebService) initStreamServer(ctx context.Context) error {
59+
func (svc *Service) initStreamServer(ctx context.Context) error {
6060
// Check to make sure stream config option is set in the webservice.
6161
var streamConfig gostream.StreamConfig
6262
if svc.opts.streamConfig != nil {

0 commit comments

Comments
 (0)