@@ -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,18 +32,17 @@ func (r *lazyRatelimiter) Close(ctx context.Context) (err error) {
3332 if r .c == nil {
3433 return nil
3534 }
36- err = r .c .Close (ctx )
37- if err != nil {
38- return xerrors .WithStackTrace (err )
39- }
40- return nil
35+ return r .c .Close (ctx )
4136}
4237
4338func (r * lazyRatelimiter ) CreateResource (
4439 ctx context.Context ,
4540 coordinationNodePath string ,
4641 resource ratelimiter.Resource ,
4742) (err error ) {
43+ if ! r .config .AutoRetry () {
44+ return r .client ().CreateResource (ctx , coordinationNodePath , resource )
45+ }
4846 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
4947 return r .client ().CreateResource (ctx , coordinationNodePath , resource )
5048 })
@@ -55,6 +53,9 @@ func (r *lazyRatelimiter) AlterResource(
5553 coordinationNodePath string ,
5654 resource ratelimiter.Resource ,
5755) (err error ) {
56+ if ! r .config .AutoRetry () {
57+ return r .client ().AlterResource (ctx , coordinationNodePath , resource )
58+ }
5859 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
5960 return r .client ().AlterResource (ctx , coordinationNodePath , resource )
6061 })
@@ -65,6 +66,9 @@ func (r *lazyRatelimiter) DropResource(
6566 coordinationNodePath string ,
6667 resourcePath string ,
6768) (err error ) {
69+ if ! r .config .AutoRetry () {
70+ return r .client ().DropResource (ctx , coordinationNodePath , resourcePath )
71+ }
6872 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
6973 return r .client ().DropResource (ctx , coordinationNodePath , resourcePath )
7074 })
@@ -76,23 +80,29 @@ func (r *lazyRatelimiter) ListResource(
7680 resourcePath string ,
7781 recursive bool ,
7882) (paths []string , err error ) {
83+ if ! r .config .AutoRetry () {
84+ return r .client ().ListResource (ctx , coordinationNodePath , resourcePath , recursive )
85+ }
7986 err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
8087 paths , err = r .client ().ListResource (ctx , coordinationNodePath , resourcePath , recursive )
81- return xerrors . WithStackTrace ( err )
88+ return err
8289 })
83- return paths , xerrors . WithStackTrace ( err )
90+ return paths , err
8491}
8592
8693func (r * lazyRatelimiter ) DescribeResource (
8794 ctx context.Context ,
8895 coordinationNodePath string ,
8996 resourcePath string ,
9097) (resource * ratelimiter.Resource , err error ) {
98+ if ! r .config .AutoRetry () {
99+ return r .client ().DescribeResource (ctx , coordinationNodePath , resourcePath )
100+ }
91101 err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
92102 resource , err = r .client ().DescribeResource (ctx , coordinationNodePath , resourcePath )
93- return xerrors . WithStackTrace ( err )
103+ return err
94104 })
95- return resource , xerrors . WithStackTrace ( err )
105+ return resource , err
96106}
97107
98108func (r * lazyRatelimiter ) AcquireResource (
@@ -102,6 +112,9 @@ func (r *lazyRatelimiter) AcquireResource(
102112 amount uint64 ,
103113 opts ... options.AcquireOption ,
104114) (err error ) {
115+ if ! r .config .AutoRetry () {
116+ return r .client ().AcquireResource (ctx , coordinationNodePath , resourcePath , amount , opts ... )
117+ }
105118 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
106119 return r .client ().AcquireResource (ctx , coordinationNodePath , resourcePath , amount , opts ... )
107120 })
@@ -111,7 +124,7 @@ func (r *lazyRatelimiter) client() ratelimiter.Client {
111124 r .m .Lock ()
112125 defer r .m .Unlock ()
113126 if r .c == nil {
114- r .c = builder .New (r .db , r .options )
127+ r .c = builder .New (r .db , r .config )
115128 }
116129 return r .c
117130}
0 commit comments