support optional convert plugin cache size
This commit is contained in:
parent
6b8abc32bf
commit
04ec418c44
5 changed files with 16 additions and 8 deletions
|
@ -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"`
|
||||
}
|
||||
|
||||
|
|
|
@ -94,6 +94,7 @@ func provideConvertPlugin(client *scm.Client, conf spec.Config) core.ConvertServ
|
|||
conf.Convert.SkipVerify,
|
||||
conf.Convert.Timeout,
|
||||
),
|
||||
conf.Convert.CacheSize,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue