@@ -48,19 +48,26 @@ type Conn interface {
4848 Unban (ctx context.Context ) State
4949}
5050
51- type conn struct {
52- mtx sync.RWMutex
53- config Config // ro access
54- grpcConn * grpc.ClientConn
55- done chan struct {}
56- endpoint endpoint.Endpoint // ro access
57- closed bool
58- state atomic.Uint32
59- childStreams * xcontext.CancelsGuard
60- lastUsage xsync.LastUsage
61- onClose []func (* conn )
62- onTransportErrors []func (ctx context.Context , cc Conn , cause error )
63- }
51+ type (
52+ connConfig interface {
53+ Trace () * trace.Driver
54+ DialTimeout () time.Duration
55+ GrpcDialOptions () []grpc.DialOption
56+ }
57+ conn struct {
58+ mtx sync.RWMutex
59+ config connConfig // ro access
60+ grpcConn * grpc.ClientConn
61+ done chan struct {}
62+ endpoint endpoint.Endpoint // ro access
63+ closed bool
64+ state atomic.Uint32
65+ childStreams * xcontext.CancelsGuard
66+ lastUsage xsync.LastUsage
67+ onClose []func (* conn )
68+ onTransportErrors []func (ctx context.Context , cc Conn , cause error )
69+ }
70+ )
6471
6572func (c * conn ) Address () string {
6673 return c .endpoint .Address ()
@@ -177,18 +184,6 @@ func (c *conn) GetState() (s State) {
177184 return State (c .state .Load ())
178185}
179186
180- func makeDialOption (overrideHost string ) []grpc.DialOption {
181- dialOption := []grpc.DialOption {
182- grpc .WithStatsHandler (statsHandler {}),
183- }
184-
185- if len (overrideHost ) != 0 {
186- dialOption = append (dialOption , grpc .WithAuthority (overrideHost ))
187- }
188-
189- return dialOption
190- }
191-
192187func (c * conn ) realConn (ctx context.Context ) (cc * grpc.ClientConn , err error ) {
193188 if c .isClosed () {
194189 return nil , xerrors .WithStackTrace (errClosedConnection )
@@ -201,31 +196,37 @@ func (c *conn) realConn(ctx context.Context) (cc *grpc.ClientConn, err error) {
201196 return c .grpcConn , nil
202197 }
203198
204- if dialTimeout := c .config .DialTimeout (); dialTimeout > 0 {
205- var cancel context.CancelFunc
206- ctx , cancel = xcontext .WithTimeout (ctx , dialTimeout )
207- defer cancel ()
208- }
199+ return c .dial (ctx )
200+ }
209201
202+ // c.mtx must be locked
203+ func (c * conn ) dial (ctx context.Context ) (cc * grpc.ClientConn , err error ) {
210204 onDone := trace .DriverOnConnDial (
211205 c .config .Trace (), & ctx ,
212- stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/conn.(*conn).realConn " ),
206+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/conn.(*conn).dial " ),
213207 c .endpoint .Copy (),
214208 )
215209 defer func () {
216210 onDone (err )
217211 }()
218212
219- // prepend "ydb" scheme for grpc dns-resolver to find the proper scheme
220- // three slashes in "ydb:///" is ok. It needs for good parse scheme in grpc resolver.
221- address := "ydb:///" + c .endpoint .Address ()
213+ if dialTimeout := c .config .DialTimeout (); dialTimeout > 0 {
214+ var cancel context.CancelFunc
215+ ctx , cancel = xcontext .WithTimeout (ctx , dialTimeout )
216+ defer cancel ()
217+ }
218+
219+ address := c .endpoint .Address ()
222220
223- dialOption := makeDialOption ( c . endpoint . OverrideHost () )
221+ dialOpts := c . config . GrpcDialOptions ( )
224222
225- cc , err = grpc .DialContext (ctx , address , append ( //nolint:staticcheck,nolintlint
226- dialOption ,
227- c .config .GrpcDialOptions ()... ,
228- )... )
223+ dialOpts = append (dialOpts , grpc .WithStatsHandler (statsHandler {}))
224+
225+ if overrideHost := c .endpoint .OverrideHost (); overrideHost != "" {
226+ dialOpts = append (dialOpts , grpc .WithAuthority (overrideHost ))
227+ }
228+
229+ cc , err = grpc .DialContext (ctx , address , dialOpts ... )
229230 if err != nil {
230231 if xerrors .IsContextError (err ) {
231232 return nil , xerrors .WithStackTrace (err )
@@ -238,7 +239,9 @@ func (c *conn) realConn(ctx context.Context) (cc *grpc.ClientConn, err error) {
238239 return nil , xerrors .WithStackTrace (
239240 xerrors .Retryable (
240241 xerrors .Transport (err ),
241- xerrors .WithName ("realConn" ),
242+ xerrors .WithName (
243+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/conn.(*conn).dial" ).FunctionID (),
244+ ),
242245 ),
243246 )
244247 }
@@ -581,7 +584,7 @@ func withOnTransportError(onTransportError func(ctx context.Context, cc Conn, ca
581584 }
582585}
583586
584- func newConn (e endpoint.Endpoint , config Config , opts ... option ) * conn {
587+ func newConn (e endpoint.Endpoint , config connConfig , opts ... option ) * conn {
585588 c := & conn {
586589 endpoint : e ,
587590 config : config ,
@@ -604,10 +607,6 @@ func newConn(e endpoint.Endpoint, config Config, opts ...option) *conn {
604607 return c
605608}
606609
607- func New (e endpoint.Endpoint , config Config , opts ... option ) Conn {
608- return newConn (e , config , opts ... )
609- }
610-
611610var _ stats.Handler = statsHandler {}
612611
613612type statsHandler struct {}
0 commit comments