@@ -9,18 +9,23 @@ import (
99
1010 "github.com/ydb-platform/ydb-go-sdk/v3/config"
1111 "github.com/ydb-platform/ydb-go-sdk/v3/coordination"
12+ coordinationConfig "github.com/ydb-platform/ydb-go-sdk/v3/coordination/config"
1213 "github.com/ydb-platform/ydb-go-sdk/v3/discovery"
13- "github.com/ydb-platform/ydb-go-sdk/v3/internal/conn"
14+ discoveryConfig "github.com/ydb-platform/ydb-go-sdk/v3/discovery/config"
15+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/single"
16+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/closer"
1417 "github.com/ydb-platform/ydb-go-sdk/v3/internal/db"
1518 "github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
1619 "github.com/ydb-platform/ydb-go-sdk/v3/internal/lazy"
1720 "github.com/ydb-platform/ydb-go-sdk/v3/internal/logger"
1821 "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
1922 "github.com/ydb-platform/ydb-go-sdk/v3/internal/proxy"
20- "github.com/ydb-platform/ydb-go-sdk/v3/log"
2123 "github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter"
24+ ratelimiterConfig "github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter/config"
2225 "github.com/ydb-platform/ydb-go-sdk/v3/scheme"
26+ schemeConfig "github.com/ydb-platform/ydb-go-sdk/v3/scheme/config"
2327 "github.com/ydb-platform/ydb-go-sdk/v3/scripting"
28+ scriptingConfig "github.com/ydb-platform/ydb-go-sdk/v3/scripting/config"
2429 "github.com/ydb-platform/ydb-go-sdk/v3/table"
2530 tableConfig "github.com/ydb-platform/ydb-go-sdk/v3/table/config"
2631 "github.com/ydb-platform/ydb-go-sdk/v3/trace"
@@ -29,7 +34,8 @@ import (
2934// Connection interface provide access to YDB service clients
3035// Interface and list of clients may be changed in the future
3136type Connection interface {
32- db.Connection
37+ closer.Closer
38+ db.ConnectionInfo
3339
3440 // Table returns table client with options from Connection instance.
3541 // Options provide options replacement for requested table client
@@ -63,30 +69,35 @@ type Connection interface {
6369}
6470
6571type connection struct {
66- config config.Config
67- options []config.Option
68- tableOptions []tableConfig.Option
69- scripting scripting.Client
72+ config config.Config
73+ options []config.Option
7074
7175 table table.Client
72- scheme scheme.Client
73- discovery discovery.Client
74- coordination coordination.Client
75- rateLimiter ratelimiter.Client
76+ tableOptions []tableConfig.Option
77+
78+ scripting scripting.Client
79+ scriptingOptions []scriptingConfig.Option
80+
81+ scheme scheme.Client
82+ schemeOptions []schemeConfig.Option
83+
84+ discoveryOptions []discoveryConfig.Option
85+
86+ coordination coordination.Client
87+ coordinationOptions []coordinationConfig.Option
7688
77- conns conn.Pool
78- mtx sync.Mutex
79- db db.Connection
89+ ratelimiter ratelimiter.Client
90+ ratelimiterOptions []ratelimiterConfig.Option
91+
92+ mtx sync.Mutex
93+ db db.Connection
8094}
8195
8296func (c * connection ) Close (ctx context.Context ) error {
8397 c .mtx .Lock ()
8498 defer c .mtx .Unlock ()
8599 var issues []error
86- if err := c .discovery .Close (ctx ); err != nil {
87- issues = append (issues , err )
88- }
89- if err := c .rateLimiter .Close (ctx ); err != nil {
100+ if err := c .ratelimiter .Close (ctx ); err != nil {
90101 issues = append (issues , err )
91102 }
92103 if err := c .coordination .Close (ctx ); err != nil {
@@ -104,9 +115,6 @@ func (c *connection) Close(ctx context.Context) error {
104115 if err := c .db .Close (ctx ); err != nil {
105116 issues = append (issues , err )
106117 }
107- if err := c .conns .Close (ctx ); err != nil {
108- issues = append (issues , err )
109- }
110118 if len (issues ) > 0 {
111119 return errors .NewWithIssues ("close failed" , issues ... )
112120 }
@@ -167,16 +175,16 @@ func (c *connection) Coordination(opts ...CustomOption) coordination.Client {
167175
168176func (c * connection ) Ratelimiter (opts ... CustomOption ) ratelimiter.Client {
169177 if len (opts ) == 0 {
170- return c .rateLimiter
178+ return c .ratelimiter
171179 }
172- return proxy .Ratelimiter (c .rateLimiter , c .meta (opts ... ))
180+ return proxy .Ratelimiter (c .ratelimiter , c .meta (opts ... ))
173181}
174182
175183func (c * connection ) Discovery (opts ... CustomOption ) discovery.Client {
176184 if len (opts ) == 0 {
177- return c .discovery
185+ return c .db . Discovery ()
178186 }
179- return proxy .Discovery (c .discovery , c .meta (opts ... ))
187+ return proxy .Discovery (c .db . Discovery () , c .meta (opts ... ))
180188}
181189
182190func (c * connection ) Scripting (opts ... CustomOption ) scripting.Client {
@@ -205,17 +213,14 @@ func New(ctx context.Context, opts ...Option) (_ Connection, err error) {
205213 }
206214 if logLevel , has := os .LookupEnv ("YDB_LOG_SEVERITY_LEVEL" ); has {
207215 if l := logger .FromString (logLevel ); l < logger .QUIET {
208- logger := logger .New (
209- logger .WithNamespace ("ydb" ),
210- logger .WithMinLevel (logger .FromString (logLevel )),
211- logger .WithNoColor (os .Getenv ("YDB_LOG_NO_COLOR" ) != "" ),
212- )
213216 opts = append (
214- []Option {
215- WithTraceDriver (log .Driver (logger , trace .DetailsAll )),
216- WithTraceTable (log .Table (logger , trace .DetailsAll )),
217- },
218- opts ... ,
217+ opts ,
218+ WithLogger (
219+ trace .DetailsAll ,
220+ WithNamespace ("ydb" ),
221+ WithMinLevel (Level (logger .FromString (logLevel ))),
222+ WithNoColor (os .Getenv ("YDB_LOG_NO_COLOR" ) != "" ),
223+ ),
219224 )
220225 }
221226 }
@@ -226,16 +231,42 @@ func New(ctx context.Context, opts ...Option) (_ Connection, err error) {
226231 }
227232 }
228233 c .config = config .New (c .options ... )
229- c .conns = conn .NewPool (ctx , c .config )
230- c .db , err = db .New (ctx , c .config , c .conns )
234+ if c .config .Endpoint () == "" {
235+ panic ("empty dial address" )
236+ }
237+ if c .config .Database () == "" {
238+ panic ("empty database" )
239+ }
240+
241+ if single .IsSingle (c .config .Balancer ()) {
242+ c .discoveryOptions = append (c .discoveryOptions , discoveryConfig .WithInterval (0 ))
243+ }
244+
245+ c .db , err = db .New (
246+ ctx ,
247+ c .config ,
248+ append (
249+ []discoveryConfig.Option {
250+ discoveryConfig .WithEndpoint (c .Endpoint ()),
251+ discoveryConfig .WithDatabase (c .Name ()),
252+ discoveryConfig .WithSecure (c .Secure ()),
253+ },
254+ c .discoveryOptions ... ,
255+ )... ,
256+ )
231257 if err != nil {
232258 return nil , err
233259 }
260+
234261 c .table = lazy .Table (c .db , c .tableOptions )
235- c .scheme = lazy .Scheme (c .db )
236- c .scripting = lazy .Scripting (c .db )
237- c .discovery = lazy .Discovery (c .db , c .config .Trace ())
238- c .coordination = lazy .Coordination (c .db )
239- c .rateLimiter = lazy .Ratelimiter (c .db )
262+
263+ c .scheme = lazy .Scheme (c .db , c .schemeOptions )
264+
265+ c .scripting = lazy .Scripting (c .db , c .scriptingOptions )
266+
267+ c .coordination = lazy .Coordination (c .db , c .coordinationOptions )
268+
269+ c .ratelimiter = lazy .Ratelimiter (c .db , c .ratelimiterOptions )
270+
240271 return c , nil
241272}
0 commit comments