From 9860ae582bc0367853aef4e331cf11a8616876be Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 3 Apr 2026 16:20:40 -0700 Subject: [PATCH] refactor: remove withoutCancelIfAlive helper, inline ctx.Err() checks directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The helper didn't reduce line count — just inline the straightforward ctx.Err() check at each call site. --- weed/filer/filerstore_wrapper.go | 55 +++++++++++++------------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/weed/filer/filerstore_wrapper.go b/weed/filer/filerstore_wrapper.go index cca8d8ce1..a8f6a2795 100644 --- a/weed/filer/filerstore_wrapper.go +++ b/weed/filer/filerstore_wrapper.go @@ -127,24 +127,13 @@ func (fsw *FilerStoreWrapper) Initialize(configuration util.Configuration, prefi return fsw.getDefaultStore().Initialize(configuration, prefix) } -// withoutCancelIfAlive detaches ctx from its parent's cancellation signal so -// that filer-store operations can finish even after the originating HTTP request -// is done. It returns an error if the context is *already* cancelled or expired -// at the time of the call — this prevents writing orphaned metadata for -// requests that have already been abandoned (e.g. S3 CopyObject, -// CompleteMultipartUpload). -func withoutCancelIfAlive(ctx context.Context) (context.Context, error) { - if err := ctx.Err(); err != nil { - return ctx, err - } - return context.WithoutCancel(ctx), nil -} - func (fsw *FilerStoreWrapper) InsertEntry(ctx context.Context, entry *Entry) error { - ctx, err := withoutCancelIfAlive(ctx) - if err != nil { + // Fail fast if the context is already cancelled to prevent orphaned metadata + // when the originating request has been abandoned. + if err := ctx.Err(); err != nil { return err } + ctx = context.WithoutCancel(ctx) actualStore := fsw.getActualStore(entry.FullPath) stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "insert").Inc() start := time.Now() @@ -171,10 +160,10 @@ func (fsw *FilerStoreWrapper) InsertEntry(ctx context.Context, entry *Entry) err // InsertEntryKnownAbsent skips the pre-insert FindEntry path when the caller has // already established that the target path does not exist. func (fsw *FilerStoreWrapper) InsertEntryKnownAbsent(ctx context.Context, entry *Entry) error { - ctx, err := withoutCancelIfAlive(ctx) - if err != nil { + if err := ctx.Err(); err != nil { return err } + ctx = context.WithoutCancel(ctx) actualStore := fsw.getActualStore(entry.FullPath) stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "insert").Inc() start := time.Now() @@ -197,10 +186,10 @@ func (fsw *FilerStoreWrapper) InsertEntryKnownAbsent(ctx context.Context, entry } func (fsw *FilerStoreWrapper) UpdateEntry(ctx context.Context, entry *Entry) error { - ctx, err := withoutCancelIfAlive(ctx) - if err != nil { + if err := ctx.Err(); err != nil { return err } + ctx = context.WithoutCancel(ctx) actualStore := fsw.getActualStore(entry.FullPath) stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "update").Inc() start := time.Now() @@ -258,10 +247,10 @@ func (fsw *FilerStoreWrapper) FindEntry(ctx context.Context, fp util.FullPath) ( } func (fsw *FilerStoreWrapper) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) { - ctx, err = withoutCancelIfAlive(ctx) - if err != nil { + if err = ctx.Err(); err != nil { return err } + ctx = context.WithoutCancel(ctx) actualStore := fsw.getActualStore(fp) stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "delete").Inc() start := time.Now() @@ -289,10 +278,10 @@ func (fsw *FilerStoreWrapper) DeleteEntry(ctx context.Context, fp util.FullPath) } func (fsw *FilerStoreWrapper) DeleteOneEntry(ctx context.Context, existingEntry *Entry) (err error) { - ctx, err = withoutCancelIfAlive(ctx) - if err != nil { + if err = ctx.Err(); err != nil { return err } + ctx = context.WithoutCancel(ctx) actualStore := fsw.getActualStore(existingEntry.FullPath) stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "delete").Inc() start := time.Now() @@ -316,10 +305,10 @@ func (fsw *FilerStoreWrapper) DeleteOneEntry(ctx context.Context, existingEntry } func (fsw *FilerStoreWrapper) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) { - ctx, err = withoutCancelIfAlive(ctx) - if err != nil { + if err = ctx.Err(); err != nil { return err } + ctx = context.WithoutCancel(ctx) actualStore := fsw.getActualStore(fp + "/") stats.FilerStoreCounter.WithLabelValues(actualStore.GetName(), "deleteFolderChildren").Inc() start := time.Now() @@ -425,18 +414,18 @@ func (fsw *FilerStoreWrapper) prefixFilterEntries(ctx context.Context, dirPath u } func (fsw *FilerStoreWrapper) BeginTransaction(ctx context.Context) (context.Context, error) { - ctx, err := withoutCancelIfAlive(ctx) - if err != nil { + if err := ctx.Err(); err != nil { return nil, err } + ctx = context.WithoutCancel(ctx) return fsw.getDefaultStore().BeginTransaction(ctx) } func (fsw *FilerStoreWrapper) CommitTransaction(ctx context.Context) error { - ctx, err := withoutCancelIfAlive(ctx) - if err != nil { + if err := ctx.Err(); err != nil { return err } + ctx = context.WithoutCancel(ctx) return fsw.getDefaultStore().CommitTransaction(ctx) } @@ -450,10 +439,10 @@ func (fsw *FilerStoreWrapper) Shutdown() { } func (fsw *FilerStoreWrapper) KvPut(ctx context.Context, key []byte, value []byte) (err error) { - ctx, err = withoutCancelIfAlive(ctx) - if err != nil { + if err = ctx.Err(); err != nil { return err } + ctx = context.WithoutCancel(ctx) return fsw.getDefaultStore().KvPut(ctx, key, value) } func (fsw *FilerStoreWrapper) KvGet(ctx context.Context, key []byte) (value []byte, err error) { @@ -461,10 +450,10 @@ func (fsw *FilerStoreWrapper) KvGet(ctx context.Context, key []byte) (value []by return fsw.getDefaultStore().KvGet(ctx, key) } func (fsw *FilerStoreWrapper) KvDelete(ctx context.Context, key []byte) (err error) { - ctx, err = withoutCancelIfAlive(ctx) - if err != nil { + if err = ctx.Err(); err != nil { return err } + ctx = context.WithoutCancel(ctx) return fsw.getDefaultStore().KvDelete(ctx, key) }