Skip to content

Commit 03f4605

Browse files
committed
refactoring of internal/table.Client.CreateSession retryer usage
1 parent b84477e commit 03f4605

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

internal/table/client.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,51 +100,58 @@ func (c *Client) CreateSession(ctx context.Context, opts ...table.Option) (_ tab
100100
if c.isClosed() {
101101
return nil, xerrors.WithStackTrace(errClosedClient)
102102
}
103-
var s *session
104103
createSession := func(ctx context.Context) (*session, error) {
105-
s, err = c.build(ctx)
104+
s, err := c.build(ctx)
106105
if err != nil {
107106
return nil, xerrors.WithStackTrace(err)
108107
}
109108

110109
return s, nil
111110
}
112111
if !c.config.AutoRetry() {
113-
s, err = createSession(ctx)
112+
s, err := createSession(ctx)
114113
if err != nil {
115114
return nil, xerrors.WithStackTrace(err)
116115
}
117116

118117
return s, nil
119118
}
120-
err = retry.Retry(ctx,
121-
func(ctx context.Context) (err error) {
122-
s, err = createSession(ctx)
123-
if err != nil {
124-
return xerrors.WithStackTrace(err)
125-
}
126119

127-
return nil
128-
},
120+
var (
121+
onDone = trace.TableOnCreateSession(c.config.Trace(), &ctx,
122+
stack.FunctionID(
123+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table.(*Client).CreateSession"),
124+
)
125+
attempts = 0
126+
s *session
127+
)
128+
defer func() {
129+
if s != nil {
130+
onDone(s, attempts, err)
131+
} else {
132+
onDone(nil, attempts, err)
133+
}
134+
}()
135+
136+
s, err = retry.RetryWithResult(ctx, createSession,
129137
append(
130138
[]retry.Option{
131139
retry.WithIdempotent(true),
132140
retry.WithTrace(&trace.Retry{
133141
OnRetry: func(info trace.RetryLoopStartInfo) func(trace.RetryLoopDoneInfo) {
134-
onDone := trace.TableOnCreateSession(c.config.Trace(), info.Context,
135-
stack.FunctionID(
136-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table.(*Client).CreateSession"))
137-
138142
return func(info trace.RetryLoopDoneInfo) {
139-
onDone(s, info.Attempts, info.Error)
143+
attempts = info.Attempts
140144
}
141145
},
142146
}),
143147
}, c.retryOptions(opts...).RetryOptions...,
144148
)...,
145149
)
150+
if err != nil {
151+
return nil, xerrors.WithStackTrace(err)
152+
}
146153

147-
return s, xerrors.WithStackTrace(err)
154+
return s, nil
148155
}
149156

150157
func (c *Client) isClosed() bool {

0 commit comments

Comments
 (0)