2019-09-03 06:05:30 +00:00
|
|
|
// Copyright 2019 Drone IO, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-09-16 07:50:05 +00:00
|
|
|
// +build !oss
|
|
|
|
|
2019-09-03 06:05:30 +00:00
|
|
|
package converter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
|
|
|
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2020-09-15 21:28:45 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-09-03 06:05:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// cache key pattern used in the cache, comprised of the
|
|
|
|
// repository slug and commit sha.
|
2020-09-15 21:28:45 +00:00
|
|
|
const keyf = "%d|%s|%s|%s|%s|%s"
|
2019-09-03 06:05:30 +00:00
|
|
|
|
|
|
|
// Memoize caches the conversion results for subsequent calls.
|
|
|
|
// This micro-optimization is intended for multi-pipeline
|
|
|
|
// projects that would otherwise covert the file for each
|
|
|
|
// pipeline execution.
|
2021-05-14 18:06:47 +00:00
|
|
|
func Memoize(base core.ConvertService, size int) core.ConvertService {
|
2019-09-03 06:05:30 +00:00
|
|
|
// simple cache prevents the same yaml file from being
|
|
|
|
// requested multiple times in a short period.
|
2020-09-15 21:28:45 +00:00
|
|
|
cache, _ := lru.New(10)
|
2021-05-14 18:06:47 +00:00
|
|
|
return &memoize{base: base, cache: cache, size: size}
|
2019-09-03 06:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type memoize struct {
|
|
|
|
base core.ConvertService
|
|
|
|
cache *lru.Cache
|
2021-05-14 18:06:47 +00:00
|
|
|
size int
|
2019-09-03 06:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *memoize) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Config, error) {
|
|
|
|
// this is a minor optimization that prevents caching if the
|
|
|
|
// base converter is a remote converter and is disabled.
|
2020-01-04 01:53:47 +00:00
|
|
|
if remote, ok := c.base.(*remote); ok == true && remote.client == nil {
|
2019-09-03 06:05:30 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:06:47 +00:00
|
|
|
// the client can optionally disable cacheing.
|
|
|
|
if c.size == 0 {
|
|
|
|
return c.base.Convert(ctx, req)
|
|
|
|
}
|
|
|
|
|
2019-09-03 06:05:30 +00:00
|
|
|
// generate the key used to cache the converted file.
|
2020-09-15 21:28:45 +00:00
|
|
|
key := fmt.Sprintf(keyf,
|
|
|
|
req.Repo.ID,
|
|
|
|
req.Build.Event,
|
|
|
|
req.Build.Action,
|
|
|
|
req.Build.Ref,
|
|
|
|
req.Build.After,
|
|
|
|
req.Repo.Config,
|
|
|
|
)
|
2019-09-03 06:05:30 +00:00
|
|
|
|
2020-09-15 21:28:45 +00:00
|
|
|
logger := logrus.WithField("repo", req.Repo.Slug).
|
|
|
|
WithField("build", req.Build.Event).
|
|
|
|
WithField("action", req.Build.Action).
|
|
|
|
WithField("ref", req.Build.Ref).
|
|
|
|
WithField("rev", req.Build.After).
|
|
|
|
WithField("config", req.Repo.Config)
|
|
|
|
|
|
|
|
logger.Trace("extension: conversion: check cache")
|
2019-09-03 06:05:30 +00:00
|
|
|
|
|
|
|
// check the cache for the file and return if exists.
|
|
|
|
cached, ok := c.cache.Get(key)
|
|
|
|
if ok {
|
2020-09-15 21:28:45 +00:00
|
|
|
logger.Trace("extension: conversion: cache hit")
|
2019-09-03 06:05:30 +00:00
|
|
|
return cached.(*core.Config), nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 21:28:45 +00:00
|
|
|
logger.Trace("extension: conversion: cache miss")
|
|
|
|
|
2019-09-03 06:05:30 +00:00
|
|
|
// else convert the configuration file.
|
|
|
|
config, err := c.base.Convert(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if config.Data == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the configuration file was converted
|
2020-09-15 21:28:45 +00:00
|
|
|
// it is temporarily cached. Note that we do
|
|
|
|
// not cache if the commit sha is empty (gogs).
|
|
|
|
if req.Build.After != "" {
|
|
|
|
c.cache.Add(key, config)
|
|
|
|
}
|
|
|
|
|
2019-09-03 06:05:30 +00:00
|
|
|
return config, nil
|
|
|
|
}
|