@@ -17,6 +17,7 @@ import (
1717 "github.com/ydb-platform/ydb-go-sdk/v3/internal/repeater"
1818 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1919 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
20+ "github.com/ydb-platform/ydb-go-sdk/v3/retry"
2021 "github.com/ydb-platform/ydb-go-sdk/v3/trace"
2122)
2223
@@ -146,6 +147,12 @@ func New(
146147 pool * conn.Pool ,
147148 opts ... discoveryConfig.Option ,
148149) (b * Balancer , err error ) {
150+ if t := c .DialTimeout (); t > 0 {
151+ var cancel context.CancelFunc
152+ ctx , cancel = context .WithTimeout (ctx , t )
153+ defer cancel ()
154+ }
155+
149156 onDone := trace .DriverOnBalancerInit (
150157 c .Trace (),
151158 & ctx ,
@@ -181,9 +188,17 @@ func New(
181188 endpointsToConnections (pool , []endpoint.Endpoint {discoveryEndpoint }),
182189 nil , balancerConfig.Info {}, false )
183190 } else {
184- if err = b .clusterDiscovery (ctx ); err != nil {
191+ // initialization of balancer state
192+ if err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
193+ if err = b .clusterDiscovery (ctx ); err != nil {
194+ return xerrors .WithStackTrace (err )
195+ }
196+ return nil
197+ }, retry .WithIdempotent (true )); err != nil {
185198 return nil , xerrors .WithStackTrace (err )
186199 }
200+
201+ // run background discovering
187202 if d := discoveryConfig .Interval (); d > 0 {
188203 b .discoveryRepeater = repeater .New (d , func (ctx context.Context ) (err error ) {
189204 ctx , cancel := context .WithTimeout (ctx , d )
@@ -197,14 +212,6 @@ func New(
197212 }
198213 }
199214
200- var cancel context.CancelFunc
201- if t := c .DialTimeout (); t > 0 {
202- ctx , cancel = context .WithTimeout (ctx , c .DialTimeout ())
203- } else {
204- ctx , cancel = context .WithCancel (ctx )
205- }
206- defer cancel ()
207-
208215 return b , nil
209216}
210217
0 commit comments