Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions drivers/guangyapan/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net/url"
"strings"
"sync"
"time"

"github.com/alist-org/alist/v3/drivers/base"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"golang.org/x/time/rate"
)

const (
Expand All @@ -36,8 +38,15 @@ type GuangYaPan struct {

resolvedRootFolderID string
rootFolderResolved bool

// apiRateLimit throttles requests per API endpoint so that batch operations
// (e.g. copying many files cross-storage) don't flood the upstream API.
apiRateLimit sync.Map
}

// apiRateInterval is the minimum gap between two requests to the same endpoint.
const apiRateInterval = 500 * time.Millisecond

func (d *GuangYaPan) Config() driver.Config {
return config
}
Expand Down Expand Up @@ -793,10 +802,18 @@ func (d *GuangYaPan) accountErr(desc, short string, resp *resty.Response) string
return msg
}

func (d *GuangYaPan) apiRateLimitWait(ctx context.Context, path string) error {
value, _ := d.apiRateLimit.LoadOrStore(path, rate.NewLimiter(rate.Every(apiRateInterval), 1))
return value.(*rate.Limiter).Wait(ctx)
}

func (d *GuangYaPan) postAPI(ctx context.Context, path string, body any, out any) error {
if strings.TrimSpace(d.AccessToken) == "" {
return errors.New("access token is empty")
}
if err := d.apiRateLimitWait(ctx, path); err != nil {
return err
}
resp, err := d.apiClient.R().
SetContext(ctx).
SetHeader("Authorization", "Bearer "+d.AccessToken).
Expand Down
Loading