From 6745dcc139dba93eb86fb50ad7849494ef7a1b6b Mon Sep 17 00:00:00 2001 From: KirCute_ECT <951206789@qq.com> Date: Mon, 30 Dec 2024 22:55:09 +0800 Subject: [PATCH] feat(task): attach creator to `user` of the context (#7729) --- internal/task/base.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/internal/task/base.go b/internal/task/base.go index 93f413a7111..22b167417db 100644 --- a/internal/task/base.go +++ b/internal/task/base.go @@ -1,17 +1,21 @@ package task import ( + "context" "github.com/alist-org/alist/v3/internal/model" "github.com/xhofe/tache" + "sync" "time" ) type TaskExtension struct { tache.Base - Creator *model.User - startTime *time.Time - endTime *time.Time - totalBytes int64 + ctx context.Context + ctxInitMutex sync.Mutex + Creator *model.User + startTime *time.Time + endTime *time.Time + totalBytes int64 } func (t *TaskExtension) SetCreator(creator *model.User) { @@ -51,6 +55,17 @@ func (t *TaskExtension) GetTotalBytes() int64 { return t.totalBytes } +func (t *TaskExtension) Ctx() context.Context { + if t.ctx == nil { + t.ctxInitMutex.Lock() + if t.ctx == nil { + t.ctx = context.WithValue(t.Base.Ctx(), "user", t.Creator) + } + t.ctxInitMutex.Unlock() + } + return t.ctx +} + type TaskExtensionInfo interface { tache.TaskWithInfo GetCreator() *model.User