@@ -16,7 +16,7 @@ import (
1616type lazyRatelimiter struct {
1717 db database.Connection
1818 options []config.Option
19- client ratelimiter.Client
19+ c ratelimiter.Client
2020 m sync.Mutex
2121}
2222
@@ -30,13 +30,13 @@ func Ratelimiter(db database.Connection, options []config.Option) ratelimiter.Cl
3030func (r * lazyRatelimiter ) Close (ctx context.Context ) (err error ) {
3131 r .m .Lock ()
3232 defer r .m .Unlock ()
33- if r .client == nil {
33+ if r .c == nil {
3434 return nil
3535 }
3636 defer func () {
37- r .client = nil
37+ r .c = nil
3838 }()
39- err = r .client .Close (ctx )
39+ err = r .c .Close (ctx )
4040 if err != nil {
4141 return xerrors .WithStackTrace (err )
4242 }
@@ -48,9 +48,8 @@ func (r *lazyRatelimiter) CreateResource(
4848 coordinationNodePath string ,
4949 resource ratelimiter.Resource ,
5050) (err error ) {
51- r .init ()
5251 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
53- return r .client .CreateResource (ctx , coordinationNodePath , resource )
52+ return r .client () .CreateResource (ctx , coordinationNodePath , resource )
5453 })
5554}
5655
@@ -59,9 +58,8 @@ func (r *lazyRatelimiter) AlterResource(
5958 coordinationNodePath string ,
6059 resource ratelimiter.Resource ,
6160) (err error ) {
62- r .init ()
6361 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
64- return r .client .AlterResource (ctx , coordinationNodePath , resource )
62+ return r .client () .AlterResource (ctx , coordinationNodePath , resource )
6563 })
6664}
6765
@@ -70,9 +68,8 @@ func (r *lazyRatelimiter) DropResource(
7068 coordinationNodePath string ,
7169 resourcePath string ,
7270) (err error ) {
73- r .init ()
7471 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
75- return r .client .DropResource (ctx , coordinationNodePath , resourcePath )
72+ return r .client () .DropResource (ctx , coordinationNodePath , resourcePath )
7673 })
7774}
7875
@@ -82,9 +79,8 @@ func (r *lazyRatelimiter) ListResource(
8279 resourcePath string ,
8380 recursive bool ,
8481) (paths []string , err error ) {
85- r .init ()
8682 err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
87- paths , err = r .client .ListResource (ctx , coordinationNodePath , resourcePath , recursive )
83+ paths , err = r .client () .ListResource (ctx , coordinationNodePath , resourcePath , recursive )
8884 return xerrors .WithStackTrace (err )
8985 })
9086 return paths , xerrors .WithStackTrace (err )
@@ -95,9 +91,8 @@ func (r *lazyRatelimiter) DescribeResource(
9591 coordinationNodePath string ,
9692 resourcePath string ,
9793) (resource * ratelimiter.Resource , err error ) {
98- r .init ()
9994 err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
100- resource , err = r .client .DescribeResource (ctx , coordinationNodePath , resourcePath )
95+ resource , err = r .client () .DescribeResource (ctx , coordinationNodePath , resourcePath )
10196 return xerrors .WithStackTrace (err )
10297 })
10398 return resource , xerrors .WithStackTrace (err )
@@ -110,16 +105,16 @@ func (r *lazyRatelimiter) AcquireResource(
110105 amount uint64 ,
111106 opts ... options.AcquireOption ,
112107) (err error ) {
113- r .init ()
114108 return retry .Retry (ctx , func (ctx context.Context ) (err error ) {
115- return r .client .AcquireResource (ctx , coordinationNodePath , resourcePath , amount , opts ... )
109+ return r .client () .AcquireResource (ctx , coordinationNodePath , resourcePath , amount , opts ... )
116110 })
117111}
118112
119- func (r * lazyRatelimiter ) init () {
113+ func (r * lazyRatelimiter ) client () ratelimiter. Client {
120114 r .m .Lock ()
121- if r .client == nil {
122- r .client = builder .New (r .db , r .options )
115+ defer r .m .Unlock ()
116+ if r .c == nil {
117+ r .c = builder .New (r .db , r .options )
123118 }
124- r . m . Unlock ()
119+ return r . c
125120}
0 commit comments