Merge pull request #3082 from owncloud/thumbnails_refactor_interface

Use decorator pattern and allow more methods to be decorated
This commit is contained in:
Willy Kloucek authored and GitHub committed 2022-02-04 11:33:50 +01:00
commit d66d649fe2
6 files changed
+80 -19

No files matched your search

+5 -4
View File
@@ -6,6 +6,7 @@ import (
"github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/ocis-pkg/version"
thumbnailssvc "github.com/owncloud/ocis/protogen/gen/ocis/services/thumbnails/v0" thumbnailssvc "github.com/owncloud/ocis/protogen/gen/ocis/services/thumbnails/v0"
svc "github.com/owncloud/ocis/thumbnails/pkg/service/v0" svc "github.com/owncloud/ocis/thumbnails/pkg/service/v0"
"github.com/owncloud/ocis/thumbnails/pkg/service/v0/decorators"
"github.com/owncloud/ocis/thumbnails/pkg/thumbnail/imgsource" "github.com/owncloud/ocis/thumbnails/pkg/thumbnail/imgsource"
"github.com/owncloud/ocis/thumbnails/pkg/thumbnail/storage" "github.com/owncloud/ocis/thumbnails/pkg/thumbnail/storage"
) )
@@ -30,7 +31,7 @@ func NewService(opts ...Option) grpc.Service {
options.Logger.Error().Err(err).Msg("could not get gateway client") options.Logger.Error().Err(err).Msg("could not get gateway client")
return grpc.Service{} return grpc.Service{}
} }
var thumbnail thumbnailssvc.ThumbnailServiceHandler var thumbnail decorators.DecoratedService
{ {
thumbnail = svc.NewService( thumbnail = svc.NewService(
svc.Config(options.Config), svc.Config(options.Config),
@@ -45,9 +46,9 @@ func NewService(opts ...Option) grpc.Service {
svc.CS3Source(imgsource.NewCS3Source(tconf, gc)), svc.CS3Source(imgsource.NewCS3Source(tconf, gc)),
svc.CS3Client(gc), svc.CS3Client(gc),
) )
thumbnail = svc.NewInstrument(thumbnail, options.Metrics) thumbnail = decorators.NewInstrument(thumbnail, options.Metrics)
thumbnail = svc.NewLogging(thumbnail, options.Logger) thumbnail = decorators.NewLogging(thumbnail, options.Logger)
thumbnail = svc.NewTracing(thumbnail) thumbnail = decorators.NewTracing(thumbnail)
} }
_ = thumbnailssvc.RegisterThumbnailServiceHandler( _ = thumbnailssvc.RegisterThumbnailServiceHandler(
@@ -0,0 +1,59 @@
package decorators
import (
"context"
thumbnailssvc "github.com/owncloud/ocis/protogen/gen/ocis/services/thumbnails/v0"
)
// Interface acting as facade, holding all the interfaces that this
// thumbnails microservice is expecting to implement.
// For now, only the thumbnailssvc.ThumbnailServiceHandler is present,
// but a future configsvc.ConfigServiceHandler is expected to be added here
//
// This interface will also act as the base interface to implement
// a decorator pattern.
type DecoratedService interface {
thumbnailssvc.ThumbnailServiceHandler
}
// Base type to implement the decorators. It will provide a basic implementation
// by delegating to the decoratedService
//
// Expected implementations will be like:
// ```
// type MyDecorator struct {
// Decorator
// myCustomOpts *opts
// additionalSrv *srv
// }
//
// func NewMyDecorator(next DecoratedService, customOpts *customOpts) DecoratedService {
// .....
// return MyDecorator{
// Decorator: Decorator{next: next},
// myCustomOpts: opts,
// additionalSrv: srv,
// }
// }
// ```
type Decorator struct {
next DecoratedService
}
// Base implementation for the GetThumbnail (for the thumbnailssvc).
// It will just delegate to the underlying decoratedService
//
// Your custom decorator is expected to overwrite this function,
// but it MUST call the underlying decoratedService at some point
// ```
// func (d MyDecorator) GetThumbnail(ctx context.Context, req *thumbnailssvc.GetThumbnailRequest, resp *thumbnailssvc.GetThumbnailResponse) error {
// doSomething()
// err := d.next.GetThumbnail(ctx, req, resp)
// doAnotherThing()
// return err
// }
// ```
func (deco Decorator) GetThumbnail(ctx context.Context, req *thumbnailssvc.GetThumbnailRequest, resp *thumbnailssvc.GetThumbnailResponse) error {
return deco.next.GetThumbnail(ctx, req, resp)
}
@@ -1,4 +1,4 @@
package svc package decorators
import ( import (
"context" "context"
@@ -9,15 +9,15 @@ import (
) )
// NewInstrument returns a service that instruments metrics. // NewInstrument returns a service that instruments metrics.
func NewInstrument(next thumbnailssvc.ThumbnailServiceHandler, metrics *metrics.Metrics) thumbnailssvc.ThumbnailServiceHandler { func NewInstrument(next DecoratedService, metrics *metrics.Metrics) DecoratedService {
return instrument{ return instrument{
next: next, Decorator: Decorator{next: next},
metrics: metrics, metrics: metrics,
} }
} }
type instrument struct { type instrument struct {
next thumbnailssvc.ThumbnailServiceHandler Decorator
metrics *metrics.Metrics metrics *metrics.Metrics
} }
@@ -1,4 +1,4 @@
package svc package decorators
import ( import (
"context" "context"
@@ -9,15 +9,15 @@ import (
) )
// NewLogging returns a service that logs messages. // NewLogging returns a service that logs messages.
func NewLogging(next thumbnailssvc.ThumbnailServiceHandler, logger log.Logger) thumbnailssvc.ThumbnailServiceHandler { func NewLogging(next DecoratedService, logger log.Logger) DecoratedService {
return logging{ return logging{
next: next, Decorator: Decorator{next: next},
logger: logger, logger: logger,
} }
} }
type logging struct { type logging struct {
next thumbnailssvc.ThumbnailServiceHandler Decorator
logger log.Logger logger log.Logger
} }
@@ -1,4 +1,4 @@
package svc package decorators
import ( import (
"context" "context"
@@ -11,14 +11,14 @@ import (
) )
// NewTracing returns a service that instruments traces. // NewTracing returns a service that instruments traces.
func NewTracing(next thumbnailssvc.ThumbnailServiceHandler) thumbnailssvc.ThumbnailServiceHandler { func NewTracing(next DecoratedService) DecoratedService {
return tracing{ return tracing{
next: next, Decorator: Decorator{next: next},
} }
} }
type tracing struct { type tracing struct {
next thumbnailssvc.ThumbnailServiceHandler Decorator
} }
// GetThumbnail implements the ThumbnailServiceHandler interface. // GetThumbnail implements the ThumbnailServiceHandler interface.
+2 -1
View File
@@ -14,6 +14,7 @@ import (
"github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/log"
thumbnailssvc "github.com/owncloud/ocis/protogen/gen/ocis/services/thumbnails/v0" thumbnailssvc "github.com/owncloud/ocis/protogen/gen/ocis/services/thumbnails/v0"
"github.com/owncloud/ocis/thumbnails/pkg/preprocessor" "github.com/owncloud/ocis/thumbnails/pkg/preprocessor"
"github.com/owncloud/ocis/thumbnails/pkg/service/v0/decorators"
"github.com/owncloud/ocis/thumbnails/pkg/thumbnail" "github.com/owncloud/ocis/thumbnails/pkg/thumbnail"
"github.com/owncloud/ocis/thumbnails/pkg/thumbnail/imgsource" "github.com/owncloud/ocis/thumbnails/pkg/thumbnail/imgsource"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -22,7 +23,7 @@ import (
) )
// NewService returns a service implementation for Service. // NewService returns a service implementation for Service.
func NewService(opts ...Option) thumbnailssvc.ThumbnailServiceHandler { func NewService(opts ...Option) decorators.DecoratedService {
options := newOptions(opts...) options := newOptions(opts...)
logger := options.Logger logger := options.Logger
resolutions, err := thumbnail.ParseResolutions(options.Config.Thumbnail.Resolutions) resolutions, err := thumbnail.ParseResolutions(options.Config.Thumbnail.Resolutions)