mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-19 20:26:26 +08:00
refactor: pass api_url
through context (#457)
* refactor: pass `api_url` through context * 移除 LinkArgs.HttpReq * pref(alias): 减少不必要下载代理 * 修复bug * net: 支持1并发 分片下载
This commit is contained in:
@ -2,7 +2,6 @@ package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/OpenListTeam/OpenList/internal/conf"
|
||||
@ -12,12 +11,21 @@ import (
|
||||
|
||||
type TaskExtension struct {
|
||||
tache.Base
|
||||
ctx context.Context
|
||||
ctxInitMutex sync.Mutex
|
||||
Creator *model.User
|
||||
startTime *time.Time
|
||||
endTime *time.Time
|
||||
totalBytes int64
|
||||
Creator *model.User
|
||||
startTime *time.Time
|
||||
endTime *time.Time
|
||||
totalBytes int64
|
||||
ApiUrl string
|
||||
}
|
||||
|
||||
func (t *TaskExtension) SetCtx(ctx context.Context) {
|
||||
if t.Creator != nil {
|
||||
ctx = context.WithValue(ctx, "user", t.Creator)
|
||||
}
|
||||
if len(t.ApiUrl) > 0 {
|
||||
ctx = context.WithValue(ctx, conf.ApiUrlKey, t.ApiUrl)
|
||||
}
|
||||
t.Base.SetCtx(ctx)
|
||||
}
|
||||
|
||||
func (t *TaskExtension) SetCreator(creator *model.User) {
|
||||
@ -57,29 +65,18 @@ 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
|
||||
}
|
||||
|
||||
func (t *TaskExtension) ReinitCtx() {
|
||||
if !conf.Conf.Tasks.AllowRetryCanceled {
|
||||
return
|
||||
}
|
||||
func (t *TaskExtension) ReinitCtx() error {
|
||||
select {
|
||||
case <-t.Base.Ctx().Done():
|
||||
case <-t.Ctx().Done():
|
||||
if !conf.Conf.Tasks.AllowRetryCanceled {
|
||||
return t.Ctx().Err()
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.SetCtx(ctx)
|
||||
t.SetCancelFunc(cancel)
|
||||
t.ctx = nil
|
||||
default:
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type TaskExtensionInfo interface {
|
||||
|
Reference in New Issue
Block a user