Skip to content

Commit 8ffcd62

Browse files
committed
define call function and call it
1 parent b955c5f commit 8ffcd62

File tree

5 files changed

+107
-89
lines changed

5 files changed

+107
-89
lines changed

internal/coordination/client.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ func (c *Client) CreateNode(ctx context.Context, path string, config coordinatio
4040
if c == nil {
4141
return xerrors.WithStackTrace(errNilClient)
4242
}
43-
if !c.config.AutoRetry() {
43+
call := func(ctx context.Context) error {
4444
return xerrors.WithStackTrace(c.createNode(ctx, path, config))
4545
}
46-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
47-
return xerrors.WithStackTrace(c.createNode(ctx, path, config))
48-
}, retry.WithStackTrace())
46+
if !c.config.AutoRetry() {
47+
return call(ctx)
48+
}
49+
return retry.Retry(ctx, call, retry.WithStackTrace())
4950
}
5051

5152
func (c *Client) createNode(ctx context.Context, path string, config coordination.NodeConfig) (err error) {
@@ -76,12 +77,13 @@ func (c *Client) AlterNode(ctx context.Context, path string, config coordination
7677
if c == nil {
7778
return xerrors.WithStackTrace(errNilClient)
7879
}
79-
if !c.config.AutoRetry() {
80+
call := func(ctx context.Context) error {
8081
return xerrors.WithStackTrace(c.alterNode(ctx, path, config))
8182
}
82-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
83-
return xerrors.WithStackTrace(c.alterNode(ctx, path, config))
84-
}, retry.WithStackTrace())
83+
if !c.config.AutoRetry() {
84+
return call(ctx)
85+
}
86+
return retry.Retry(ctx, call, retry.WithStackTrace())
8587
}
8688

8789
func (c *Client) alterNode(ctx context.Context, path string, config coordination.NodeConfig) (err error) {
@@ -112,12 +114,13 @@ func (c *Client) DropNode(ctx context.Context, path string) (err error) {
112114
if c == nil {
113115
return xerrors.WithStackTrace(errNilClient)
114116
}
115-
if !c.config.AutoRetry() {
117+
call := func(ctx context.Context) error {
116118
return xerrors.WithStackTrace(c.dropNode(ctx, path))
117119
}
118-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
119-
return xerrors.WithStackTrace(c.dropNode(ctx, path))
120-
}, retry.WithStackTrace())
120+
if !c.config.AutoRetry() {
121+
return call(ctx)
122+
}
123+
return retry.Retry(ctx, call, retry.WithStackTrace())
121124
}
122125

123126
func (c *Client) dropNode(ctx context.Context, path string) (err error) {
@@ -148,14 +151,15 @@ func (c *Client) DescribeNode(
148151
err = xerrors.WithStackTrace(errNilClient)
149152
return
150153
}
151-
if !c.config.AutoRetry() {
152-
entry, config, err = c.describeNode(ctx, path)
153-
return entry, config, xerrors.WithStackTrace(err)
154-
}
155-
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
154+
call := func(ctx context.Context) error {
156155
entry, config, err = c.describeNode(ctx, path)
157156
return xerrors.WithStackTrace(err)
158-
}, retry.WithStackTrace())
157+
}
158+
if !c.config.AutoRetry() {
159+
err = call(ctx)
160+
return
161+
}
162+
err = retry.Retry(ctx, call, retry.WithStackTrace())
159163
return
160164
}
161165

internal/ratelimiter/client.go

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ func (c *Client) CreateResource(
5353
if c == nil {
5454
return xerrors.WithStackTrace(errNilClient)
5555
}
56-
if !c.config.AutoRetry() {
56+
call := func(ctx context.Context) error {
5757
return xerrors.WithStackTrace(c.createResource(ctx, coordinationNodePath, resource))
5858
}
59-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
60-
return c.createResource(ctx, coordinationNodePath, resource)
61-
}, retry.WithStackTrace())
59+
if !c.config.AutoRetry() {
60+
return call(ctx)
61+
}
62+
return retry.Retry(ctx, call, retry.WithStackTrace())
6263
}
6364

6465
func (c *Client) createResource(
@@ -95,12 +96,13 @@ func (c *Client) AlterResource(
9596
if c == nil {
9697
return xerrors.WithStackTrace(errNilClient)
9798
}
98-
if !c.config.AutoRetry() {
99+
call := func(ctx context.Context) error {
99100
return xerrors.WithStackTrace(c.alterResource(ctx, coordinationNodePath, resource))
100101
}
101-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
102-
return xerrors.WithStackTrace(c.alterResource(ctx, coordinationNodePath, resource))
103-
}, retry.WithStackTrace())
102+
if !c.config.AutoRetry() {
103+
return call(ctx)
104+
}
105+
return retry.Retry(ctx, call, retry.WithStackTrace())
104106
}
105107

106108
func (c *Client) alterResource(
@@ -137,12 +139,13 @@ func (c *Client) DropResource(
137139
if c == nil {
138140
return xerrors.WithStackTrace(errNilClient)
139141
}
140-
if !c.config.AutoRetry() {
142+
call := func(ctx context.Context) error {
141143
return xerrors.WithStackTrace(c.dropResource(ctx, coordinationNodePath, resourcePath))
142144
}
143-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
144-
return xerrors.WithStackTrace(c.dropResource(ctx, coordinationNodePath, resourcePath))
145-
}, retry.WithStackTrace())
145+
if !c.config.AutoRetry() {
146+
return call(ctx)
147+
}
148+
return retry.Retry(ctx, call, retry.WithStackTrace())
146149
}
147150

148151
func (c *Client) dropResource(
@@ -172,14 +175,15 @@ func (c *Client) ListResource(
172175
if c == nil {
173176
return list, xerrors.WithStackTrace(errNilClient)
174177
}
175-
if !c.config.AutoRetry() {
176-
list, err = c.listResource(ctx, coordinationNodePath, resourcePath, recursive)
177-
return list, xerrors.WithStackTrace(err)
178-
}
179-
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
178+
call := func(ctx context.Context) error {
180179
list, err = c.listResource(ctx, coordinationNodePath, resourcePath, recursive)
181180
return xerrors.WithStackTrace(err)
182-
}, retry.WithIdempotent(true), retry.WithStackTrace())
181+
}
182+
if !c.config.AutoRetry() {
183+
err = call(ctx)
184+
return
185+
}
186+
err = retry.Retry(ctx, call, retry.WithIdempotent(true), retry.WithStackTrace())
183187
return
184188
}
185189

@@ -222,14 +226,15 @@ func (c *Client) DescribeResource(
222226
if c == nil {
223227
return resource, xerrors.WithStackTrace(errNilClient)
224228
}
225-
if !c.config.AutoRetry() {
226-
resource, err = c.describeResource(ctx, coordinationNodePath, resourcePath)
227-
return resource, xerrors.WithStackTrace(err)
228-
}
229-
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
229+
call := func(ctx context.Context) error {
230230
resource, err = c.describeResource(ctx, coordinationNodePath, resourcePath)
231231
return xerrors.WithStackTrace(err)
232-
}, retry.WithIdempotent(true), retry.WithStackTrace())
232+
}
233+
if !c.config.AutoRetry() {
234+
err = call(ctx)
235+
return
236+
}
237+
err = retry.Retry(ctx, call, retry.WithIdempotent(true), retry.WithStackTrace())
233238
return
234239
}
235240

@@ -286,12 +291,13 @@ func (c *Client) AcquireResource(
286291
if c == nil {
287292
return xerrors.WithStackTrace(errNilClient)
288293
}
289-
if !c.config.AutoRetry() {
294+
call := func(ctx context.Context) error {
290295
return xerrors.WithStackTrace(c.acquireResource(ctx, coordinationNodePath, resourcePath, amount, opts...))
291296
}
292-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
293-
return xerrors.WithStackTrace(c.acquireResource(ctx, coordinationNodePath, resourcePath, amount, opts...))
294-
}, retry.WithStackTrace())
297+
if !c.config.AutoRetry() {
298+
return call(ctx)
299+
}
300+
return retry.Retry(ctx, call, retry.WithStackTrace())
295301
}
296302

297303
func (c *Client) acquireResource(

internal/scheme/client.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ func (c *Client) MakeDirectory(ctx context.Context, path string) (err error) {
4646
if c == nil {
4747
return xerrors.WithStackTrace(errNilClient)
4848
}
49-
if !c.config.AutoRetry() {
49+
call := func(ctx context.Context) error {
5050
return xerrors.WithStackTrace(c.makeDirectory(ctx, path))
5151
}
52-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
53-
return xerrors.WithStackTrace(c.makeDirectory(ctx, path))
54-
}, retry.WithStackTrace())
52+
if !c.config.AutoRetry() {
53+
return call(ctx)
54+
}
55+
return retry.Retry(ctx, call, retry.WithStackTrace())
5556
}
5657

5758
func (c *Client) makeDirectory(ctx context.Context, path string) (err error) {
@@ -74,12 +75,13 @@ func (c *Client) RemoveDirectory(ctx context.Context, path string) (err error) {
7475
if c == nil {
7576
return xerrors.WithStackTrace(errNilClient)
7677
}
77-
if !c.config.AutoRetry() {
78+
call := func(ctx context.Context) error {
7879
return xerrors.WithStackTrace(c.removeDirectory(ctx, path))
7980
}
80-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
81-
return xerrors.WithStackTrace(c.removeDirectory(ctx, path))
82-
}, retry.WithStackTrace())
81+
if !c.config.AutoRetry() {
82+
return call(ctx)
83+
}
84+
return retry.Retry(ctx, call, retry.WithStackTrace())
8385
}
8486

8587
func (c *Client) removeDirectory(ctx context.Context, path string) (err error) {
@@ -102,14 +104,15 @@ func (c *Client) ListDirectory(ctx context.Context, path string) (d scheme.Direc
102104
if c == nil {
103105
return d, xerrors.WithStackTrace(errNilClient)
104106
}
105-
if !c.config.AutoRetry() {
106-
d, err = c.listDirectory(ctx, path)
107-
return d, xerrors.WithStackTrace(err)
108-
}
109-
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
107+
call := func(ctx context.Context) error {
110108
d, err = c.listDirectory(ctx, path)
111109
return xerrors.WithStackTrace(err)
112-
}, retry.WithIdempotent(true), retry.WithStackTrace())
110+
}
111+
if !c.config.AutoRetry() {
112+
err = call(ctx)
113+
return
114+
}
115+
err = retry.Retry(ctx, call, retry.WithIdempotent(true), retry.WithStackTrace())
113116
return
114117
}
115118

@@ -149,14 +152,15 @@ func (c *Client) DescribePath(ctx context.Context, path string) (e scheme.Entry,
149152
if c == nil {
150153
return e, xerrors.WithStackTrace(errNilClient)
151154
}
152-
if !c.config.AutoRetry() {
153-
e, err = c.describePath(ctx, path)
154-
return e, xerrors.WithStackTrace(err)
155-
}
156-
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
155+
call := func(ctx context.Context) error {
157156
e, err = c.describePath(ctx, path)
158157
return xerrors.WithStackTrace(err)
159-
}, retry.WithIdempotent(true), retry.WithStackTrace())
158+
}
159+
if !c.config.AutoRetry() {
160+
err = call(ctx)
161+
return
162+
}
163+
err = retry.Retry(ctx, call, retry.WithIdempotent(true), retry.WithStackTrace())
160164
return
161165
}
162166

@@ -192,12 +196,13 @@ func (c *Client) ModifyPermissions(ctx context.Context, path string, opts ...sch
192196
if c == nil {
193197
return xerrors.WithStackTrace(errNilClient)
194198
}
195-
if !c.config.AutoRetry() {
199+
call := func(ctx context.Context) error {
196200
return xerrors.WithStackTrace(c.modifyPermissions(ctx, path, opts...))
197201
}
198-
return retry.Retry(ctx, func(ctx context.Context) (err error) {
199-
return xerrors.WithStackTrace(c.modifyPermissions(ctx, path, opts...))
200-
}, retry.WithStackTrace())
202+
if !c.config.AutoRetry() {
203+
return call(ctx)
204+
}
205+
return retry.Retry(ctx, call, retry.WithStackTrace())
201206
}
202207

203208
func (c *Client) modifyPermissions(ctx context.Context, path string, opts ...scheme.PermissionsOption) (err error) {

internal/scripting/client.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ func (c *Client) Execute(
4444
if c == nil {
4545
return r, xerrors.WithStackTrace(errNilClient)
4646
}
47-
if !c.config.AutoRetry() {
48-
r, err = c.execute(ctx, query, params)
49-
return r, xerrors.WithStackTrace(err)
50-
}
51-
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
47+
call := func(ctx context.Context) error {
5248
r, err = c.execute(ctx, query, params)
5349
return xerrors.WithStackTrace(err)
54-
}, retry.WithStackTrace())
50+
}
51+
if !c.config.AutoRetry() {
52+
err = call(ctx)
53+
return
54+
}
55+
err = retry.Retry(ctx, call, retry.WithStackTrace())
5556
return
5657
}
5758

@@ -109,14 +110,15 @@ func (c *Client) Explain(
109110
if c == nil {
110111
return e, xerrors.WithStackTrace(errNilClient)
111112
}
112-
if !c.config.AutoRetry() {
113-
e, err = c.explain(ctx, query, mode)
114-
return e, xerrors.WithStackTrace(err)
115-
}
116-
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
113+
call := func(ctx context.Context) error {
117114
e, err = c.explain(ctx, query, mode)
118115
return xerrors.WithStackTrace(err)
119-
}, retry.WithStackTrace())
116+
}
117+
if !c.config.AutoRetry() {
118+
err = call(ctx)
119+
return
120+
}
121+
err = retry.Retry(ctx, call, retry.WithStackTrace())
120122
return
121123
}
122124

@@ -172,14 +174,15 @@ func (c *Client) StreamExecute(
172174
if c == nil {
173175
return r, xerrors.WithStackTrace(errNilClient)
174176
}
175-
if !c.config.AutoRetry() {
176-
r, err = c.streamExecute(ctx, query, params)
177-
return r, xerrors.WithStackTrace(err)
178-
}
179-
err = retry.Retry(ctx, func(ctx context.Context) (err error) {
177+
call := func(ctx context.Context) error {
180178
r, err = c.streamExecute(ctx, query, params)
181179
return xerrors.WithStackTrace(err)
182-
}, retry.WithStackTrace())
180+
}
181+
if !c.config.AutoRetry() {
182+
err = call(ctx)
183+
return
184+
}
185+
err = retry.Retry(ctx, call, retry.WithStackTrace())
183186
return
184187
}
185188

test/scripting_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestScripting(t *testing.T) {
8686
return fmt.Errorf("unexpected sum: %v", sum)
8787
}
8888
return res.Err()
89-
}, retry.WithIdempotent(true), retry.WithStackTrace()); err != nil {
89+
}, retry.WithIdempotent(true)); err != nil {
9090
t.Fatalf("Execute failed: %v", err)
9191
}
9292
// StreamExecute

0 commit comments

Comments
 (0)