Skip to content

Commit c97da45

Browse files
committed
refactor retry/retry and upload with off gocognit linter
1 parent 14394d5 commit c97da45

File tree

3 files changed

+73
-50
lines changed

3 files changed

+73
-50
lines changed

CHANGELOG.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
* Refactored `internal/topic/topicwriterinternal/queue_test.go` and extract funcs
2-
* Refactored `log/driver.go` and extract funcs
3-
* Refactored `internal/table/scanner/scanner.go` and extract funcs
4-
* Refactored `log/topic.go` and extract funcs
5-
* Refactored `internal/table/scanner/scanner_test.go` and extract funcs
6-
* Refactored `log/sql.go` and extract funcs
7-
* Refactored `metrics/driver.go` and extract funcs
8-
* Refactored `metrics/sql.go` and extract funcs
9-
* Refactored `internal/table/retry_test.go` and extract func `retry_test.checkResultsRetryWithCustomErrors`
10-
* Refactored `internal/table/client.go` and extract func `client.onCloseSession`
11-
* Refactored `internal/decimal/decimal.go` and extract func `decimal.dotStringAnalysis`
12-
* Refactored `internal/backoff/backoff_test.go` and extract func `backoff_test.checkExpWithAct` for compare
13-
* Refactored `internal/xsql/dsn.go` and extract func `dsn.bindTablePathPrefixInConnectorOptions`
14-
* Refactored `sugar/path.go` and extract funcs `path.removeWithPrefix`, `path.removeEntry`
15-
* Refactored `internal/stack/record.go` and extract func `record.findFileNameAndPkgPath`
1+
* Refactored `retry/retry.go` and extract func `retry.RecoveryCallbackWrapper` for gocognit linter
2+
* Refactored `retry/sql_test.go` and extract func `sql_test.canRetry` for gocognit linter
3+
* Refactored `internal/topic/topicwriterinternal/queue_test.go` and extract funcs for gocognit linter
4+
* Refactored `log/driver.go` and extract funcs for gocognit linter
5+
* Refactored `internal/table/scanner/scanner.go` and extract funcs for gocognit linter
6+
* Refactored `log/topic.go` and extract funcs for gocognit linter
7+
* Refactored `internal/table/scanner/scanner_test.go` and extract funcs for gocognit linter
8+
* Refactored `log/sql.go` and extract funcs for gocognit linter
9+
* Refactored `metrics/driver.go` and extract funcs for gocognit linter
10+
* Refactored `metrics/sql.go` and extract funcs for gocognit linter
11+
* Refactored `internal/table/retry_test.go` and extract func `retry_test.checkResultsRetryWithCustomErrors` for gocognit linter
12+
* Refactored `internal/table/client.go` and extract func `client.onCloseSession` for gocognit linter
13+
* Refactored `internal/decimal/decimal.go` and extract func `decimal.dotStringAnalysis` for gocognit linter
14+
* Refactored `internal/backoff/backoff_test.go` and extract func `backoff_test.checkExpWithAct` for gocognit linter
15+
* Refactored `internal/xsql/dsn.go` and extract func `dsn.bindTablePathPrefixInConnectorOptions` for gocognit linter
16+
* Refactored `sugar/path.go` and extract funcs `path.removeWithPrefix`, `path.removeEntry` for gocognit linter
17+
* Refactored `internal/stack/record.go` and extract func `record.findFileNameAndPkgPath` for gocognit linter
1618
* Fixed topic writer infinite reconnections in some cases
1719
* Refactored nil on err `internal/grpcwrapper/rawydb/issues.go`, when golangci-lint nilerr enabled
1820
* Refactored nil on err `internal/grpcwrapper/rawtopic/describe_topic.go`, when golangci-lint nilerr enabled

retry/retry.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -276,21 +276,7 @@ func Retry(ctx context.Context, op retryOperation, opts ...Option) (finalErr err
276276
)
277277

278278
default:
279-
err := func() (err error) {
280-
if options.panicCallback != nil {
281-
defer func() {
282-
if e := recover(); e != nil {
283-
options.panicCallback(e)
284-
err = xerrors.WithStackTrace(
285-
fmt.Errorf("panic recovered: %v", e),
286-
)
287-
}
288-
}()
289-
}
290-
291-
return op(ctx)
292-
}()
293-
279+
err := RecoveryCallbackWrapper(ctx, op, options)
294280
if err == nil {
295281
return nil
296282
}
@@ -335,6 +321,25 @@ func Retry(ctx context.Context, op retryOperation, opts ...Option) (finalErr err
335321
}
336322
}
337323

324+
// RecoveryCallbackWrapper is a function that wraps the provided `op` retry operation
325+
// with a panic recovery mechanism. If the `options.panicCallback` is specified, it
326+
// is invoked with the recovered value, and an error is returned with the recovered value wrapped
327+
// as the cause. This function returns the result of the `op` retry operation.
328+
func RecoveryCallbackWrapper(context context.Context, op retryOperation, options *retryOptions) (err error) {
329+
if options.panicCallback != nil {
330+
defer func() {
331+
if e := recover(); e != nil {
332+
options.panicCallback(e)
333+
err = xerrors.WithStackTrace(
334+
fmt.Errorf("panic recovered: %v", e),
335+
)
336+
}
337+
}()
338+
}
339+
340+
return op(context)
341+
}
342+
338343
// Check returns retry mode for queryErr.
339344
func Check(err error) (m retryMode) {
340345
code, errType, backoffType, deleteSession := xerrors.Check(err)

retry/sql_test.go

+36-20
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ func (m *mockStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (
180180
return m.conn.QueryContext(ctx, m.query, args)
181181
}
182182

183-
//nolint:nestif
184183
func TestDoTx(t *testing.T) {
185184
for _, idempotentType := range []idempotency{
186185
idempotent,
@@ -228,27 +227,44 @@ func TestDoTx(t *testing.T) {
228227
},
229228
}),
230229
)
231-
if tt.canRetry[idempotentType] {
232-
if err != nil {
233-
t.Errorf("unexpected err after attempts=%d and driver conns=%d: %v)", attempts, m.conns, err)
234-
}
235-
if attempts <= 1 {
236-
t.Errorf("must be attempts > 1 (actual=%d), driver conns=%d)", attempts, m.conns)
237-
}
238-
if tt.deleteSession {
239-
if m.conns <= 1 {
240-
t.Errorf("must be retry on different conns (attempts=%d, driver conns=%d)", attempts, m.conns)
241-
}
242-
} else {
243-
if m.conns > 1 {
244-
t.Errorf("must be retry on single conn (attempts=%d, driver conns=%d)", attempts, m.conns)
245-
}
246-
}
247-
} else if err == nil {
248-
t.Errorf("unexpected nil err (attempts=%d, driver conns=%d)", attempts, m.conns)
249-
}
230+
canRetry(t, tt, idempotentType, err, attempts, m)
250231
})
251232
}
252233
})
253234
}
254235
}
236+
237+
// canRetry checks if a retry can be performed based on the given parameters.
238+
//
239+
//nolint:nestif
240+
func canRetry(t *testing.T, tt struct {
241+
err error
242+
backoff backoff.Type
243+
deleteSession bool
244+
canRetry map[idempotency]bool
245+
},
246+
idempotentType idempotency,
247+
err error,
248+
attempts int,
249+
m *mockConnector,
250+
) {
251+
if tt.canRetry[idempotentType] {
252+
if err != nil {
253+
t.Errorf("unexpected err after attempts=%d and driver conns=%d: %v)", attempts, m.conns, err)
254+
}
255+
if attempts <= 1 {
256+
t.Errorf("must be attempts > 1 (actual=%d), driver conns=%d)", attempts, m.conns)
257+
}
258+
if tt.deleteSession {
259+
if m.conns <= 1 {
260+
t.Errorf("must be retry on different conns (attempts=%d, driver conns=%d)", attempts, m.conns)
261+
}
262+
} else {
263+
if m.conns > 1 {
264+
t.Errorf("must be retry on single conn (attempts=%d, driver conns=%d)", attempts, m.conns)
265+
}
266+
}
267+
} else if err == nil {
268+
t.Errorf("unexpected nil err (attempts=%d, driver conns=%d)", attempts, m.conns)
269+
}
270+
}

0 commit comments

Comments
 (0)