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.
This commit is contained in:
Ralf Haferkamp
2023-05-09 14:30:35 +02:00
committed by Ralf Haferkamp
parent 3e92e409f7
commit 18bb3dbaca
6 changed files with 46 additions and 22 deletions

View File

@@ -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()

View File

@@ -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
}
}

View File

@@ -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")
}

View File

@@ -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
}
}

View File

@@ -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)

View File

@@ -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)
}