@@ -35,6 +35,7 @@ import (
35
35
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
36
36
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
37
37
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
38
+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
38
39
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
39
40
"github.com/ydb-platform/ydb-go-sdk/v3/table"
40
41
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
@@ -248,7 +249,7 @@ var (
248
249
// Note that after session is no longer needed it should be destroyed by
249
250
// Close() call.
250
251
type Session struct {
251
- onClose [] func (s * Session ) error
252
+ closeOnce func (ctx context. Context ) error
252
253
id string
253
254
client Ydb_Table_V1.TableServiceClient
254
255
status table.SessionStatus
@@ -329,7 +330,7 @@ func newSession(ctx context.Context, cc grpc.ClientConnInterface, config *config
329
330
return newTableSession (ctx , cc , config )
330
331
}
331
332
332
- func newTableSession ( //nolint:funlen
333
+ func newTableSession (
333
334
ctx context.Context , cc grpc.ClientConnInterface , config * config.Config ,
334
335
) (* Session , error ) {
335
336
response , err := Ydb_Table_V1 .NewTableServiceClient (cc ).CreateSession (ctx ,
@@ -355,29 +356,6 @@ func newTableSession( //nolint:funlen
355
356
id : result .GetSessionId (),
356
357
config : config ,
357
358
status : table .SessionReady ,
358
- onClose : []func (s * Session ) error {
359
- func (s * Session ) error {
360
- if err := ctx .Err (); err != nil {
361
- return xerrors .WithStackTrace (err )
362
- }
363
-
364
- _ , err = s .client .DeleteSession (ctx ,
365
- & Ydb_Table.DeleteSessionRequest {
366
- SessionId : s .id ,
367
- OperationParams : operation .Params (ctx ,
368
- s .config .OperationTimeout (),
369
- s .config .OperationCancelAfter (),
370
- operation .ModeSync ,
371
- ),
372
- },
373
- )
374
- if err != nil {
375
- return xerrors .WithStackTrace (err )
376
- }
377
-
378
- return nil
379
- },
380
- },
381
359
}
382
360
383
361
s .lastUsage .Store (time .Now ().Unix ())
@@ -391,6 +369,7 @@ func newTableSession( //nolint:funlen
391
369
},
392
370
),
393
371
)
372
+ s .closeOnce = xsync .OnceFunc (closeTableSession (s .client , s .config , s .id ))
394
373
s .dataQuery = tableClientExecutor {
395
374
client : s .client ,
396
375
ignoreTruncated : s .config .IgnoreTruncated (),
@@ -399,7 +378,37 @@ func newTableSession( //nolint:funlen
399
378
return s , nil
400
379
}
401
380
402
- func newQuerySession ( //nolint:funlen
381
+ func closeTableSession (c Ydb_Table_V1.TableServiceClient , cfg * config.Config , id string ) func (context.Context ) error {
382
+ return func (ctx context.Context ) error {
383
+ if err := ctx .Err (); err != nil {
384
+ return xerrors .WithStackTrace (err )
385
+ }
386
+
387
+ if t := cfg .DeleteTimeout (); t > 0 {
388
+ var cancel context.CancelFunc
389
+ ctx , cancel = xcontext .WithTimeout (ctx , t )
390
+ defer cancel ()
391
+ }
392
+
393
+ _ , err := c .DeleteSession (ctx ,
394
+ & Ydb_Table.DeleteSessionRequest {
395
+ SessionId : id ,
396
+ OperationParams : operation .Params (ctx ,
397
+ cfg .OperationTimeout (),
398
+ cfg .OperationCancelAfter (),
399
+ operation .ModeSync ,
400
+ ),
401
+ },
402
+ )
403
+ if err != nil {
404
+ return xerrors .WithStackTrace (err )
405
+ }
406
+
407
+ return nil
408
+ }
409
+ }
410
+
411
+ func newQuerySession (
403
412
ctx context.Context , cc grpc.ClientConnInterface , config * config.Config ,
404
413
) (* Session , error ) {
405
414
s := & Session {
@@ -442,16 +451,7 @@ func newQuerySession( //nolint:funlen
442
451
},
443
452
),
444
453
)
445
- s .onClose = []func (s * Session ) error {
446
- func (s * Session ) error {
447
- err := core .Close (ctx )
448
- if err != nil {
449
- return xerrors .WithStackTrace (err )
450
- }
451
-
452
- return nil
453
- },
454
- }
454
+ s .closeOnce = xsync .OnceFunc (closeQuerySession (core ))
455
455
if config .ExecuteDataQueryOverQueryService () {
456
456
s .dataQuery = queryClientExecutor {
457
457
core : core ,
@@ -467,6 +467,17 @@ func newQuerySession( //nolint:funlen
467
467
return s , nil
468
468
}
469
469
470
+ func closeQuerySession (core query.Core ) func (context.Context ) error {
471
+ return func (ctx context.Context ) error {
472
+ err := core .Close (ctx )
473
+ if err != nil {
474
+ return xerrors .WithStackTrace (err )
475
+ }
476
+
477
+ return nil
478
+ }
479
+ }
480
+
470
481
func (s * Session ) ID () string {
471
482
if s == nil {
472
483
return ""
@@ -485,11 +496,9 @@ func (s *Session) Close(ctx context.Context) (finalErr error) {
485
496
s .SetStatus (table .SessionClosed )
486
497
}()
487
498
488
- for _ , onClose := range s .onClose {
489
- err := onClose (s )
490
- if err != nil {
491
- return xerrors .WithStackTrace (err )
492
- }
499
+ err := s .closeOnce (ctx )
500
+ if err != nil {
501
+ return xerrors .WithStackTrace (err )
493
502
}
494
503
495
504
return nil
0 commit comments