From 642e123b5c7f0073007caa019424fd07bc2b3540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Tue, 10 Jun 2025 16:43:00 +0200 Subject: [PATCH 1/2] refactor: clarify setup for the RPC server --- ocis/pkg/runtime/service/service.go | 34 +++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/ocis/pkg/runtime/service/service.go b/ocis/pkg/runtime/service/service.go index 9bd2adb0d67..86f3bc7c98b 100644 --- a/ocis/pkg/runtime/service/service.go +++ b/ocis/pkg/runtime/service/service.go @@ -402,18 +402,12 @@ func Start(ctx context.Context, o ...Option) error { } } - if err = rpc.Register(s); err != nil { - if s != nil { - s.Log.Fatal().Err(err).Msg("could not register rpc service") - } - } - rpc.HandleHTTP() - - l, err := net.Listen("tcp", net.JoinHostPort(s.cfg.Runtime.Host, s.cfg.Runtime.Port)) + // prepare RPC server + srv, err := newRPCServer(s) if err != nil { - s.Log.Fatal().Err(err).Msg("could not start listener") + s.Log.Fatal().Err(err).Msg("could not create RPC server") + // TODO: Should abort? previous code kept going } - srv := new(http.Server) // prepare the set of services to run // runset keeps track of which services to start supervised. @@ -439,7 +433,7 @@ func Start(ctx context.Context, o ...Option) error { scheduleServiceTokens(s, runset, s.Additional) go func() { - if err = srv.Serve(l); err != nil && !errors.Is(err, http.ErrServerClosed) { + if err = srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { s.Log.Fatal().Err(err).Msg("could not start rpc server") } }() @@ -599,3 +593,21 @@ func wait(d time.Duration) func(cfg *ociscfg.Config) error { return nil } } + +// newRPCServer creates an HTTP server to expose the "s" service's methods using RPC. +// The host and port for the server are taken from the service configuration (s.cfg.Runtime) +func newRPCServer(s *Service) (*http.Server, error) { + rpcSrv := rpc.NewServer() + if err := rpcSrv.Register(s); err != nil { + return nil, err + } + rpcSrv.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath) + // srv.HandleHTTP will register the handlers in the http.DefaultServeMux + + srv := &http.Server{ + Addr: net.JoinHostPort(s.cfg.Runtime.Host, s.cfg.Runtime.Port), // this is always tcp + // Handler must be nil to use the http.DefaultServeMux + ReadHeaderTimeout: 5 * time.Second, + } + return srv, nil +} From 0e33001418b2c5d415d8e66d99288180e86dedd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Wed, 11 Jun 2025 11:04:48 +0200 Subject: [PATCH 2/2] fix: return error if RPC server can't be created --- ocis/pkg/runtime/service/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocis/pkg/runtime/service/service.go b/ocis/pkg/runtime/service/service.go index 86f3bc7c98b..7c6b3968689 100644 --- a/ocis/pkg/runtime/service/service.go +++ b/ocis/pkg/runtime/service/service.go @@ -406,7 +406,7 @@ func Start(ctx context.Context, o ...Option) error { srv, err := newRPCServer(s) if err != nil { s.Log.Fatal().Err(err).Msg("could not create RPC server") - // TODO: Should abort? previous code kept going + return err } // prepare the set of services to run