Skip to content

Commit

Permalink
settings: Instanciate only a single instance of the ServiceHandler/Store
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rhafer committed May 12, 2023
1 parent 3e92e40 commit 18bb3db
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
13 changes: 12 additions & 1 deletion services/settings/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -51,13 +52,16 @@ 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),
http.Logger(logger),
http.Context(ctx),
http.Config(cfg),
http.Metrics(mtrcs),
http.ServiceHandler(handle),
)
if err != nil {
logger.Error().
Expand All @@ -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()
Expand Down
21 changes: 15 additions & 6 deletions services/settings/pkg/server/grpc/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.
Expand Down Expand Up @@ -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
}
}
3 changes: 1 addition & 2 deletions services/settings/pkg/server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
}
Expand Down
21 changes: 15 additions & 6 deletions services/settings/pkg/server/http/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.
Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion services/settings/pkg/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions services/settings/pkg/store/metadata/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 18bb3db

Please sign in to comment.