Skip to content

Commit 8f4f7d1

Browse files
authored
feat(doubao): Add rate limiting (#1618)
1 parent ee2c77a commit 8f4f7d1

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

drivers/doubao/driver.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
1616
"github.com/go-resty/resty/v2"
1717
"github.com/google/uuid"
18+
"golang.org/x/time/rate"
1819
)
1920

2021
type Doubao struct {
@@ -23,6 +24,7 @@ type Doubao struct {
2324
*UploadToken
2425
UserId string
2526
uploadThread int
27+
limiter *rate.Limiter
2628
}
2729

2830
func (d *Doubao) Config() driver.Config {
@@ -61,6 +63,17 @@ func (d *Doubao) Init(ctx context.Context) error {
6163
d.UploadToken = uploadToken
6264
}
6365

66+
if d.LimitRate > 0 {
67+
d.limiter = rate.NewLimiter(rate.Limit(d.LimitRate), 1)
68+
}
69+
70+
return nil
71+
}
72+
73+
func (d *Doubao) WaitLimit(ctx context.Context) error {
74+
if d.limiter != nil {
75+
return d.limiter.Wait(ctx)
76+
}
6477
return nil
6578
}
6679

@@ -69,6 +82,10 @@ func (d *Doubao) Drop(ctx context.Context) error {
6982
}
7083

7184
func (d *Doubao) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
85+
if err := d.WaitLimit(ctx); err != nil {
86+
return nil, err
87+
}
88+
7289
var files []model.Obj
7390
fileList, err := d.getFiles(dir.GetID(), "")
7491
if err != nil {
@@ -95,6 +112,10 @@ func (d *Doubao) List(ctx context.Context, dir model.Obj, args model.ListArgs) (
95112
}
96113

97114
func (d *Doubao) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
115+
if err := d.WaitLimit(ctx); err != nil {
116+
return nil, err
117+
}
118+
98119
var downloadUrl string
99120

100121
if u, ok := file.(*Object); ok {
@@ -160,6 +181,10 @@ func (d *Doubao) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
160181
}
161182

162183
func (d *Doubao) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
184+
if err := d.WaitLimit(ctx); err != nil {
185+
return err
186+
}
187+
163188
var r UploadNodeResp
164189
_, err := d.request("/samantha/aispace/upload_node", http.MethodPost, func(req *resty.Request) {
165190
req.SetBody(base.Json{
@@ -177,6 +202,10 @@ func (d *Doubao) MakeDir(ctx context.Context, parentDir model.Obj, dirName strin
177202
}
178203

179204
func (d *Doubao) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
205+
if err := d.WaitLimit(ctx); err != nil {
206+
return err
207+
}
208+
180209
var r UploadNodeResp
181210
_, err := d.request("/samantha/aispace/move_node", http.MethodPost, func(req *resty.Request) {
182211
req.SetBody(base.Json{
@@ -191,6 +220,10 @@ func (d *Doubao) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
191220
}
192221

193222
func (d *Doubao) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
223+
if err := d.WaitLimit(ctx); err != nil {
224+
return err
225+
}
226+
194227
var r BaseResp
195228
_, err := d.request("/samantha/aispace/rename_node", http.MethodPost, func(req *resty.Request) {
196229
req.SetBody(base.Json{
@@ -207,6 +240,10 @@ func (d *Doubao) Copy(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj,
207240
}
208241

209242
func (d *Doubao) Remove(ctx context.Context, obj model.Obj) error {
243+
if err := d.WaitLimit(ctx); err != nil {
244+
return err
245+
}
246+
210247
var r BaseResp
211248
_, err := d.request("/samantha/aispace/delete_node", http.MethodPost, func(req *resty.Request) {
212249
req.SetBody(base.Json{"node_list": []base.Json{{"id": obj.GetID()}}})
@@ -215,6 +252,10 @@ func (d *Doubao) Remove(ctx context.Context, obj model.Obj) error {
215252
}
216253

217254
func (d *Doubao) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) {
255+
if err := d.WaitLimit(ctx); err != nil {
256+
return nil, err
257+
}
258+
218259
// 根据MIME类型确定数据类型
219260
mimetype := file.GetMimetype()
220261
dataType := FileDataType

drivers/doubao/meta.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ type Addition struct {
1010
// driver.RootPath
1111
driver.RootID
1212
// define other
13-
Cookie string `json:"cookie" type:"text"`
14-
UploadThread string `json:"upload_thread" default:"3"`
15-
DownloadApi string `json:"download_api" type:"select" options:"get_file_url,get_download_info" default:"get_file_url"`
13+
Cookie string `json:"cookie" type:"text"`
14+
UploadThread string `json:"upload_thread" default:"3"`
15+
DownloadApi string `json:"download_api" type:"select" options:"get_file_url,get_download_info" default:"get_file_url"`
16+
LimitRate float64 `json:"limit_rate" type:"float" default:"2" help:"limit all api request rate ([limit]r/1s)"`
1617
}
1718

1819
var config = driver.Config{
@@ -23,6 +24,10 @@ var config = driver.Config{
2324

2425
func init() {
2526
op.RegisterDriver(func() driver.Driver {
26-
return &Doubao{}
27+
return &Doubao{
28+
Addition: Addition{
29+
LimitRate: 2,
30+
},
31+
}
2732
})
2833
}

0 commit comments

Comments
 (0)