From 664b67d25c49eefee06a04410dc3029dce60b8a0 Mon Sep 17 00:00:00 2001 From: Yadong Ding Date: Wed, 6 Sep 2023 11:25:10 +0800 Subject: [PATCH] chore: separate gc and convert - GC doesn't return any error, change to the error log. - Change gc call to ```goroutine```. Signed-off-by: Yadong Ding --- pkg/adapter/adapter.go | 10 +++------- pkg/content/content.go | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 16 deletions(-) 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