From 7d2b5ac34cdc130aeac5ab9ed39ed2582b69165b Mon Sep 17 00:00:00 2001 From: Roman Golov Date: Fri, 25 Oct 2024 18:42:52 +0300 Subject: [PATCH 1/6] USe common balancer in child drivers --- CHANGELOG.md | 2 ++ driver.go | 5 +++-- options.go | 9 +++++++++ with.go | 4 +--- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87971af03..8f71dab72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* Use common balancer for child drivers + ## v3.89.0 * Fixed send optional arguments to the server with `ydb.ParamsBuilder` diff --git a/driver.go b/driver.go index 49920cf2a..e6ed1c573 100644 --- a/driver.go +++ b/driver.go @@ -415,8 +415,9 @@ func (d *Driver) connect(ctx context.Context) (err error) { if d.pool == nil { d.pool = conn.NewPool(ctx, d.config) } - - d.balancer, err = balancer.New(ctx, d.config, d.pool, d.discoveryOptions...) + if d.balancer == nil { + d.balancer, err = balancer.New(ctx, d.config, d.pool, d.discoveryOptions...) + } if err != nil { return xerrors.WithStackTrace(err) } diff --git a/options.go b/options.go index b2983d366..565a673f8 100644 --- a/options.go +++ b/options.go @@ -11,6 +11,7 @@ import ( "github.com/ydb-platform/ydb-go-sdk/v3/config" "github.com/ydb-platform/ydb-go-sdk/v3/credentials" + "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer" balancerConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/config" "github.com/ydb-platform/ydb-go-sdk/v3/internal/certificates" "github.com/ydb-platform/ydb-go-sdk/v3/internal/conn" @@ -804,3 +805,11 @@ func withConnPool(pool *conn.Pool) Option { return pool.Take(ctx) } } + +func withSharedBalancer(balancer *balancer.Balancer) Option { + return func(ctx context.Context, c *Driver) error { + c.balancer = balancer + + return nil + } +} diff --git a/with.go b/with.go index b2d4c1776..e3711a289 100644 --- a/with.go +++ b/with.go @@ -19,9 +19,6 @@ func (d *Driver) with(ctx context.Context, opts ...Option) (*Driver, uint64, err append( append( d.opts, - WithBalancer( - d.config.Balancer(), - ), withOnClose(func(child *Driver) { d.childrenMtx.Lock() defer d.childrenMtx.Unlock() @@ -29,6 +26,7 @@ func (d *Driver) with(ctx context.Context, opts ...Option) (*Driver, uint64, err delete(d.children, id) }), withConnPool(d.pool), + withSharedBalancer(d.balancer), ), opts..., )..., From ae32ddb63e1fe73d7022b8df675de0fbee1b41ab Mon Sep 17 00:00:00 2001 From: Aleksey Myasnikov Date: Fri, 25 Oct 2024 18:48:07 +0300 Subject: [PATCH 2/6] Apply suggestions from code review --- options.go | 4 ++-- with.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/options.go b/options.go index 565a673f8..6ef14bade 100644 --- a/options.go +++ b/options.go @@ -806,9 +806,9 @@ func withConnPool(pool *conn.Pool) Option { } } -func withSharedBalancer(balancer *balancer.Balancer) Option { +func WithSharedBalancer(parent *Driver) Option { return func(ctx context.Context, c *Driver) error { - c.balancer = balancer + c.balancer = parent.balancer return nil } diff --git a/with.go b/with.go index e3711a289..ec70e8713 100644 --- a/with.go +++ b/with.go @@ -26,7 +26,7 @@ func (d *Driver) with(ctx context.Context, opts ...Option) (*Driver, uint64, err delete(d.children, id) }), withConnPool(d.pool), - withSharedBalancer(d.balancer), + WithSharedBalancer(d), ), opts..., )..., From da0f282565d241182c9a572aba702fa44c7c9719 Mon Sep 17 00:00:00 2001 From: Aleksey Myasnikov Date: Fri, 25 Oct 2024 18:49:09 +0300 Subject: [PATCH 3/6] Apply suggestions from code review --- CHANGELOG.md | 2 +- with.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f71dab72..574e398e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -* Use common balancer for child drivers +* Added option `ydb.WithSharedBalancer(*Driver)` for child drivers ## v3.89.0 * Fixed send optional arguments to the server with `ydb.ParamsBuilder` diff --git a/with.go b/with.go index ec70e8713..9060fa976 100644 --- a/with.go +++ b/with.go @@ -26,7 +26,6 @@ func (d *Driver) with(ctx context.Context, opts ...Option) (*Driver, uint64, err delete(d.children, id) }), withConnPool(d.pool), - WithSharedBalancer(d), ), opts..., )..., From b895142e67085c93efeb4dcdbf48b8a7fcc934fe Mon Sep 17 00:00:00 2001 From: Roman Golov Date: Fri, 25 Oct 2024 18:55:54 +0300 Subject: [PATCH 4/6] rollback code --- with.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/with.go b/with.go index 9060fa976..b2d4c1776 100644 --- a/with.go +++ b/with.go @@ -19,6 +19,9 @@ func (d *Driver) with(ctx context.Context, opts ...Option) (*Driver, uint64, err append( append( d.opts, + WithBalancer( + d.config.Balancer(), + ), withOnClose(func(child *Driver) { d.childrenMtx.Lock() defer d.childrenMtx.Unlock() From 49287c98cb697af4ab97c096ef2674f63aba4281 Mon Sep 17 00:00:00 2001 From: Roman Golov Date: Fri, 25 Oct 2024 18:56:12 +0300 Subject: [PATCH 5/6] Move WithSharedBalancer option upper --- options.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/options.go b/options.go index 6ef14bade..7dfdfc3dd 100644 --- a/options.go +++ b/options.go @@ -11,7 +11,6 @@ import ( "github.com/ydb-platform/ydb-go-sdk/v3/config" "github.com/ydb-platform/ydb-go-sdk/v3/credentials" - "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer" balancerConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/config" "github.com/ydb-platform/ydb-go-sdk/v3/internal/certificates" "github.com/ydb-platform/ydb-go-sdk/v3/internal/conn" @@ -600,6 +599,15 @@ func WithPanicCallback(panicCallback func(e interface{})) Option { } } +// WithSharedBalancer sets balancer from parent driver to child driver +func WithSharedBalancer(parent *Driver) Option { + return func(ctx context.Context, c *Driver) error { + c.balancer = parent.balancer + + return nil + } +} + // WithTraceTable appends trace.Table into table traces func WithTraceTable(t trace.Table, opts ...trace.TableComposeOption) Option { //nolint:gocritic return func(ctx context.Context, c *Driver) error { @@ -805,11 +813,3 @@ func withConnPool(pool *conn.Pool) Option { return pool.Take(ctx) } } - -func WithSharedBalancer(parent *Driver) Option { - return func(ctx context.Context, c *Driver) error { - c.balancer = parent.balancer - - return nil - } -} From 8b810e41edf588ffbcfe3ddebeaeb8c61856443e Mon Sep 17 00:00:00 2001 From: Roman Golov Date: Fri, 25 Oct 2024 19:00:04 +0300 Subject: [PATCH 6/6] fix error check --- driver.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/driver.go b/driver.go index e6ed1c573..b168f87ae 100644 --- a/driver.go +++ b/driver.go @@ -417,9 +417,9 @@ func (d *Driver) connect(ctx context.Context) (err error) { } if d.balancer == nil { d.balancer, err = balancer.New(ctx, d.config, d.pool, d.discoveryOptions...) - } - if err != nil { - return xerrors.WithStackTrace(err) + if err != nil { + return xerrors.WithStackTrace(err) + } } d.table = xsync.OnceValue(func() (*internalTable.Client, error) {