Skip to content

Commit 18c4b4b

Browse files
committed
WIP
1 parent 14d3ded commit 18c4b4b

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

driver.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"google.golang.org/grpc"
78
"os"
89
"sync"
910
"sync/atomic"
1011

11-
"google.golang.org/grpc"
12-
1312
"github.com/ydb-platform/ydb-go-sdk/v3/config"
1413
"github.com/ydb-platform/ydb-go-sdk/v3/coordination"
1514
"github.com/ydb-platform/ydb-go-sdk/v3/discovery"
@@ -103,7 +102,7 @@ type (
103102

104103
children map[uint64]*Driver
105104
childrenMtx xsync.Mutex
106-
onClose []func(c *Driver)
105+
onClose xsync.Map[uint64, func(c *Driver)]
107106
closed atomic.Bool
108107

109108
panicCallback func(e interface{})
@@ -164,9 +163,11 @@ func (d *Driver) Close(ctx context.Context) (finalErr error) {
164163
d.ctxCancel()
165164

166165
defer func() {
167-
for _, f := range d.onClose {
166+
d.onClose.Range(func(_ uint64, f func(c *Driver)) bool {
168167
f(d)
169-
}
168+
169+
return true
170+
})
170171
}()
171172

172173
closes := make([]func(context.Context) error, 0)

internal/xsql/connector.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
type (
3636
Engine uint8
3737
Connector struct {
38+
ID uint64
3839
parent ydbDriver
3940
balancer grpc.ClientConnInterface
4041

options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ func WithTraceDatabaseSQL(t trace.DatabaseSQL, opts ...trace.DatabaseSQLComposeO
832832

833833
func withOnClose(onClose func(c *Driver)) Option {
834834
return func(ctx context.Context, d *Driver) error {
835-
d.onClose = append(d.onClose, onClose)
835+
d.onClose.Append(onClose)
836836

837837
return nil
838838
}

sql.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql"
66
"database/sql/driver"
77
"fmt"
8+
"sync/atomic"
89

910
"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
1011
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
@@ -52,17 +53,25 @@ func (d *sqlDriver) OpenConnector(dataSourceName string) (driver.Connector, erro
5253
return nil, xerrors.WithStackTrace(fmt.Errorf("failed to connect by data source name '%s': %w", dataSourceName, err))
5354
}
5455

55-
c, err := connector(db, db.databaseSQLOptions...)
56+
c, err := Connector(db, db.databaseSQLOptions...)
5657
if err != nil {
5758
return nil, xerrors.WithStackTrace(fmt.Errorf("failed to create connector: %w", err))
5859
}
5960

60-
d.attach(c, db)
61-
6261
return c, nil
6362
}
6463

64+
var globalConnectorCounter atomic.Uint64
65+
6566
func (d *sqlDriver) attach(c *xsql.Connector, parent *Driver) {
67+
c.ID = globalConnectorCounter.Add(1)
68+
69+
if parent != nil {
70+
parent.onClose.Append(func(_ *Driver) {
71+
_ = c.Close()
72+
})
73+
}
74+
6675
d.connectors.Set(c, parent)
6776
}
6877

@@ -239,7 +248,7 @@ type SQLConnector interface {
239248
Close() error
240249
}
241250

242-
func connector(parent *Driver, opts ...ConnectorOption) (*xsql.Connector, error) {
251+
func Connector(parent *Driver, opts ...ConnectorOption) (SQLConnector, error) {
243252
c, err := xsql.Open(parent, parent.metaBalancer,
244253
append(
245254
append(
@@ -255,16 +264,7 @@ func connector(parent *Driver, opts ...ConnectorOption) (*xsql.Connector, error)
255264
return nil, xerrors.WithStackTrace(err)
256265
}
257266

258-
return c, nil
259-
}
260-
261-
func Connector(parent *Driver, opts ...ConnectorOption) (SQLConnector, error) {
262-
c, err := connector(parent, opts...)
263-
if err != nil {
264-
return nil, xerrors.WithStackTrace(err)
265-
}
266-
267-
d.attach(c, nil)
267+
d.attach(c, parent)
268268

269269
return c, nil
270270
}

0 commit comments

Comments
 (0)