@@ -3,7 +3,6 @@ package aliyundrive_open
3
3
import (
4
4
"context"
5
5
"errors"
6
- "fmt"
7
6
"net/http"
8
7
"path/filepath"
9
8
"time"
@@ -13,7 +12,6 @@ import (
13
12
"github.com/OpenListTeam/OpenList/v4/internal/errs"
14
13
"github.com/OpenListTeam/OpenList/v4/internal/model"
15
14
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
16
- "github.com/OpenListTeam/rateg"
17
15
"github.com/go-resty/resty/v2"
18
16
log "github.com/sirupsen/logrus"
19
17
)
@@ -24,9 +22,8 @@ type AliyundriveOpen struct {
24
22
25
23
DriveId string
26
24
27
- limitList func (ctx context.Context , data base.Json ) (* Files , error )
28
- limitLink func (ctx context.Context , file model.Obj ) (* model.Link , error )
29
- ref * AliyundriveOpen
25
+ limiter * limiter
26
+ ref * AliyundriveOpen
30
27
}
31
28
32
29
func (d * AliyundriveOpen ) Config () driver.Config {
@@ -38,25 +35,23 @@ func (d *AliyundriveOpen) GetAddition() driver.Additional {
38
35
}
39
36
40
37
func (d * AliyundriveOpen ) Init (ctx context.Context ) error {
38
+ d .limiter = getLimiterForUser (globalLimiterUserID ) // First create a globally shared limiter to limit the initial requests.
41
39
if d .LIVPDownloadFormat == "" {
42
40
d .LIVPDownloadFormat = "jpeg"
43
41
}
44
42
if d .DriveType == "" {
45
43
d .DriveType = "default"
46
44
}
47
- res , err := d .request ("/adrive/v1.0/user/getDriveInfo" , http .MethodPost , nil )
45
+ res , err := d .request (ctx , limiterOther , "/adrive/v1.0/user/getDriveInfo" , http .MethodPost , nil )
48
46
if err != nil {
47
+ d .limiter .free ()
48
+ d .limiter = nil
49
49
return err
50
50
}
51
51
d .DriveId = utils .Json .Get (res , d .DriveType + "_drive_id" ).ToString ()
52
- d .limitList = rateg .LimitFnCtx (d .list , rateg.LimitFnOption {
53
- Limit : 4 ,
54
- Bucket : 1 ,
55
- })
56
- d .limitLink = rateg .LimitFnCtx (d .link , rateg.LimitFnOption {
57
- Limit : 1 ,
58
- Bucket : 1 ,
59
- })
52
+ userid := utils .Json .Get (res , "user_id" ).ToString ()
53
+ d .limiter .free ()
54
+ d .limiter = getLimiterForUser (userid ) // Allocate a corresponding limiter for each user.
60
55
return nil
61
56
}
62
57
@@ -70,6 +65,8 @@ func (d *AliyundriveOpen) InitReference(storage driver.Driver) error {
70
65
}
71
66
72
67
func (d * AliyundriveOpen ) Drop (ctx context.Context ) error {
68
+ d .limiter .free ()
69
+ d .limiter = nil
73
70
d .ref = nil
74
71
return nil
75
72
}
@@ -87,9 +84,6 @@ func (d *AliyundriveOpen) GetRoot(ctx context.Context) (model.Obj, error) {
87
84
}
88
85
89
86
func (d * AliyundriveOpen ) List (ctx context.Context , dir model.Obj , args model.ListArgs ) ([]model.Obj , error ) {
90
- if d .limitList == nil {
91
- return nil , fmt .Errorf ("driver not init" )
92
- }
93
87
files , err := d .getFiles (ctx , dir .GetID ())
94
88
if err != nil {
95
89
return nil , err
@@ -107,8 +101,8 @@ func (d *AliyundriveOpen) List(ctx context.Context, dir model.Obj, args model.Li
107
101
return objs , err
108
102
}
109
103
110
- func (d * AliyundriveOpen ) link (ctx context.Context , file model.Obj ) (* model.Link , error ) {
111
- res , err := d .request ("/adrive/v1.0/openFile/getDownloadUrl" , http .MethodPost , func (req * resty.Request ) {
104
+ func (d * AliyundriveOpen ) Link (ctx context.Context , file model.Obj , args model. LinkArgs ) (* model.Link , error ) {
105
+ res , err := d .request (ctx , limiterLink , "/adrive/v1.0/openFile/getDownloadUrl" , http .MethodPost , func (req * resty.Request ) {
112
106
req .SetBody (base.Json {
113
107
"drive_id" : d .DriveId ,
114
108
"file_id" : file .GetID (),
@@ -132,17 +126,10 @@ func (d *AliyundriveOpen) link(ctx context.Context, file model.Obj) (*model.Link
132
126
}, nil
133
127
}
134
128
135
- func (d * AliyundriveOpen ) Link (ctx context.Context , file model.Obj , args model.LinkArgs ) (* model.Link , error ) {
136
- if d .limitLink == nil {
137
- return nil , fmt .Errorf ("driver not init" )
138
- }
139
- return d .limitLink (ctx , file )
140
- }
141
-
142
129
func (d * AliyundriveOpen ) MakeDir (ctx context.Context , parentDir model.Obj , dirName string ) (model.Obj , error ) {
143
130
nowTime , _ := getNowTime ()
144
131
newDir := File {CreatedAt : nowTime , UpdatedAt : nowTime }
145
- _ , err := d .request ("/adrive/v1.0/openFile/create" , http .MethodPost , func (req * resty.Request ) {
132
+ _ , err := d .request (ctx , limiterOther , "/adrive/v1.0/openFile/create" , http .MethodPost , func (req * resty.Request ) {
146
133
req .SetBody (base.Json {
147
134
"drive_id" : d .DriveId ,
148
135
"parent_file_id" : parentDir .GetID (),
@@ -168,7 +155,7 @@ func (d *AliyundriveOpen) MakeDir(ctx context.Context, parentDir model.Obj, dirN
168
155
169
156
func (d * AliyundriveOpen ) Move (ctx context.Context , srcObj , dstDir model.Obj ) (model.Obj , error ) {
170
157
var resp MoveOrCopyResp
171
- _ , err := d .request ("/adrive/v1.0/openFile/move" , http .MethodPost , func (req * resty.Request ) {
158
+ _ , err := d .request (ctx , limiterOther , "/adrive/v1.0/openFile/move" , http .MethodPost , func (req * resty.Request ) {
172
159
req .SetBody (base.Json {
173
160
"drive_id" : d .DriveId ,
174
161
"file_id" : srcObj .GetID (),
@@ -198,7 +185,7 @@ func (d *AliyundriveOpen) Move(ctx context.Context, srcObj, dstDir model.Obj) (m
198
185
199
186
func (d * AliyundriveOpen ) Rename (ctx context.Context , srcObj model.Obj , newName string ) (model.Obj , error ) {
200
187
var newFile File
201
- _ , err := d .request ("/adrive/v1.0/openFile/update" , http .MethodPost , func (req * resty.Request ) {
188
+ _ , err := d .request (ctx , limiterOther , "/adrive/v1.0/openFile/update" , http .MethodPost , func (req * resty.Request ) {
202
189
req .SetBody (base.Json {
203
190
"drive_id" : d .DriveId ,
204
191
"file_id" : srcObj .GetID (),
@@ -230,7 +217,7 @@ func (d *AliyundriveOpen) Rename(ctx context.Context, srcObj model.Obj, newName
230
217
231
218
func (d * AliyundriveOpen ) Copy (ctx context.Context , srcObj , dstDir model.Obj ) error {
232
219
var resp MoveOrCopyResp
233
- _ , err := d .request ("/adrive/v1.0/openFile/copy" , http .MethodPost , func (req * resty.Request ) {
220
+ _ , err := d .request (ctx , limiterOther , "/adrive/v1.0/openFile/copy" , http .MethodPost , func (req * resty.Request ) {
234
221
req .SetBody (base.Json {
235
222
"drive_id" : d .DriveId ,
236
223
"file_id" : srcObj .GetID (),
@@ -256,7 +243,7 @@ func (d *AliyundriveOpen) Remove(ctx context.Context, obj model.Obj) error {
256
243
if d .RemoveWay == "delete" {
257
244
uri = "/adrive/v1.0/openFile/delete"
258
245
}
259
- _ , err := d .request (uri , http .MethodPost , func (req * resty.Request ) {
246
+ _ , err := d .request (ctx , limiterOther , uri , http .MethodPost , func (req * resty.Request ) {
260
247
req .SetBody (base.Json {
261
248
"drive_id" : d .DriveId ,
262
249
"file_id" : obj .GetID (),
@@ -295,7 +282,7 @@ func (d *AliyundriveOpen) Other(ctx context.Context, args model.OtherArgs) (inte
295
282
default :
296
283
return nil , errs .NotSupport
297
284
}
298
- _ , err := d .request (uri , http .MethodPost , func (req * resty.Request ) {
285
+ _ , err := d .request (ctx , limiterOther , uri , http .MethodPost , func (req * resty.Request ) {
299
286
req .SetBody (data ).SetResult (& resp )
300
287
})
301
288
if err != nil {
0 commit comments