From 1d399d97f988da6f494f0778407cf24eca776cca Mon Sep 17 00:00:00 2001 From: zyxkad Date: Mon, 12 Aug 2024 19:27:10 -0600 Subject: [PATCH] add gc --- cluster/storage.go | 25 +++++++++++++++++++++++++ main.go | 6 +++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cluster/storage.go b/cluster/storage.go index 1d48521..05d4510 100644 --- a/cluster/storage.go +++ b/cluster/storage.go @@ -702,6 +702,31 @@ func (c *HTTPClient) fetchFileWithBuf( return } +func (c *HTTPClient) Gc( + ctx context.Context, + manager *storage.Manager, + files map[string]*StorageFileInfo, +) error { + errs := make([]error, len(manager.Storages)) + var wg sync.WaitGroup + for i, s := range manager.Storages { + wg.Add(1) + go func(i int, s storage.Storage) { + defer wg.Done() + errs[i] = s.WalkDir(func(hash string, size int64) error { + info, ok := files[hash] + ok = ok && slices.Contains(info.Storages, s) + if !ok { + s.Remove(hash) + } + return nil + }) + }(i, s) + } + wg.Wait() + return errors.Join(errs...) +} + func getHashMethod(l int) (hashMethod crypto.Hash, err error) { switch l { case 32: diff --git a/main.go b/main.go index 3dd36c8..0d46a91 100644 --- a/main.go +++ b/main.go @@ -543,9 +543,9 @@ func (r *Runner) InitSynchronizer(ctx context.Context) { } go r.UpdateFileRecords(fileMap, nil) - // if !r.Config.Advanced.NoGC { - // go r.cluster.Gc() - // } + if !r.Config.Advanced.NoGC { + go r.client.Gc(context.TODO(), r.storageManager, fileMap) + } } // else if fl != nil { // if err := r.cluster.SetFilesetByExists(ctx, fl); err != nil {