Skip to content

Commit caf6f23

Browse files
committed
fixed context usage for closing of sessions
1 parent 7c73ec5 commit caf6f23

File tree

2 files changed

+52
-43
lines changed

2 files changed

+52
-43
lines changed

internal/table/retry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ func TestDoBadSession(t *testing.T) {
8888
p := pool.New[*Session, Session](ctx,
8989
pool.WithCreateItemFunc[*Session, Session](func(ctx context.Context) (*Session, error) {
9090
s := simpleSession(t)
91-
s.onClose = append(s.onClose, func(s *Session) error {
91+
s.closeOnce = func(_ context.Context) error {
9292
closed[s] = true
9393

9494
return nil
95-
})
95+
}
9696

9797
return s, nil
9898
}),

internal/table/session.go

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
3636
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
3737
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
38+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
3839
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
3940
"github.com/ydb-platform/ydb-go-sdk/v3/table"
4041
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
@@ -248,7 +249,7 @@ var (
248249
// Note that after session is no longer needed it should be destroyed by
249250
// Close() call.
250251
type Session struct {
251-
onClose []func(s *Session) error
252+
closeOnce func(ctx context.Context) error
252253
id string
253254
client Ydb_Table_V1.TableServiceClient
254255
status table.SessionStatus
@@ -329,7 +330,7 @@ func newSession(ctx context.Context, cc grpc.ClientConnInterface, config *config
329330
return newTableSession(ctx, cc, config)
330331
}
331332

332-
func newTableSession( //nolint:funlen
333+
func newTableSession(
333334
ctx context.Context, cc grpc.ClientConnInterface, config *config.Config,
334335
) (*Session, error) {
335336
response, err := Ydb_Table_V1.NewTableServiceClient(cc).CreateSession(ctx,
@@ -355,29 +356,6 @@ func newTableSession( //nolint:funlen
355356
id: result.GetSessionId(),
356357
config: config,
357358
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-
},
381359
}
382360

383361
s.lastUsage.Store(time.Now().Unix())
@@ -391,6 +369,7 @@ func newTableSession( //nolint:funlen
391369
},
392370
),
393371
)
372+
s.closeOnce = xsync.OnceFunc(closeTableSession(s.client, s.config, s.id))
394373
s.dataQuery = tableClientExecutor{
395374
client: s.client,
396375
ignoreTruncated: s.config.IgnoreTruncated(),
@@ -399,7 +378,37 @@ func newTableSession( //nolint:funlen
399378
return s, nil
400379
}
401380

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(
403412
ctx context.Context, cc grpc.ClientConnInterface, config *config.Config,
404413
) (*Session, error) {
405414
s := &Session{
@@ -442,16 +451,7 @@ func newQuerySession( //nolint:funlen
442451
},
443452
),
444453
)
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))
455455
if config.ExecuteDataQueryOverQueryService() {
456456
s.dataQuery = queryClientExecutor{
457457
core: core,
@@ -467,6 +467,17 @@ func newQuerySession( //nolint:funlen
467467
return s, nil
468468
}
469469

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+
470481
func (s *Session) ID() string {
471482
if s == nil {
472483
return ""
@@ -485,11 +496,9 @@ func (s *Session) Close(ctx context.Context) (finalErr error) {
485496
s.SetStatus(table.SessionClosed)
486497
}()
487498

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)
493502
}
494503

495504
return nil

0 commit comments

Comments
 (0)