fix: load balance

This commit is contained in:
Xhofe 2022-03-31 21:52:19 +08:00
parent ced61da33a
commit b52e1e8be3
4 changed files with 33 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"sort"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -131,6 +132,7 @@ var balanceMap sync.Map
// GetBalancedAccount 根据名称获取账号,负载均衡之后的 // GetBalancedAccount 根据名称获取账号,负载均衡之后的
func GetBalancedAccount(name string) (Account, bool) { func GetBalancedAccount(name string) (Account, bool) {
accounts := GetAccountsByPath(name) accounts := GetAccountsByPath(name)
log.Debugf("accounts: %+v", accounts)
accountNum := len(accounts) accountNum := len(accounts)
switch accountNum { switch accountNum {
case 0: case 0:
@ -203,7 +205,7 @@ func GetAccountsByPath(path string) []Account {
name := utils.ParsePath(v.Name) name := utils.ParsePath(v.Name)
bIndex := strings.LastIndex(name, balance) bIndex := strings.LastIndex(name, balance)
if bIndex != -1 { if bIndex != -1 {
name = v.Name[:bIndex] name = name[:bIndex]
} }
// 不是这个账号 // 不是这个账号
if path != name && !strings.HasPrefix(path, name+"/") { if path != name && !strings.HasPrefix(path, name+"/") {
@ -220,18 +222,29 @@ func GetAccountsByPath(path string) []Account {
} }
accounts = append(accounts, v) accounts = append(accounts, v)
} }
sort.Slice(accounts, func(i, j int) bool {
return accounts[i].Name < accounts[j].Name
})
return accounts return accounts
} }
// GetAccountFilesByPath 根据路径获取账号虚拟文件 // GetAccountFilesByPath 根据路径获取账号虚拟文件
// 如有账号: /a/b,/a/c,/a/d/e,/a/b.balance1,/av // 如有账号: /a/b,/a/c,/a/d/e,/a/b.balance1,/av
// GetAccountFilesByPath(/a) => b,c,d // GetAccountFilesByPath(/a) => b,c,d
func GetAccountFilesByPath(prefix string) ([]File, error) { func GetAccountFilesByPath(prefix string) []File {
files := make([]File, 0) files := make([]File, 0)
var accounts []Account accounts := make([]Account, AccountsCount())
if err := conf.DB.Order(columnName("index")).Find(&accounts).Error; err != nil { i := 0
return nil, err for _, v := range accountsMap {
accounts[i] = v
i += 1
} }
sort.Slice(accounts, func(i, j int) bool {
if accounts[i].Index == accounts[j].Index {
return accounts[i].Name < accounts[j].Name
}
return accounts[i].Index < accounts[j].Index
})
prefix = utils.ParsePath(prefix) prefix = utils.ParsePath(prefix)
set := make(map[string]interface{}) set := make(map[string]interface{})
for _, v := range accounts { for _, v := range accounts {
@ -257,5 +270,5 @@ func GetAccountFilesByPath(prefix string) ([]File, error) {
}) })
set[name] = nil set[name] = nil
} }
return files, nil return files
} }

View File

@ -33,7 +33,12 @@ func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
if !ok { if !ok {
return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type) return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type)
} }
return &account, strings.TrimPrefix(rawPath, utils.ParsePath(account.Name)), driver, nil name := utils.ParsePath(account.Name)
bIndex := strings.LastIndex(name, ".balance")
if bIndex != -1 {
name = name[:bIndex]
}
return &account, strings.TrimPrefix(rawPath, name), driver, nil
} }
func ErrorResp(c *gin.Context, err error, code int) { func ErrorResp(c *gin.Context, err error, code int) {

View File

@ -4,22 +4,21 @@ import (
"github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate" "github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
log "github.com/sirupsen/logrus"
) )
func Path(rawPath string) (*model.File, []model.File, *model.Account, base.Driver, string, error) { func Path(rawPath string) (*model.File, []model.File, *model.Account, base.Driver, string, error) {
account, path, driver, err := ParsePath(rawPath) account, path, driver, err := ParsePath(rawPath)
if err != nil { if err != nil {
if err.Error() == "path not found" { if err.Error() == "path not found" {
accountFiles, err := model.GetAccountFilesByPath(rawPath) accountFiles := model.GetAccountFilesByPath(rawPath)
if err != nil {
return nil, nil, nil, nil, "", err
}
if len(accountFiles) != 0 { if len(accountFiles) != 0 {
return nil, accountFiles, nil, nil, path, nil return nil, accountFiles, nil, nil, path, nil
} }
} }
return nil, nil, nil, nil, "", err return nil, nil, nil, nil, "", err
} }
log.Debugln("use account: ", account.Name)
file, files, err := operate.Path(driver, account, path) file, files, err := operate.Path(driver, account, path)
if err != nil { if err != nil {
return nil, nil, nil, nil, "", err return nil, nil, nil, nil, "", err
@ -27,10 +26,7 @@ func Path(rawPath string) (*model.File, []model.File, *model.Account, base.Drive
if file != nil { if file != nil {
return file, nil, account, driver, path, nil return file, nil, account, driver, path, nil
} else { } else {
accountFiles, err := model.GetAccountFilesByPath(rawPath) accountFiles := model.GetAccountFilesByPath(rawPath)
if err != nil {
return nil, nil, nil, nil, "", err
}
files = append(files, accountFiles...) files = append(files, accountFiles...)
return nil, files, account, driver, path, nil return nil, files, account, driver, path, nil
} }

View File

@ -18,6 +18,10 @@ func RefreshFolder(c *gin.Context) {
} }
account, path_, _, err := common.ParsePath(req.Path) account, path_, _, err := common.ParsePath(req.Path)
if err != nil { if err != nil {
if err.Error() == "path not found" && req.Path == "/" {
common.SuccessResp(c)
return
}
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }