@@ -3,7 +3,6 @@ package aliyundrive_open
33import (
44 "context"
55 "errors"
6- "fmt"
76 "net/http"
87 "path/filepath"
98 "time"
@@ -13,7 +12,6 @@ import (
1312 "github.com/OpenListTeam/OpenList/v4/internal/errs"
1413 "github.com/OpenListTeam/OpenList/v4/internal/model"
1514 "github.com/OpenListTeam/OpenList/v4/pkg/utils"
16- "github.com/OpenListTeam/rateg"
1715 "github.com/go-resty/resty/v2"
1816 log "github.com/sirupsen/logrus"
1917)
@@ -24,9 +22,8 @@ type AliyundriveOpen struct {
2422
2523 DriveId string
2624
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
3027}
3128
3229func (d * AliyundriveOpen ) Config () driver.Config {
@@ -38,25 +35,23 @@ func (d *AliyundriveOpen) GetAddition() driver.Additional {
3835}
3936
4037func (d * AliyundriveOpen ) Init (ctx context.Context ) error {
38+ d .limiter = getLimiterForUser (globalLimiterUserID ) // First create a globally shared limiter to limit the initial requests.
4139 if d .LIVPDownloadFormat == "" {
4240 d .LIVPDownloadFormat = "jpeg"
4341 }
4442 if d .DriveType == "" {
4543 d .DriveType = "default"
4644 }
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 )
4846 if err != nil {
47+ d .limiter .free ()
48+ d .limiter = nil
4949 return err
5050 }
5151 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.
6055 return nil
6156}
6257
@@ -70,6 +65,8 @@ func (d *AliyundriveOpen) InitReference(storage driver.Driver) error {
7065}
7166
7267func (d * AliyundriveOpen ) Drop (ctx context.Context ) error {
68+ d .limiter .free ()
69+ d .limiter = nil
7370 d .ref = nil
7471 return nil
7572}
@@ -87,9 +84,6 @@ func (d *AliyundriveOpen) GetRoot(ctx context.Context) (model.Obj, error) {
8784}
8885
8986func (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- }
9387 files , err := d .getFiles (ctx , dir .GetID ())
9488 if err != nil {
9589 return nil , err
@@ -107,8 +101,8 @@ func (d *AliyundriveOpen) List(ctx context.Context, dir model.Obj, args model.Li
107101 return objs , err
108102}
109103
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 ) {
112106 req .SetBody (base.Json {
113107 "drive_id" : d .DriveId ,
114108 "file_id" : file .GetID (),
@@ -132,17 +126,10 @@ func (d *AliyundriveOpen) link(ctx context.Context, file model.Obj) (*model.Link
132126 }, nil
133127}
134128
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-
142129func (d * AliyundriveOpen ) MakeDir (ctx context.Context , parentDir model.Obj , dirName string ) (model.Obj , error ) {
143130 nowTime , _ := getNowTime ()
144131 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 ) {
146133 req .SetBody (base.Json {
147134 "drive_id" : d .DriveId ,
148135 "parent_file_id" : parentDir .GetID (),
@@ -168,7 +155,7 @@ func (d *AliyundriveOpen) MakeDir(ctx context.Context, parentDir model.Obj, dirN
168155
169156func (d * AliyundriveOpen ) Move (ctx context.Context , srcObj , dstDir model.Obj ) (model.Obj , error ) {
170157 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 ) {
172159 req .SetBody (base.Json {
173160 "drive_id" : d .DriveId ,
174161 "file_id" : srcObj .GetID (),
@@ -198,7 +185,7 @@ func (d *AliyundriveOpen) Move(ctx context.Context, srcObj, dstDir model.Obj) (m
198185
199186func (d * AliyundriveOpen ) Rename (ctx context.Context , srcObj model.Obj , newName string ) (model.Obj , error ) {
200187 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 ) {
202189 req .SetBody (base.Json {
203190 "drive_id" : d .DriveId ,
204191 "file_id" : srcObj .GetID (),
@@ -230,7 +217,7 @@ func (d *AliyundriveOpen) Rename(ctx context.Context, srcObj model.Obj, newName
230217
231218func (d * AliyundriveOpen ) Copy (ctx context.Context , srcObj , dstDir model.Obj ) error {
232219 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 ) {
234221 req .SetBody (base.Json {
235222 "drive_id" : d .DriveId ,
236223 "file_id" : srcObj .GetID (),
@@ -256,7 +243,7 @@ func (d *AliyundriveOpen) Remove(ctx context.Context, obj model.Obj) error {
256243 if d .RemoveWay == "delete" {
257244 uri = "/adrive/v1.0/openFile/delete"
258245 }
259- _ , err := d .request (uri , http .MethodPost , func (req * resty.Request ) {
246+ _ , err := d .request (ctx , limiterOther , uri , http .MethodPost , func (req * resty.Request ) {
260247 req .SetBody (base.Json {
261248 "drive_id" : d .DriveId ,
262249 "file_id" : obj .GetID (),
@@ -295,7 +282,7 @@ func (d *AliyundriveOpen) Other(ctx context.Context, args model.OtherArgs) (inte
295282 default :
296283 return nil , errs .NotSupport
297284 }
298- _ , err := d .request (uri , http .MethodPost , func (req * resty.Request ) {
285+ _ , err := d .request (ctx , limiterOther , uri , http .MethodPost , func (req * resty.Request ) {
299286 req .SetBody (data ).SetResult (& resp )
300287 })
301288 if err != nil {
0 commit comments