mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-20 04:36:09 +08:00

* fix(aliyundrive_open): limit rate for `Remove` and `MakeDir`; reduce limit for `List` and `Link` (close #724) * Update drivers/aliyundrive_open/driver.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: 火星大王 <34576789+huoxingdawang@users.noreply.github.com> * Update drivers/aliyundrive_open/driver.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: 火星大王 <34576789+huoxingdawang@users.noreply.github.com> * fix(aliyundrive_open): limit rate for every request * fix(aliyundrive_open): fix limiter not work on reference driver * fix(aliyundrive_open): typo * fix(aliyundrive_open): limiter not set to nil after free * fix(aliyundrive_share): limit rate for every request --------- Signed-off-by: 火星大王 <34576789+huoxingdawang@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package aliyundrive_share
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
// See issue https://github.com/OpenListTeam/OpenList/issues/724
|
|
// Seems there is no limit per user.
|
|
|
|
type limiterType int
|
|
|
|
const (
|
|
limiterList limiterType = iota
|
|
limiterLink
|
|
limiterOther
|
|
)
|
|
|
|
const (
|
|
listRateLimit = 3.9 // 4 per second in document, but we use 3.9 per second to be safe
|
|
linkRateLimit = 0.9 // 1 per second in document, but we use 0.9 per second to be safe
|
|
otherRateLimit = 14.9 // 15 per second in document, but we use 14.9 per second to be safe
|
|
)
|
|
|
|
type limiter struct {
|
|
list *rate.Limiter
|
|
link *rate.Limiter
|
|
other *rate.Limiter
|
|
}
|
|
|
|
func getLimiter() *limiter {
|
|
return &limiter{
|
|
list: rate.NewLimiter(rate.Limit(listRateLimit), 1),
|
|
link: rate.NewLimiter(rate.Limit(linkRateLimit), 1),
|
|
other: rate.NewLimiter(rate.Limit(otherRateLimit), 1),
|
|
}
|
|
}
|
|
|
|
func (l *limiter) wait(ctx context.Context, typ limiterType) error {
|
|
if l == nil {
|
|
return fmt.Errorf("driver not init")
|
|
}
|
|
switch typ {
|
|
case limiterList:
|
|
return l.list.Wait(ctx)
|
|
case limiterLink:
|
|
return l.link.Wait(ctx)
|
|
case limiterOther:
|
|
return l.other.Wait(ctx)
|
|
default:
|
|
return fmt.Errorf("unknown limiter type")
|
|
}
|
|
}
|
|
func (l *limiter) free() {
|
|
|
|
}
|
|
func (d *AliyundriveShare) wait(ctx context.Context, typ limiterType) error {
|
|
if d == nil {
|
|
return fmt.Errorf("driver not init")
|
|
}
|
|
//if d.ref != nil {
|
|
// return d.ref.wait(ctx, typ) // If this is a reference driver, wait on the reference driver.
|
|
//}
|
|
return d.limiter.wait(ctx, typ)
|
|
}
|