Skip to content

Commit

Permalink
* Added trace.Retry into driver instance for usage in call `retry.R…
Browse files Browse the repository at this point in the history
…etry` func

* Refactored traces and metrics
* Renamed retry option `WithID` to `WithLabel`
  • Loading branch information
asmyasnikov committed Oct 22, 2023
1 parent fb20e38 commit da26af2
Show file tree
Hide file tree
Showing 50 changed files with 1,780 additions and 1,258 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Added `trace.Retry` into driver instance for usage in call `retry.Retry` func
* Refactored traces and metrics
* Renamed retry option `WithID` to `WithLabel`

## v3.53.4
* Downgrade `golang.org/x/net` from `0.17.0` to `0.15.0`
* Downgrade `golang.org/x/sys` from `v0.13.0` to `v0.12.0`
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ func WithTrace(t trace.Driver, opts ...trace.DriverComposeOption) Option { //nol
}
}

func WithTraceRetry(t *trace.Retry, opts ...trace.RetryComposeOption) Option {
return func(c *Config) {
config.SetTraceRetry(&c.Common, t, opts...)
}
}

func WithUserAgent(userAgent string) Option {
return func(c *Config) {
c.metaOptions = append(c.metaOptions, meta.WithUserAgentOption(userAgent))
Expand Down
171 changes: 88 additions & 83 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,34 +139,34 @@ type Driver struct { //nolint:maligned
}

// Close closes Driver and clear resources
func (c *Driver) Close(ctx context.Context) error {
c.mtx.Lock()
defer c.mtx.Unlock()
func (d *Driver) Close(ctx context.Context) error {
d.mtx.Lock()
defer d.mtx.Unlock()

defer func() {
for _, f := range c.onClose {
f(c)
for _, f := range d.onClose {
f(d)
}
}()

closers := make([]func(context.Context) error, 0)
c.childrenMtx.WithLock(func() {
for _, child := range c.children {
d.childrenMtx.WithLock(func() {
for _, child := range d.children {
closers = append(closers, child.Close)
}
c.children = nil
d.children = nil
})

closers = append(
closers,
c.ratelimiterOnce.Close,
c.coordinationOnce.Close,
c.schemeOnce.Close,
c.scriptingOnce.Close,
c.tableOnce.Close,
c.topicOnce.Close,
c.balancer.Close,
c.pool.Release,
d.ratelimiterOnce.Close,
d.coordinationOnce.Close,
d.schemeOnce.Close,
d.scriptingOnce.Close,
d.tableOnce.Close,
d.topicOnce.Close,
d.balancer.Close,
d.pool.Release,
)

var issues []error
Expand All @@ -184,167 +184,171 @@ func (c *Driver) Close(ctx context.Context) error {
}

// Endpoint returns initial endpoint
func (c *Driver) Endpoint() string {
return c.config.Endpoint()
func (d *Driver) Endpoint() string {
return d.config.Endpoint()
}

// Name returns database name
func (c *Driver) Name() string {
return c.config.Database()
func (d *Driver) Name() string {
return d.config.Database()
}

// Secure returns true if database Driver is secure
func (c *Driver) Secure() bool {
return c.config.Secure()
func (d *Driver) Secure() bool {
return d.config.Secure()
}

// Table returns table client
func (c *Driver) Table() table.Client {
c.tableOnce.Init(func() closeFunc {
c.table = internalTable.New(
c.balancer,
func (d *Driver) Table() table.Client {
d.tableOnce.Init(func() closeFunc {
d.table = internalTable.New(
d.balancer,
tableConfig.New(
append(
// prepend common params from root config
[]tableConfig.Option{
tableConfig.With(c.config.Common),
tableConfig.With(d.config.Common),
},
c.tableOptions...,
d.tableOptions...,
)...,
),
)
return c.table.Close
return d.table.Close
})
// may be nil if driver closed early
return c.table
return d.table
}

// Scheme returns scheme client
func (c *Driver) Scheme() scheme.Client {
c.schemeOnce.Init(func() closeFunc {
c.scheme = internalScheme.New(
c.balancer,
func (d *Driver) Scheme() scheme.Client {
d.schemeOnce.Init(func() closeFunc {
d.scheme = internalScheme.New(
d.balancer,
schemeConfig.New(
append(
// prepend common params from root config
[]schemeConfig.Option{
schemeConfig.WithDatabaseName(c.Name()),
schemeConfig.With(c.config.Common),
schemeConfig.WithDatabaseName(d.Name()),
schemeConfig.With(d.config.Common),
},
c.schemeOptions...,
d.schemeOptions...,
)...,
),
)
return c.scheme.Close
return d.scheme.Close
})
// may be nil if driver closed early
return c.scheme
return d.scheme
}

func (d *Driver) TraceRetry() *trace.Retry {
return d.config.TraceRetry()
}

// Coordination returns coordination client
func (c *Driver) Coordination() coordination.Client {
c.coordinationOnce.Init(func() closeFunc {
c.coordination = internalCoordination.New(
c.balancer,
func (d *Driver) Coordination() coordination.Client {
d.coordinationOnce.Init(func() closeFunc {
d.coordination = internalCoordination.New(
d.balancer,
coordinationConfig.New(
append(
// prepend common params from root config
[]coordinationConfig.Option{
coordinationConfig.With(c.config.Common),
coordinationConfig.With(d.config.Common),
},
c.coordinationOptions...,
d.coordinationOptions...,
)...,
),
)
return c.coordination.Close
return d.coordination.Close
})
// may be nil if driver closed early
return c.coordination
return d.coordination
}

// Ratelimiter returns ratelimiter client
func (c *Driver) Ratelimiter() ratelimiter.Client {
c.ratelimiterOnce.Init(func() closeFunc {
c.ratelimiter = internalRatelimiter.New(
c.balancer,
func (d *Driver) Ratelimiter() ratelimiter.Client {
d.ratelimiterOnce.Init(func() closeFunc {
d.ratelimiter = internalRatelimiter.New(
d.balancer,
ratelimiterConfig.New(
append(
// prepend common params from root config
[]ratelimiterConfig.Option{
ratelimiterConfig.With(c.config.Common),
ratelimiterConfig.With(d.config.Common),
},
c.ratelimiterOptions...,
d.ratelimiterOptions...,
)...,
),
)
return c.ratelimiter.Close
return d.ratelimiter.Close
})
// may be nil if driver closed early
return c.ratelimiter
return d.ratelimiter
}

// Discovery returns discovery client
func (c *Driver) Discovery() discovery.Client {
c.discoveryOnce.Init(func() closeFunc {
c.discovery = internalDiscovery.New(
c.pool.Get(endpoint.New(c.config.Endpoint())),
func (d *Driver) Discovery() discovery.Client {
d.discoveryOnce.Init(func() closeFunc {
d.discovery = internalDiscovery.New(
d.pool.Get(endpoint.New(d.config.Endpoint())),
discoveryConfig.New(
append(
// prepend common params from root config
[]discoveryConfig.Option{
discoveryConfig.With(c.config.Common),
discoveryConfig.WithEndpoint(c.Endpoint()),
discoveryConfig.WithDatabase(c.Name()),
discoveryConfig.WithSecure(c.Secure()),
discoveryConfig.WithMeta(c.config.Meta()),
discoveryConfig.With(d.config.Common),
discoveryConfig.WithEndpoint(d.Endpoint()),
discoveryConfig.WithDatabase(d.Name()),
discoveryConfig.WithSecure(d.Secure()),
discoveryConfig.WithMeta(d.config.Meta()),
},
c.discoveryOptions...,
d.discoveryOptions...,
)...,
),
)
return c.discovery.Close
return d.discovery.Close
})
// may be nil if driver closed early
return c.discovery
return d.discovery
}

// Scripting returns scripting client
func (c *Driver) Scripting() scripting.Client {
c.scriptingOnce.Init(func() closeFunc {
c.scripting = internalScripting.New(
c.balancer,
func (d *Driver) Scripting() scripting.Client {
d.scriptingOnce.Init(func() closeFunc {
d.scripting = internalScripting.New(
d.balancer,
scriptingConfig.New(
append(
// prepend common params from root config
[]scriptingConfig.Option{
scriptingConfig.With(c.config.Common),
scriptingConfig.With(d.config.Common),
},
c.scriptingOptions...,
d.scriptingOptions...,
)...,
),
)
return c.scripting.Close
return d.scripting.Close
})
// may be nil if driver closed early
return c.scripting
return d.scripting
}

// Topic returns topic client
func (c *Driver) Topic() topic.Client {
c.topicOnce.Init(func() closeFunc {
c.topic = topicclientinternal.New(c.balancer, c.config.Credentials(),
func (d *Driver) Topic() topic.Client {
d.topicOnce.Init(func() closeFunc {
d.topic = topicclientinternal.New(d.balancer, d.config.Credentials(),
append(
// prepend common params from root config
[]topicoptions.TopicOption{
topicoptions.WithOperationTimeout(c.config.OperationTimeout()),
topicoptions.WithOperationCancelAfter(c.config.OperationCancelAfter()),
topicoptions.WithOperationTimeout(d.config.OperationTimeout()),
topicoptions.WithOperationCancelAfter(d.config.OperationCancelAfter()),
},
c.topicOptions...,
d.topicOptions...,
)...,
)
return c.topic.Close
return d.topic.Close
})
return c.topic
return d.topic
}

// Open connects to database by DSN and return driver runtime holder
Expand Down Expand Up @@ -427,6 +431,7 @@ func newConnectionFromOptions(ctx context.Context, opts ...Option) (_ *Driver, e
WithTraceDiscovery(log.Discovery(d.logger, d.loggerDetails, d.loggerOpts...)),
WithTraceTopic(log.Topic(d.logger, d.loggerDetails, d.loggerOpts...)),
WithTraceDatabaseSQL(log.DatabaseSQL(d.logger, d.loggerDetails, d.loggerOpts...)),
WithTraceRetry(log.Retry(d.logger, d.loggerDetails, d.loggerOpts...)),
} {
if opt != nil {
err = opt(ctx, d)
Expand Down
38 changes: 15 additions & 23 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@ go 1.20
require (
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/lib/pq v1.10.2
github.com/prometheus/client_golang v1.13.0
github.com/lib/pq v1.10.7
github.com/ydb-platform/gorm-driver v0.0.5
github.com/ydb-platform/ydb-go-sdk-auth-environ v0.1.2
github.com/ydb-platform/ydb-go-sdk-prometheus v0.11.10
github.com/ydb-platform/ydb-go-sdk/v3 v3.47.3
github.com/ydb-platform/ydb-go-sdk/v3 v3.48.7
github.com/ydb-platform/ydb-go-yc v0.10.1
google.golang.org/genproto v0.0.0-20230131230820-1c016267d619
gorm.io/driver/postgres v1.5.0
gorm.io/driver/sqlite v1.5.0
gorm.io/gorm v1.25.1
modernc.org/sqlite v1.14.2
modernc.org/sqlite v1.20.4
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978
xorm.io/xorm v1.3.2
)

require (
cloud.google.com/go/firestore v1.9.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/goccy/go-json v0.8.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
Expand All @@ -39,18 +36,13 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/yandex-cloud/go-genproto v0.0.0-20220815090733-4c139c0154e2 // indirect
github.com/ydb-platform/ydb-go-genproto v0.0.0-20231012155159-f85a672542fd // indirect
github.com/ydb-platform/ydb-go-sdk-metrics v0.16.3 // indirect
github.com/ydb-platform/ydb-go-yc-metadata v0.5.4 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/mod v0.8.0 // indirect
Expand All @@ -61,17 +53,17 @@ require (
golang.org/x/tools v0.6.0 // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
lukechampine.com/uint128 v1.1.1 // indirect
modernc.org/cc/v3 v3.35.18 // indirect
modernc.org/ccgo/v3 v3.12.82 // indirect
modernc.org/libc v1.11.87 // indirect
modernc.org/mathutil v1.4.1 // indirect
modernc.org/memory v1.0.5 // indirect
modernc.org/opt v0.1.1 // indirect
modernc.org/strutil v1.1.1 // indirect
modernc.org/token v1.0.0 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
modernc.org/libc v1.22.2 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.4.0 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/token v1.0.1 // indirect
)

replace github.com/ydb-platform/ydb-go-sdk/v3 => ../

replace xorm.io/xorm => github.com/ydb-platform/xorm v0.0.3
replace xorm.io/xorm => github.com/ydb-platform/xorm v0.0.6
Loading

0 comments on commit da26af2

Please sign in to comment.