@@ -129,7 +129,7 @@ func parseOptions(opts ...crudOption) *crudOptionsHolder {
129129
130130type Getter interface {
131131 // Get gets conn from cluster
132- Get (ctx context.Context , opts ... crudOption ) (cc conn.Conn , err error )
132+ Get (ctx context.Context ) (cc conn.Conn , err error )
133133}
134134
135135type Inserter interface {
@@ -247,17 +247,11 @@ func (c *cluster) Close(ctx context.Context) (err error) {
247247
248248// Get returns next available connection.
249249// It returns error on given deadline cancellation or when cluster become closed.
250- func (c * cluster ) Get (ctx context.Context , opts ... crudOption ) (cc conn.Conn , err error ) {
250+ func (c * cluster ) Get (ctx context.Context ) (cc conn.Conn , err error ) {
251251 var cancel context.CancelFunc
252252 ctx , cancel = context .WithTimeout (ctx , MaxGetConnTimeout )
253253 defer cancel ()
254254
255- options := parseOptions (opts ... )
256- if options .withLock {
257- c .mu .Lock ()
258- defer c .mu .Unlock ()
259- }
260-
261255 if c .closed {
262256 return nil , errors .WithStackTrace (ErrClusterClosed )
263257 }
@@ -272,7 +266,9 @@ func (c *cluster) Get(ctx context.Context, opts ...crudOption) (cc conn.Conn, er
272266 }()
273267
274268 if e , ok := ContextEndpoint (ctx ); ok {
269+ c .mu .RLock ()
275270 cc , ok = c .endpoints [e .NodeID ()]
271+ c .mu .RUnlock ()
276272 if ok && cc .IsState (
277273 conn .Created ,
278274 conn .Online ,
@@ -282,15 +278,22 @@ func (c *cluster) Get(ctx context.Context, opts ...crudOption) (cc conn.Conn, er
282278 }
283279 }
284280
285- c .balancerMtx .RLock ()
286- defer c .balancerMtx .RUnlock ()
287-
288- cc = c .config .Balancer ().Next ()
289- if cc == nil {
290- return nil , errors .WithStackTrace (ErrClusterEmpty )
281+ for {
282+ select {
283+ case <- ctx .Done ():
284+ return nil , errors .WithStackTrace (ctx .Err ())
285+ default :
286+ c .balancerMtx .RLock ()
287+ cc = c .config .Balancer ().Next ()
288+ c .balancerMtx .RUnlock ()
289+ if cc == nil {
290+ return nil , errors .WithStackTrace (ErrClusterEmpty )
291+ }
292+ if err = cc .Ping (ctx ); err == nil {
293+ return cc , nil
294+ }
295+ }
291296 }
292-
293- return cc , nil
294297}
295298
296299// Insert inserts new connection into the cluster.
0 commit comments