@@ -8,22 +8,21 @@ import (
88 builder "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter"
99 "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter/config"
1010 "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter/options"
11- "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1211 "github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter"
1312 "github.com/ydb-platform/ydb-go-sdk/v3/retry"
1413)
1514
1615type lazyRatelimiter struct {
17- db database.Connection
18- options [] config.Option
19- c ratelimiter.Client
20- m sync.Mutex
16+ db database.Connection
17+ config config.Config
18+ c ratelimiter.Client
19+ m sync.Mutex
2120}
2221
2322func Ratelimiter (db database.Connection , options []config.Option ) ratelimiter.Client {
2423 return & lazyRatelimiter {
25- db : db ,
26- options : options ,
24+ db : db ,
25+ config : config . New ( options ... ) ,
2726 }
2827}
2928
@@ -33,21 +32,17 @@ func (r *lazyRatelimiter) Close(ctx context.Context) (err error) {
3332 if r .c == nil {
3433 return nil
3534 }
36- defer func () {
37- r .c = nil
38- }()
39- err = r .c .Close (ctx )
40- if err != nil {
41- return xerrors .WithStackTrace (err )
42- }
43- return nil
35+ return r .c .Close (ctx )
4436}
4537
4638func (r * lazyRatelimiter ) CreateResource (
4739 ctx context.Context ,
4840 coordinationNodePath string ,
4941 resource ratelimiter.Resource ,
5042) (err error ) {
43+ if ! r .config .AutoRetry () {
44+ return r .client ().CreateResource (ctx , coordinationNodePath , resource )
45+ }
5146 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
5247 return r .client ().CreateResource (ctx , coordinationNodePath , resource )
5348 })
@@ -58,6 +53,9 @@ func (r *lazyRatelimiter) AlterResource(
5853 coordinationNodePath string ,
5954 resource ratelimiter.Resource ,
6055) (err error ) {
56+ if ! r .config .AutoRetry () {
57+ return r .client ().AlterResource (ctx , coordinationNodePath , resource )
58+ }
6159 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
6260 return r .client ().AlterResource (ctx , coordinationNodePath , resource )
6361 })
@@ -68,6 +66,9 @@ func (r *lazyRatelimiter) DropResource(
6866 coordinationNodePath string ,
6967 resourcePath string ,
7068) (err error ) {
69+ if ! r .config .AutoRetry () {
70+ return r .client ().DropResource (ctx , coordinationNodePath , resourcePath )
71+ }
7172 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
7273 return r .client ().DropResource (ctx , coordinationNodePath , resourcePath )
7374 })
@@ -79,23 +80,29 @@ func (r *lazyRatelimiter) ListResource(
7980 resourcePath string ,
8081 recursive bool ,
8182) (paths []string , err error ) {
83+ if ! r .config .AutoRetry () {
84+ return r .client ().ListResource (ctx , coordinationNodePath , resourcePath , recursive )
85+ }
8286 err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
8387 paths , err = r .client ().ListResource (ctx , coordinationNodePath , resourcePath , recursive )
84- return xerrors . WithStackTrace ( err )
88+ return err
8589 })
86- return paths , xerrors . WithStackTrace ( err )
90+ return paths , err
8791}
8892
8993func (r * lazyRatelimiter ) DescribeResource (
9094 ctx context.Context ,
9195 coordinationNodePath string ,
9296 resourcePath string ,
9397) (resource * ratelimiter.Resource , err error ) {
98+ if ! r .config .AutoRetry () {
99+ return r .client ().DescribeResource (ctx , coordinationNodePath , resourcePath )
100+ }
94101 err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
95102 resource , err = r .client ().DescribeResource (ctx , coordinationNodePath , resourcePath )
96- return xerrors . WithStackTrace ( err )
103+ return err
97104 })
98- return resource , xerrors . WithStackTrace ( err )
105+ return resource , err
99106}
100107
101108func (r * lazyRatelimiter ) AcquireResource (
@@ -105,6 +112,9 @@ func (r *lazyRatelimiter) AcquireResource(
105112 amount uint64 ,
106113 opts ... options.AcquireOption ,
107114) (err error ) {
115+ if ! r .config .AutoRetry () {
116+ return r .client ().AcquireResource (ctx , coordinationNodePath , resourcePath , amount , opts ... )
117+ }
108118 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
109119 return r .client ().AcquireResource (ctx , coordinationNodePath , resourcePath , amount , opts ... )
110120 })
@@ -114,7 +124,7 @@ func (r *lazyRatelimiter) client() ratelimiter.Client {
114124 r .m .Lock ()
115125 defer r .m .Unlock ()
116126 if r .c == nil {
117- r .c = builder .New (r .db , r .options )
127+ r .c = builder .New (r .db , r .config )
118128 }
119129 return r .c
120130}
0 commit comments