From 04ec418c448dea69afe9c76aac58d0029e264baa Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Fri, 14 May 2021 14:06:47 -0400 Subject: [PATCH] support optional convert plugin cache size --- cmd/drone-server/config/config.go | 1 + cmd/drone-server/inject_plugin.go | 1 + plugin/converter/memoize.go | 10 ++++++++-- plugin/converter/memoize_oss.go | 2 +- plugin/converter/memoize_test.go | 10 +++++----- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/drone-server/config/config.go b/cmd/drone-server/config/config.go index 9fd870b9..2a4badaa 100644 --- a/cmd/drone-server/config/config.go +++ b/cmd/drone-server/config/config.go @@ -306,6 +306,7 @@ type ( Endpoint string `envconfig:"DRONE_CONVERT_PLUGIN_ENDPOINT"` Secret string `envconfig:"DRONE_CONVERT_PLUGIN_SECRET"` SkipVerify bool `envconfig:"DRONE_CONVERT_PLUGIN_SKIP_VERIFY"` + CacheSize int `envconfig:"DRONE_CONVERT_PLUGIN_CACHE_SIZE" default:"10"` Timeout time.Duration `envconfig:"DRONE_CONVERT_TIMEOUT" default:"1m"` } diff --git a/cmd/drone-server/inject_plugin.go b/cmd/drone-server/inject_plugin.go index ef2e7463..ed22380b 100644 --- a/cmd/drone-server/inject_plugin.go +++ b/cmd/drone-server/inject_plugin.go @@ -94,6 +94,7 @@ func provideConvertPlugin(client *scm.Client, conf spec.Config) core.ConvertServ conf.Convert.SkipVerify, conf.Convert.Timeout, ), + conf.Convert.CacheSize, ), ) } diff --git a/plugin/converter/memoize.go b/plugin/converter/memoize.go index 4a6a32cd..ea78b168 100644 --- a/plugin/converter/memoize.go +++ b/plugin/converter/memoize.go @@ -34,16 +34,17 @@ const keyf = "%d|%s|%s|%s|%s|%s" // This micro-optimization is intended for multi-pipeline // projects that would otherwise covert the file for each // pipeline execution. -func Memoize(base core.ConvertService) core.ConvertService { +func Memoize(base core.ConvertService, size int) core.ConvertService { // simple cache prevents the same yaml file from being // requested multiple times in a short period. cache, _ := lru.New(10) - return &memoize{base: base, cache: cache} + return &memoize{base: base, cache: cache, size: size} } type memoize struct { base core.ConvertService cache *lru.Cache + size int } func (c *memoize) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Config, error) { @@ -53,6 +54,11 @@ func (c *memoize) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Con return nil, nil } + // the client can optionally disable cacheing. + if c.size == 0 { + return c.base.Convert(ctx, req) + } + // generate the key used to cache the converted file. key := fmt.Sprintf(keyf, req.Repo.ID, diff --git a/plugin/converter/memoize_oss.go b/plugin/converter/memoize_oss.go index 6ac86c27..08d4aae5 100644 --- a/plugin/converter/memoize_oss.go +++ b/plugin/converter/memoize_oss.go @@ -24,6 +24,6 @@ import ( // This micro-optimization is intended for multi-pipeline // projects that would otherwise covert the file for each // pipeline execution. -func Memoize(base core.ConvertService) core.ConvertService { +func Memoize(base core.ConvertService, size int) core.ConvertService { return new(noop) } diff --git a/plugin/converter/memoize_test.go b/plugin/converter/memoize_test.go index d61a3008..f700734d 100644 --- a/plugin/converter/memoize_test.go +++ b/plugin/converter/memoize_test.go @@ -30,7 +30,7 @@ func TestMemoize(t *testing.T) { base := mock.NewMockConvertService(controller) base.EXPECT().Convert(gomock.Any(), gomock.Any()).Return(args.Config, nil) - service := Memoize(base).(*memoize) + service := Memoize(base, 10).(*memoize) _, err := service.Convert(noContext, args) if err != nil { t.Error(err) @@ -69,7 +69,7 @@ func TestMemoize_Tag(t *testing.T) { base := mock.NewMockConvertService(controller) base.EXPECT().Convert(gomock.Any(), gomock.Any()).Return(args.Config, nil) - service := Memoize(base).(*memoize) + service := Memoize(base, 10).(*memoize) res, err := service.Convert(noContext, args) if err != nil { t.Error(err) @@ -93,7 +93,7 @@ func TestMemoize_Empty(t *testing.T) { base := mock.NewMockConvertService(controller) base.EXPECT().Convert(gomock.Any(), gomock.Any()).Return(args.Config, nil) - service := Memoize(base).(*memoize) + service := Memoize(base, 10).(*memoize) res, err := service.Convert(noContext, args) if err != nil { t.Error(err) @@ -120,7 +120,7 @@ func TestMemoize_Nil(t *testing.T) { base := mock.NewMockConvertService(controller) base.EXPECT().Convert(gomock.Any(), gomock.Any()).Return(args.Config, nil) - service := Memoize(base).(*memoize) + service := Memoize(base, 10).(*memoize) res, err := service.Convert(noContext, args) if err != nil { t.Error(err) @@ -147,7 +147,7 @@ func TestMemoize_Error(t *testing.T) { base := mock.NewMockConvertService(controller) base.EXPECT().Convert(gomock.Any(), gomock.Any()).Return(nil, want) - service := Memoize(base).(*memoize) + service := Memoize(base, 10).(*memoize) _, err := service.Convert(noContext, args) if err == nil { t.Errorf("Expect error from base returned to caller")