@@ -15,7 +15,6 @@ import (
15
15
"github.com/ydb-platform/ydb-go-sdk/v3/internal/empty"
16
16
"github.com/ydb-platform/ydb-go-sdk/v3/internal/topic"
17
17
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
18
- "github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
19
18
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
20
19
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
21
20
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
@@ -24,6 +23,7 @@ import (
24
23
var (
25
24
errReconnectRequestOutdated = xerrors .Wrap (errors .New ("ydb: reconnect request outdated" ))
26
25
errReconnect = xerrors .Wrap (errors .New ("ydb: reconnect to topic grpc stream" ))
26
+ errConnectionTimeout = xerrors .Wrap (errors .New ("ydb: topic reader connection timeout for stream" ))
27
27
)
28
28
29
29
type readerConnectFunc func (ctx context.Context ) (batchedStreamReader , error )
@@ -33,6 +33,7 @@ type readerReconnector struct {
33
33
clock clockwork.Clock
34
34
retrySettings topic.RetrySettings
35
35
streamVal batchedStreamReader
36
+ streamContextCancel context.CancelCauseFunc
36
37
streamErr error
37
38
closedErr error
38
39
initErr error
@@ -148,6 +149,7 @@ func (r *readerReconnector) CloseWithError(ctx context.Context, err error) error
148
149
149
150
if r .streamVal != nil {
150
151
streamCloseErr := r .streamVal .CloseWithError (ctx , xerrors .WithStackTrace (errReaderClosed ))
152
+ r .streamContextCancel (errReaderClosed )
151
153
if closeErr == nil {
152
154
closeErr = streamCloseErr
153
155
}
@@ -267,7 +269,7 @@ func (r *readerReconnector) reconnect(ctx context.Context, reason error, oldRead
267
269
_ = oldReader .CloseWithError (ctx , xerrors .WithStackTrace (errReconnect ))
268
270
}
269
271
270
- newStream , err := r .connectWithTimeout ()
272
+ newStream , newStreamClose , err := r .connectWithTimeout ()
271
273
272
274
if r .isRetriableError (err ) {
273
275
go func (reason error ) {
@@ -281,6 +283,7 @@ func (r *readerReconnector) reconnect(ctx context.Context, reason error, oldRead
281
283
r .streamErr = err
282
284
if err == nil {
283
285
r .streamVal = newStream
286
+ r .streamContextCancel = newStreamClose
284
287
if ! r .initDone {
285
288
r .initDone = true
286
289
close (r .initDoneCh )
@@ -304,14 +307,14 @@ func (r *readerReconnector) checkErrRetryMode(err error, retriesDuration time.Du
304
307
return topic .CheckRetryMode (err , r .retrySettings , retriesDuration )
305
308
}
306
309
307
- func (r * readerReconnector ) connectWithTimeout () (_ batchedStreamReader , err error ) {
310
+ func (r * readerReconnector ) connectWithTimeout () (_ batchedStreamReader , _ context. CancelCauseFunc , err error ) {
308
311
bgContext := r .background .Context ()
309
312
310
313
if err = bgContext .Err (); err != nil {
311
- return nil , err
314
+ return nil , nil , err
312
315
}
313
316
314
- connectionContext , cancel := xcontext . WithCancel (context .Background ( ))
317
+ connectionContext , cancel := context . WithCancelCause (context .WithoutCancel ( bgContext ))
315
318
316
319
type connectResult struct {
317
320
stream batchedStreamReader
@@ -332,17 +335,17 @@ func (r *readerReconnector) connectWithTimeout() (_ batchedStreamReader, err err
332
335
case <- connectionTimoutTimer .Chan ():
333
336
// cancel connection context only if timeout exceed while connection
334
337
// because if cancel context after connect - it will break
335
- cancel ()
338
+ cancel (xerrors . WithStackTrace ( errConnectionTimeout ) )
336
339
res = <- result
337
340
case res = <- result :
338
341
// pass
339
342
}
340
343
341
344
if res .err == nil {
342
- return res .stream , nil
345
+ return res .stream , cancel , nil
343
346
}
344
347
345
- return nil , res .err
348
+ return nil , nil , res .err
346
349
}
347
350
348
351
func (r * readerReconnector ) WaitInit (ctx context.Context ) error {
0 commit comments