diff --git a/pkg/adapter/adapter.go b/pkg/adapter/adapter.go index 0e32e74f..ead9386b 100644 --- a/pkg/adapter/adapter.go +++ b/pkg/adapter/adapter.go @@ -100,9 +100,7 @@ func NewLocalAdapter(cfg *config.Config) (*LocalAdapter, error) { func startScheduledGC(content *content.Content) { ticker := time.NewTicker(time.Hour) for range ticker.C { - if err := content.GC(namespaces.WithNamespace(context.Background(), "acceleration-service"), content.Threshold/2); err != nil { - logrus.Error(errors.Wrap(err, "scheduled gc task")) - } + content.GC(namespaces.WithNamespace(context.Background(), "acceleration-service"), content.Threshold/2) } } @@ -126,14 +124,12 @@ func (adp *LocalAdapter) Convert(ctx context.Context, source string) error { return err } adp.content.GcMutex.RLock() + defer adp.content.GcMutex.RUnlock() if _, err = adp.cvt.Convert(ctx, source, target, cacheRef); err != nil { adp.content.GcMutex.RUnlock() return err } - adp.content.GcMutex.RUnlock() - if err := adp.content.GC(ctx, adp.content.Threshold); err != nil { - return err - } + go adp.content.GC(ctx, adp.content.Threshold) return nil } diff --git a/pkg/content/content.go b/pkg/content/content.go index 2a6e7e6b..e61cdbcb 100644 --- a/pkg/content/content.go +++ b/pkg/content/content.go @@ -135,27 +135,29 @@ func (content *Content) Size() (int64, error) { } // GC clean the local caches by cfg.Provider.GCPolicy configuration -func (content *Content) GC(ctx context.Context, threshold int64) error { +func (content *Content) GC(ctx context.Context, threshold int64) { size, err := content.Size() if err != nil { - return err + logrus.Error(errors.Wrap(err, "gc get content size")) + return } // if the local content size over eighty percent of threshold, gc start if size > (threshold*gcPercent)/100 { - if _, err, _ := content.gcSingleflight.Do(accelerationServiceNamespace, func() (interface{}, error) { + content.gcSingleflight.Do(accelerationServiceNamespace, func() (interface{}, error) { content.GcMutex.Lock() defer content.GcMutex.Unlock() // recalculate the local cache size size, err := content.Size() if err != nil { - return nil, err + logrus.Error(errors.Wrap(err, "gc get content size")) + return nil, nil } - return nil, content.garbageCollect(ctx, size-(threshold*gcPercent)/100) - }); err != nil { - return err - } + if err := content.garbageCollect(ctx, size-(threshold*gcPercent)/100); err != nil { + logrus.Error(errors.Wrap(err, "gc")) + } + return nil, nil + }) } - return nil } // garbageCollect clean the local caches by lease