Skip to content

Commit 710f840

Browse files
authored
Merge pull request #149 from ydb-platform/with
## 3.13.0
2 parents d2a873e + 5450d43 commit 710f840

28 files changed

+416
-852
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 3.13.0
2+
* Refactored `Connection` interface
3+
* Removed `CustomOption` and taking client with custom options
4+
* Removed `proxy` package
5+
* Improved `db.With()` helper for child connections creation
6+
* Set shared `conn.Pool` for all children `ydb.Connection`
7+
* Fixed bug with `RoundRobin` and `RandomChoice` balancers `Create()`
8+
19
## 3.12.1
210
* Added `trace.Driver.OnConnPark` event
311
* Added `trace.Driver.OnConnClose` event

connection.go

Lines changed: 79 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
2020
"github.com/ydb-platform/ydb-go-sdk/v3/internal/lazy"
2121
"github.com/ydb-platform/ydb-go-sdk/v3/internal/logger"
22-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/proxy"
2322
"github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter"
2423
ratelimiterConfig "github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter/config"
2524
"github.com/ydb-platform/ydb-go-sdk/v3/scheme"
@@ -38,42 +37,32 @@ type Connection interface {
3837
db.ConnectionInfo
3938
grpc.ClientConnInterface
4039

41-
// Table returns table client with options from Connection instance.
42-
// Options provide options replacement for requested table client
43-
// such as database and access token
44-
Table(opts ...CustomOption) table.Client
40+
// Table returns table client
41+
Table() table.Client
4542

46-
// Scheme returns scheme client with options from Connection instance.
47-
// Options provide options replacement for requested scheme client
48-
// such as database and access token
49-
Scheme(opts ...CustomOption) scheme.Client
43+
// Scheme returns scheme client
44+
Scheme() scheme.Client
5045

51-
// Coordination returns coordination client with options from Connection instance.
52-
// Options provide options replacement for requested coordination client
53-
// such as database and access token
54-
Coordination(opts ...CustomOption) coordination.Client
46+
// Coordination returns coordination client
47+
Coordination() coordination.Client
5548

56-
// Ratelimiter returns rate limiter client with options from Connection instance.
57-
// Options provide options replacement for requested rate limiter client
58-
// such as database and access token
59-
Ratelimiter(opts ...CustomOption) ratelimiter.Client
49+
// Ratelimiter returns rate limiter client
50+
Ratelimiter() ratelimiter.Client
6051

61-
// Discovery returns discovery client with options from Connection instance.
62-
// Options provide options replacement for requested discovery client
63-
// such as database and access token
64-
Discovery(opts ...CustomOption) discovery.Client
52+
// Discovery returns discovery client
53+
Discovery() discovery.Client
6554

66-
// Scripting returns scripting client with options from Connection instance.
67-
// Options provide options replacement for requested scripting client
68-
// such as database and access token
69-
Scripting(opts ...CustomOption) scripting.Client
55+
// Scripting returns scripting client
56+
Scripting() scripting.Client
7057

7158
// With returns Connection specified with custom options
7259
// Options provide options replacement for all clients taked from new Connection
73-
With(opts ...CustomOption) Connection
60+
With(ctx context.Context, opts ...Option) (Connection, error)
7461
}
7562

7663
type connection struct {
64+
opts []Option
65+
7766
config config.Config
7867
options []config.Option
7968

@@ -94,39 +83,75 @@ type connection struct {
9483
ratelimiter ratelimiter.Client
9584
ratelimiterOptions []ratelimiterConfig.Option
9685

86+
pool conn.Pool
87+
9788
mtx sync.Mutex
9889
db db.Connection
99-
}
10090

101-
func (c *connection) With(opts ...CustomOption) Connection {
102-
return newProxy(c, newMeta(c.config.Meta(), opts...))
91+
children map[uint64]Connection
92+
childrenMtx sync.Mutex
93+
onClose []func(c *connection)
10394
}
10495

10596
func (c *connection) Close(ctx context.Context) error {
10697
c.mtx.Lock()
10798
defer c.mtx.Unlock()
108-
var issues []error
99+
100+
defer func() {
101+
for _, f := range c.onClose {
102+
f(c)
103+
}
104+
}()
105+
106+
var (
107+
issues []error
108+
children = make([]Connection, 0, len(c.children))
109+
)
110+
111+
c.childrenMtx.Lock()
112+
for _, child := range c.children {
113+
children = append(children, child)
114+
}
115+
c.childrenMtx.Unlock()
116+
117+
for _, child := range children {
118+
if err := child.Close(ctx); err != nil {
119+
issues = append(issues, err)
120+
}
121+
}
122+
109123
if err := c.ratelimiter.Close(ctx); err != nil {
110124
issues = append(issues, err)
111125
}
126+
112127
if err := c.coordination.Close(ctx); err != nil {
113128
issues = append(issues, err)
114129
}
130+
115131
if err := c.scheme.Close(ctx); err != nil {
116132
issues = append(issues, err)
117133
}
134+
118135
if err := c.table.Close(ctx); err != nil {
119136
issues = append(issues, err)
120137
}
138+
121139
if err := c.scripting.Close(ctx); err != nil {
122140
issues = append(issues, err)
123141
}
142+
124143
if err := c.db.Close(ctx); err != nil {
125144
issues = append(issues, err)
126145
}
146+
147+
if err := c.pool.Release(ctx); err != nil {
148+
issues = append(issues, err)
149+
}
150+
127151
if len(issues) > 0 {
128152
return errors.NewWithIssues("close failed", issues...)
129153
}
154+
130155
return nil
131156
}
132157

@@ -172,51 +197,36 @@ func (c *connection) Secure() bool {
172197
return c.config.Secure()
173198
}
174199

175-
func (c *connection) Table(opts ...CustomOption) table.Client {
176-
if len(opts) == 0 {
177-
return c.table
178-
}
179-
return proxy.Table(c.table, newMeta(c.config.Meta(), opts...))
200+
func (c *connection) Table() table.Client {
201+
return c.table
180202
}
181203

182-
func (c *connection) Scheme(opts ...CustomOption) scheme.Client {
183-
if len(opts) == 0 {
184-
return c.scheme
185-
}
186-
return proxy.Scheme(c.scheme, newMeta(c.config.Meta(), opts...))
204+
func (c *connection) Scheme() scheme.Client {
205+
return c.scheme
187206
}
188207

189-
func (c *connection) Coordination(opts ...CustomOption) coordination.Client {
190-
if len(opts) == 0 {
191-
return c.coordination
192-
}
193-
return proxy.Coordination(c.coordination, newMeta(c.config.Meta(), opts...))
208+
func (c *connection) Coordination() coordination.Client {
209+
return c.coordination
194210
}
195211

196-
func (c *connection) Ratelimiter(opts ...CustomOption) ratelimiter.Client {
197-
if len(opts) == 0 {
198-
return c.ratelimiter
199-
}
200-
return proxy.Ratelimiter(c.ratelimiter, newMeta(c.config.Meta(), opts...))
212+
func (c *connection) Ratelimiter() ratelimiter.Client {
213+
return c.ratelimiter
201214
}
202215

203-
func (c *connection) Discovery(opts ...CustomOption) discovery.Client {
204-
if len(opts) == 0 {
205-
return c.db.Discovery()
206-
}
207-
return proxy.Discovery(c.db.Discovery(), newMeta(c.config.Meta(), opts...))
216+
func (c *connection) Discovery() discovery.Client {
217+
return c.db.Discovery()
208218
}
209219

210-
func (c *connection) Scripting(opts ...CustomOption) scripting.Client {
211-
if len(opts) == 0 {
212-
return c.scripting
213-
}
214-
return proxy.Scripting(c.scripting, newMeta(c.config.Meta(), opts...))
220+
func (c *connection) Scripting() scripting.Client {
221+
return c.scripting
215222
}
216223

217224
// New connects to name and return name runtime holder
218225
func New(ctx context.Context, opts ...Option) (_ Connection, err error) {
219-
c := &connection{}
226+
c := &connection{
227+
opts: opts,
228+
children: make(map[uint64]Connection),
229+
}
220230
if caFile, has := os.LookupEnv("YDB_SSL_ROOT_CERTIFICATES_FILE"); has {
221231
opts = append([]Option{WithCertificatesFromFile(caFile)}, opts...)
222232
}
@@ -255,9 +265,17 @@ func New(ctx context.Context, opts ...Option) (_ Connection, err error) {
255265
)
256266
}
257267

268+
if c.pool == nil {
269+
c.pool = conn.NewPool(
270+
ctx,
271+
c.config,
272+
)
273+
}
274+
258275
c.db, err = db.New(
259276
ctx,
260277
c.config,
278+
c.pool,
261279
append(
262280
// prepend config params from root config
263281
[]discoveryConfig.Option{

custom_options.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

internal/balancer/rr/rr.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@ type roundRobin struct {
2828
}
2929

3030
func (r *roundRobin) Create() balancer.Balancer {
31-
return &roundRobin{
32-
min: r.min,
33-
max: r.max,
34-
belt: r.belt,
35-
next: r.next,
36-
conns: r.conns,
37-
r: rand.New(),
38-
}
31+
return RoundRobin()
3932
}
4033

4134
func RoundRobin() balancer.Balancer {
@@ -58,16 +51,7 @@ type randomChoice struct {
5851
}
5952

6053
func (r *randomChoice) Create() balancer.Balancer {
61-
return &randomChoice{
62-
roundRobin: roundRobin{
63-
min: r.roundRobin.min,
64-
max: r.roundRobin.max,
65-
belt: r.roundRobin.belt,
66-
next: r.roundRobin.next,
67-
conns: r.roundRobin.conns,
68-
r: rand.New(),
69-
},
70-
}
54+
return RandomChoice()
7155
}
7256

7357
func (r *roundRobin) Next() conn.Conn {

internal/balancer/single/single.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type single struct {
1515
}
1616

1717
func (b *single) Create() balancer.Balancer {
18-
return &single{conn: b.conn}
18+
return &single{}
1919
}
2020

2121
func (b *single) Next() conn.Conn {

0 commit comments

Comments
 (0)