mirror of https://github.com/Xhofe/alist
✨ load balance
parent
a22903533e
commit
f5c5162a9b
|
@ -45,7 +45,7 @@ var onedriveHostMap = map[string]Host{
|
|||
|
||||
func (driver Onedrive) GetMetaUrl(account *model.Account, auth bool, path string) string {
|
||||
path = filepath.Join(account.RootFolder, path)
|
||||
log.Debugf(path)
|
||||
//log.Debugf(path)
|
||||
host, _ := onedriveHostMap[account.Zone]
|
||||
if auth {
|
||||
return host.Oauth
|
||||
|
@ -253,7 +253,7 @@ func (driver Onedrive) Request(url string, method int, headers, query, form map[
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
//log.Debug(res.String())
|
||||
if e.Error.Code != "" {
|
||||
if e.Error.Code == "InvalidAuthenticationToken" {
|
||||
err = driver.RefreshToken(account)
|
||||
|
|
|
@ -2,6 +2,9 @@ package model
|
|||
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -45,7 +48,9 @@ type Account struct {
|
|||
ExtractFolder string `json:"extract_folder"`
|
||||
}
|
||||
|
||||
var accountsMap = map[string]Account{}
|
||||
var accountsMap = make(map[string]Account)
|
||||
|
||||
var balance = ".balance"
|
||||
|
||||
// SaveAccount save account to database
|
||||
func SaveAccount(account *Account) error {
|
||||
|
@ -100,6 +105,41 @@ func GetAccount(name string) (Account, bool) {
|
|||
return account, ok
|
||||
}
|
||||
|
||||
func GetAccountsByName(name string) []Account {
|
||||
accounts := make([]Account, 0)
|
||||
for _, v := range accountsMap {
|
||||
if v.Name == name || (strings.HasSuffix(v.Name, balance) && strings.HasPrefix(v.Name, name)) {
|
||||
accounts = append(accounts, v)
|
||||
}
|
||||
}
|
||||
return accounts
|
||||
}
|
||||
|
||||
var balanceMap sync.Map
|
||||
|
||||
func GetBalancedAccount(name string) (Account, bool) {
|
||||
accounts := GetAccountsByName(name)
|
||||
accountNum := len(accounts)
|
||||
switch accountNum {
|
||||
case 0:
|
||||
return Account{}, false
|
||||
case 1:
|
||||
return accounts[0], true
|
||||
default:
|
||||
cur, ok := balanceMap.Load(name)
|
||||
if ok {
|
||||
i := cur.(int)
|
||||
i = (i + 1) % accountNum
|
||||
balanceMap.Store(name, i)
|
||||
log.Debugln("use: ", i)
|
||||
return accounts[i], true
|
||||
} else {
|
||||
balanceMap.Store(name, 0)
|
||||
return accounts[0], true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetAccountById(id uint) (*Account, error) {
|
||||
var account Account
|
||||
account.ID = id
|
||||
|
@ -116,6 +156,9 @@ func GetAccountFiles() ([]File, error) {
|
|||
return nil, err
|
||||
}
|
||||
for _, v := range accounts {
|
||||
if strings.HasSuffix(v.Name, balance) {
|
||||
continue
|
||||
}
|
||||
files = append(files, File{
|
||||
Name: v.Name,
|
||||
Size: 0,
|
||||
|
|
|
@ -40,7 +40,7 @@ func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
|
|||
path = "/" + strings.Join(paths[2:], "/")
|
||||
name = paths[1]
|
||||
}
|
||||
account, ok := model.GetAccount(name)
|
||||
account, ok := model.GetBalancedAccount(name)
|
||||
if !ok {
|
||||
return nil, "", nil, fmt.Errorf("no [%s] account", name)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue