From 18bb3dbaca11e66e20aa366ef8f2fd46776a5b64 Mon Sep 17 00:00:00 2001 From: Ralf Haferkamp Date: Tue, 9 May 2023 14:30:35 +0200 Subject: [PATCH] settings: Instanciate only a single instance of the ServiceHandler/Store Share the same instance between the HTTP and the GRPC service. This is in preparation for moving the cache of the metadata storage backend to a go-micro/store based implementation. By sharing the same service instance in the HTTP and GRPC services we can avoid the usage of global variables for the caches, which will make the move to the go-micro/store implementation simpler. --- services/settings/pkg/command/server.go | 13 +++++++++++- services/settings/pkg/server/grpc/option.go | 21 +++++++++++++------ services/settings/pkg/server/grpc/server.go | 3 +-- services/settings/pkg/server/http/option.go | 21 +++++++++++++------ services/settings/pkg/server/http/server.go | 2 +- services/settings/pkg/store/metadata/cache.go | 8 ++----- 6 files changed, 46 insertions(+), 22 deletions(-) diff --git a/services/settings/pkg/command/server.go b/services/settings/pkg/command/server.go index 98f55f3f6b..cd48ba639e 100644 --- a/services/settings/pkg/command/server.go +++ b/services/settings/pkg/command/server.go @@ -15,6 +15,7 @@ import ( "github.com/owncloud/ocis/v2/services/settings/pkg/server/debug" "github.com/owncloud/ocis/v2/services/settings/pkg/server/grpc" "github.com/owncloud/ocis/v2/services/settings/pkg/server/http" + svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0" "github.com/owncloud/ocis/v2/services/settings/pkg/tracing" "github.com/urfave/cli/v2" ) @@ -51,6 +52,8 @@ func Server(cfg *config.Config) *cli.Command { mtrcs := metrics.New() mtrcs.BuildInfo.WithLabelValues(version.GetString()).Set(1) + handle := svc.NewService(cfg, logger) + // prepare an HTTP server and add it to the group run. httpServer, err := http.Server( http.Name(cfg.Service.Name), @@ -58,6 +61,7 @@ func Server(cfg *config.Config) *cli.Command { http.Context(ctx), http.Config(cfg), http.Metrics(mtrcs), + http.ServiceHandler(handle), ) if err != nil { logger.Error(). @@ -71,7 +75,14 @@ func Server(cfg *config.Config) *cli.Command { }) // prepare a gRPC server and add it to the group run. - grpcServer := grpc.Server(grpc.Name(cfg.Service.Name), grpc.Logger(logger), grpc.Context(ctx), grpc.Config(cfg), grpc.Metrics(mtrcs)) + grpcServer := grpc.Server( + grpc.Name(cfg.Service.Name), + grpc.Logger(logger), + grpc.Context(ctx), + grpc.Config(cfg), + grpc.Metrics(mtrcs), + grpc.ServiceHandler(handle), + ) servers.Add(grpcServer.Run, func(_ error) { logger.Info().Str("server", "grpc").Msg("Shutting down server") cancel() diff --git a/services/settings/pkg/server/grpc/option.go b/services/settings/pkg/server/grpc/option.go index b87b02813d..afcc6a39b8 100644 --- a/services/settings/pkg/server/grpc/option.go +++ b/services/settings/pkg/server/grpc/option.go @@ -6,6 +6,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/settings/pkg/config" "github.com/owncloud/ocis/v2/services/settings/pkg/metrics" + svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0" "github.com/urfave/cli/v2" ) @@ -14,12 +15,13 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Name string - Logger log.Logger - Context context.Context - Config *config.Config - Metrics *metrics.Metrics - Flags []cli.Flag + Name string + Logger log.Logger + Context context.Context + Config *config.Config + Metrics *metrics.Metrics + ServiceHandler svc.Service + Flags []cli.Flag } // newOptions initializes the available default options. @@ -74,3 +76,10 @@ func Flags(val []cli.Flag) Option { o.Flags = append(o.Flags, val...) } } + +// ServiceHandler provides a function to set the ServiceHandler option +func ServiceHandler(val svc.Service) Option { + return func(o *Options) { + o.ServiceHandler = val + } +} diff --git a/services/settings/pkg/server/grpc/server.go b/services/settings/pkg/server/grpc/server.go index 7e448ee62d..4cad0fdd15 100644 --- a/services/settings/pkg/server/grpc/server.go +++ b/services/settings/pkg/server/grpc/server.go @@ -7,7 +7,6 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/version" settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0" - svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0" "go-micro.dev/v4/api" "go-micro.dev/v4/server" ) @@ -34,7 +33,7 @@ func Server(opts ...Option) grpc.Service { options.Logger.Fatal().Err(err).Msg("Error creating settings service") } - handle := svc.NewService(options.Config, options.Logger) + handle := options.ServiceHandler if err := settingssvc.RegisterBundleServiceHandler(service.Server(), handle); err != nil { options.Logger.Fatal().Err(err).Msg("could not register Bundle service handler") } diff --git a/services/settings/pkg/server/http/option.go b/services/settings/pkg/server/http/option.go index e4537ffb12..f3e3fdcb71 100644 --- a/services/settings/pkg/server/http/option.go +++ b/services/settings/pkg/server/http/option.go @@ -6,6 +6,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/settings/pkg/config" "github.com/owncloud/ocis/v2/services/settings/pkg/metrics" + svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0" "github.com/urfave/cli/v2" ) @@ -14,12 +15,13 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Name string - Logger log.Logger - Context context.Context - Config *config.Config - Metrics *metrics.Metrics - Flags []cli.Flag + Name string + Logger log.Logger + Context context.Context + Config *config.Config + Metrics *metrics.Metrics + ServiceHandler svc.Service + Flags []cli.Flag } // newOptions initializes the available default options. @@ -74,3 +76,10 @@ func Flags(val []cli.Flag) Option { o.Flags = append(o.Flags, val...) } } + +// ServiceHandler provides a function to set the ServiceHandler option +func ServiceHandler(val svc.Service) Option { + return func(o *Options) { + o.ServiceHandler = val + } +} diff --git a/services/settings/pkg/server/http/server.go b/services/settings/pkg/server/http/server.go index c6d76dd646..3fcb4a4691 100644 --- a/services/settings/pkg/server/http/server.go +++ b/services/settings/pkg/server/http/server.go @@ -37,7 +37,7 @@ func Server(opts ...Option) (ohttp.Service, error) { return ohttp.Service{}, fmt.Errorf("could not initialize http service: %w", err) } - handle := svc.NewService(options.Config, options.Logger) + handle := options.ServiceHandler { handle = svc.NewInstrument(handle, options.Metrics) diff --git a/services/settings/pkg/store/metadata/cache.go b/services/settings/pkg/store/metadata/cache.go index 5b5242a16b..50072606fa 100644 --- a/services/settings/pkg/store/metadata/cache.go +++ b/services/settings/pkg/store/metadata/cache.go @@ -11,10 +11,6 @@ import ( var ( cachettl = 0 - // these need to be global instances for now as the `Service` (and therefore the `Store`) are instantiated twice (for grpc and http) - // therefore caches need to cover both instances - dircache = initCache(cachettl) - filescache = initCache(cachettl) ) // CachedMDC is cache for the metadataclient @@ -99,8 +95,8 @@ func (c *CachedMDC) MakeDirIfNotExist(ctx context.Context, id string) error { // Init instantiates the caches func (c *CachedMDC) Init(ctx context.Context, id string) error { - c.dirs = dircache - c.files = filescache + c.dirs = initCache(cachettl) + c.files = initCache(cachettl) return c.next.Init(ctx, id) }