Skip to content

Commit

Permalink
Revert "perf(123pan): optimize rate limiting (AlistGo#6859)"
Browse files Browse the repository at this point in the history
This reverts commit AlistGo#6859, which introduced performance enhancements in AlistGo#6859. Unfortunately, these changes led to unintended consequences resulting in account bans. The revert is necessary until a more thorough review and testing can be conducted to ensure no negative impacts on account status.
  • Loading branch information
xrgzs committed Jul 27, 2024
1 parent af9c6af commit f1f1c5e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
16 changes: 9 additions & 7 deletions drivers/123/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (d *Pan123) Drop(ctx context.Context) error {
}

func (d *Pan123) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
files, err := d.getFiles(ctx, dir.GetID(), dir.GetName())
files, err := d.getFiles(dir.GetID(), dir.GetName())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -247,6 +247,9 @@ func (d *Pan123) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
}
_, err = uploader.UploadWithContext(ctx, input)
}
if err != nil {
return err
}
_, err = d.request(UploadComplete, http.MethodPost, func(req *resty.Request) {
req.SetBody(base.Json{
"fileId": resp.Data.FileId,
Expand All @@ -255,12 +258,11 @@ func (d *Pan123) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
return err
}

func (d *Pan123) APIRateLimit(ctx context.Context, api string) error {
value, _ := d.apiRateLimit.LoadOrStore(api,
rate.NewLimiter(rate.Every(700*time.Millisecond), 1))
limiter := value.(*rate.Limiter)

return limiter.Wait(ctx)
func (d *Pan123) APIRateLimit(api string) bool {
limiter, _ := d.apiRateLimit.LoadOrStore(api,
rate.NewLimiter(rate.Every(time.Millisecond*700), 1))
ins := limiter.(*rate.Limiter)
return ins.Allow()
}

var _ driver.Driver = (*Pan123)(nil)
10 changes: 5 additions & 5 deletions drivers/123/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package _123

import (
"context"
"errors"
"fmt"
"hash/crc32"
Expand All @@ -15,7 +14,7 @@ import (

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2"
resty "github.com/go-resty/resty/v2"
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -234,14 +233,15 @@ func (d *Pan123) request(url string, method string, callback base.ReqCallback, r
return body, nil
}

func (d *Pan123) getFiles(ctx context.Context, parentId string, name string) ([]File, error) {
func (d *Pan123) getFiles(parentId string, name string) ([]File, error) {
page := 1
total := 0
res := make([]File, 0)
// 2024-02-06 fix concurrency by 123pan
for {
if err := d.APIRateLimit(ctx, FileList); err != nil {
return nil, err
if !d.APIRateLimit(FileList) {
time.Sleep(time.Millisecond * 200)
continue
}
var resp Files
query := map[string]string{
Expand Down
13 changes: 6 additions & 7 deletions drivers/123_share/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (d *Pan123Share) Drop(ctx context.Context) error {

func (d *Pan123Share) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
// TODO return the files list, required
files, err := d.getFiles(ctx, dir.GetID())
files, err := d.getFiles(dir.GetID())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -150,12 +150,11 @@ func (d *Pan123Share) Put(ctx context.Context, dstDir model.Obj, stream model.Fi
// return nil, errs.NotSupport
//}

func (d *Pan123Share) APIRateLimit(ctx context.Context, api string) error {
value, _ := d.apiRateLimit.LoadOrStore(api,
rate.NewLimiter(rate.Every(700*time.Millisecond), 1))
limiter := value.(*rate.Limiter)

return limiter.Wait(ctx)
func (d *Pan123Share) APIRateLimit(api string) bool {
limiter, _ := d.apiRateLimit.LoadOrStore(api,
rate.NewLimiter(rate.Every(time.Millisecond*700), 1))
ins := limiter.(*rate.Limiter)
return ins.Allow()
}

var _ driver.Driver = (*Pan123Share)(nil)
8 changes: 4 additions & 4 deletions drivers/123_share/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package _123Share

import (
"context"
"errors"
"fmt"
"hash/crc32"
Expand Down Expand Up @@ -81,12 +80,13 @@ func (d *Pan123Share) request(url string, method string, callback base.ReqCallba
return body, nil
}

func (d *Pan123Share) getFiles(ctx context.Context, parentId string) ([]File, error) {
func (d *Pan123Share) getFiles(parentId string) ([]File, error) {
page := 1
res := make([]File, 0)
for {
if err := d.APIRateLimit(ctx, FileList); err != nil {
return nil, err
if !d.APIRateLimit(FileList) {
time.Sleep(time.Millisecond * 200)
continue
}
var resp Files
query := map[string]string{
Expand Down

0 comments on commit f1f1c5e

Please sign in to comment.