Skip to content

Commit 95fadff

Browse files
authored
Merge pull request #220 from ydb-platform/fix_dsn_sugar
Reimplement sugar.DSN with net/url
2 parents 7eb5e28 + 9b4900c commit 95fadff

File tree

8 files changed

+130
-81
lines changed

8 files changed

+130
-81
lines changed

CHANGELOG.md

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
* Reimplement sugar.DSN with net/url
2+
13
## v3.21.0
2-
* Fixed gtrace tool generation code style bug with leading spaces
4+
* Fixed gtrace tool generation code style bug with leading spaces
35
* Removed accounting load factor (unused field) in balancers
46
* Enabled by default anonymous credentials
57
* Enabled by default internal dns resolver
@@ -39,7 +41,7 @@
3941

4042
## v3.18.5
4143
* Fixed duplicating `WithPanicCallback` proxying to table config options
42-
* Fixed comments for `xerrros.Is` and `xerrros.As`
44+
* Fixed comments for `xerrros.Is` and `xerrros.As`
4345

4446
## v3.18.4
4547
* Renamed internal packages `errors`, `net` and `resolver` to `xerrors`, `xnet` and `xresolver` for excluding ambiguous interpretation
@@ -52,7 +54,7 @@
5254
## v3.18.2
5355
* Refactored balancers (makes concurrent-safe)
5456
* Excluded separate balancers lock from cluster
55-
* Refactored `cluster.Cluster` interface (`Insert` and `Remove` returning nothing now)
57+
* Refactored `cluster.Cluster` interface (`Insert` and `Remove` returning nothing now)
5658
* Replaced unsafe `cluster.close` boolean flag to `cluster.done` chan for listening close event
5759
* Added internal checker `cluster.isClosed()` for check cluster state
5860
* Extracted getting available conn from balancer to internal helper `cluster.get` (called inside `cluster.Get` as last effort)
@@ -69,7 +71,7 @@
6971
* Added `ydb.WithOperationTimeout` and `ydb.WithOperationCancelAfter` context modifiers
7072

7173
## v3.17.0
72-
* Removed redundant `trace.With{Table,Driver,Retry}` and `trace.Context{Table,Driver,Retry}` funcs
74+
* Removed redundant `trace.With{Table,Driver,Retry}` and `trace.Context{Table,Driver,Retry}` funcs
7375
* Moved `gtrace` tool from `./cmd/gtrace` to `./internal/cmd/gtrace`
7476
* Refactored `gtrace` tool for generate `Compose` options
7577
* Added panic recover on trace calls in `Compose` call step
@@ -78,7 +80,7 @@
7880

7981
## v3.16.12
8082
* Fixed bug with check acquire error over `ydb.IsRatelimiterAcquireError`
81-
* Added full changelog link to github release description
83+
* Added full changelog link to github release description
8284

8385
## v3.16.11
8486
* Added stacktrace to errors with issues
@@ -96,7 +98,7 @@
9698
* Refactored internal operation and transport errors
9799

98100
## v3.16.8
99-
* Added `config.ExcludeGRPCCodesForPessimization()` opttion for exclude some grpc codes from pessimization rules
101+
* Added `config.ExcludeGRPCCodesForPessimization()` opttion for exclude some grpc codes from pessimization rules
100102
* Refactored pessimization node conditions
101103
* Added closing of ticker in `conn.Conn.connParker`
102104
* Removed `config.WithSharedPool` and usages it
@@ -114,7 +116,7 @@
114116

115117
## v3.16.5
116118
* Added `config.SharedPool()` setting and `config.WithSharedPool()` option
117-
* Added management of shared pool flag on change dial timeout and credentials
119+
* Added management of shared pool flag on change dial timeout and credentials
118120
* Removed explicit checks of conditions for use (or not) shared pool in `ydb.With()`
119121
* Renamed `internal/db` interfaces
120122
* Changed signature of `conn.Conn.Release` (added error as result)
@@ -132,7 +134,7 @@
132134
* Removed explicit call meta in `db.New()`
133135

134136
## v3.16.1
135-
* Added `WithMeta()` discovery config option
137+
* Added `WithMeta()` discovery config option
136138
* Fixed bug with credentials on discovery
137139

138140
## v3.16.0
@@ -142,7 +144,7 @@
142144
## v3.15.1
143145
* Removed all conditions for trace retry errors
144146
* Fixed background color of warn messages
145-
* Added to log messages additional information about error, such as retryable (or not), delete session (or not), etc.
147+
* Added to log messages additional information about error, such as retryable (or not), delete session (or not), etc.
146148

147149
## v3.15.0
148150
* Added github action for publish release tags
@@ -151,7 +153,7 @@
151153
* Fixed race on check trailer on closing table grpc-stream
152154
* Refactored traces (start and done struct names have prefix about trace)
153155
* Replaced `errors.Error`, `errors.Errorf` and `errors.ErrorfSkip` to single `errors.WithStackTrace`
154-
* Refactored table client options
156+
* Refactored table client options
155157
* Declared and implemented interface `errors.isYdbError` for checking ybd/non-ydb errors
156158
* Fixed double tracing table do events
157159
* Added `retry.WithFastBackoff` and `retry.WithFastBackoff` options
@@ -183,17 +185,17 @@
183185
* Fixed linter issues
184186

185187
## 3.13.2
186-
* Fixed race with read/write pool conns on closing conn
188+
* Fixed race with read/write pool conns on closing conn
187189

188190
## 3.13.1
189191
* Improved error messages
190192
* Defended `cluster.balancer` with `sync.RWMutex` on `cluster.Insert`, `cluster.Update`, `cluster.Remove` and `cluster.Get`
191193
* Excluded `Close` and `Park` methods from `conn.Conn` interface
192-
* Fixed bug with `Multi` balancer `Create()`
194+
* Fixed bug with `Multi` balancer `Create()`
193195
* Improved `errors.IsTransportError` (check a few transport error codes instead check single transport error code)
194196
* Improved `errors.Is` (check a few errors instead check single error)
195197
* Refactored YDB errors checking API on client-side
196-
* Implemented of scripting traces
198+
* Implemented of scripting traces
197199

198200
## 3.13.0
199201
* Refactored `Connection` interface
@@ -209,7 +211,7 @@
209211
* Fixed bug with closing nil session in table retryer
210212
* Restored repeater `Force` call on pessimize event
211213
* Changed mutex type in `conn.Conn` from `sync.Mutex` to `sync.RWMutex` for exclude deadlocks
212-
* Reverted applying empty `discovery` results to `cluster`
214+
* Reverted applying empty `discovery` results to `cluster`
213215

214216
## 3.12.0
215217
* Added `balancers.Prefer` and `balancers.PreferWithFallback` constructors
@@ -236,7 +238,7 @@
236238
## 3.11.8
237239
* Added `trace.EndpointInfo.LastUpdated()` timestamp
238240
* Refactored `endpoint.Endpoint` (split to struct `endopint` and interface `Endpoint`)
239-
* Returned safe-thread copy of `endpoint.Endpoint` to trace callbacks
241+
* Returned safe-thread copy of `endpoint.Endpoint` to trace callbacks
240242
* Added `endpoint.Endpoint.Touch()` func for refresh endpoint info
241243
* Added `conn.conn.onClose` slice for call optional funcs on close step
242244
* Added removing `conn.Conn` from `conn.Pool` on `conn.Conn.Close()` call
@@ -275,7 +277,7 @@
275277
* Removed `trace.Table.OnPoolTake` event (unused)
276278
* Refactored `trace.Details` matching by string pattern
277279
* Added resolver trace callback
278-
* Refactored initialization step of grpc dial options
280+
* Refactored initialization step of grpc dial options
279281
* Added internal package `net` with `net.Conn` proxy object
280282
* Fixed closing proxy clients
281283
* Added `ydb.Connection.With(opts ...ydb.CustomOption)` for taking proxy `ydb.Connection` with some redefined options
@@ -319,7 +321,7 @@
319321
* Added `ydb.RegisterParser` method for registering external parser of connection string
320322

321323
## 3.8.12
322-
* Unwrap sub-tests called as `t.Run(...)` in integration tests
324+
* Unwrap sub-tests called as `t.Run(...)` in integration tests
323325
* Updated `grpc` dependency (from `v1.38.0` to `v1.43.0`)
324326
* Updated `protobuf` dependency (from `v1.26.0` to `v1.27.1`)
325327
* Added internal retryers into `lazy.Ratelimiter`
@@ -390,7 +392,7 @@
390392
* Added `String()` method to `table.types.Type` interface
391393
* Added `With[Custom]UserAgent()` `Option` and `CustomOption` constructors
392394
* Refactored `log.Logger` interface and internal implementation
393-
* Added `retry.RetryableError()` for returns user-defined error which must be retryed
395+
* Added `retry.RetryableError()` for returns user-defined error which must be retryed
394396
* Renamed internal type `internal.errors.OperationCompleted` to `internal.errors.OperationStatus`
395397
* Added `String()` method to `table.KeyRange` and `table.Value` types
396398
* Replaced creation of goroutine on each stream call to explicit call stream.Recv() on NextResultSet()
@@ -403,7 +405,7 @@
403405

404406
## 3.6.1
405407
* Switched closing cluster after closing all sub-services
406-
* Added windows and macOS runtimes to unit and integration tests
408+
* Added windows and macOS runtimes to unit and integration tests
407409

408410
## 3.6.0
409411
* Added `config/balancer` package with popular balancers
@@ -448,7 +450,7 @@
448450
* Fixed `CGO` race on `Darwin` system when driver tried to expand tilde on certificates path
449451
* Removed `EnsurePathExists` and `CleanupDatabase` from API of `scheme.Client`
450452
* Added helpers `MakePath` and `CleanPath` to root of package `ydb-go-sdk`
451-
* Removed call `types.Scanner.UnmarshalYDB()` inside `scanner.setDefaults()`
453+
* Removed call `types.Scanner.UnmarshalYDB()` inside `scanner.setDefaults()`
452454
* Added `DoTx()` API method into `table.Client`
453455
* Added `String()` method into `ConnectParams` for serialize params to connection string
454456
* Added early exit from Rollback for committed transaction
@@ -485,25 +487,25 @@
485487
## 3.3.0
486488
* Stored node ID into `endpoint.Endpoint` struct
487489
* Simplified <Host,Port> in `endpoint.Endpoint` to single fqdn Address
488-
* On table session requests now preferred the endpoint by `ID` extracted from session `ID`. If
490+
* On table session requests now preferred the endpoint by `ID` extracted from session `ID`. If
489491
endpoint by `ID` not found - using the endpoint from balancer
490-
* Upgraded internal logger for print colored messages
492+
* Upgraded internal logger for print colored messages
491493

492494
## 3.2.7
493495
* Fixed compare endpoints func
494496

495497
## 3.2.6
496-
* Reverted `NodeID` as key for link between session and endpoint because yandex-cloud YDB
497-
installation not supported `Endpoint.ID` entity
498+
* Reverted `NodeID` as key for link between session and endpoint because yandex-cloud YDB
499+
installation not supported `Endpoint.ID` entity
498500

499501
## 3.2.5
500-
* Dropped endpoint.Addr entity as unused. After change link type between session and endpoint
502+
* Dropped endpoint.Addr entity as unused. After change link type between session and endpoint
501503
to NodeID endpoint.Addr became unnecessary for internal logic of driver
502504
* Enabled integration test table pool health
503505
* Fixed race on session stream requests
504506

505507
## 3.2.4
506-
* Returned context error when context is done on `session.StreamExecuteScanQuery`
508+
* Returned context error when context is done on `session.StreamExecuteScanQuery`
507509
and `session.StreamReadTable`
508510

509511
## 3.2.3
@@ -524,7 +526,7 @@
524526
* added internal leveled logger which implement interface `log.Logger`
525527
* supported environment variable `YDB_LOG_SEVERITY_LEVEL`
526528
* changed name of the field `RetryAttempts` to` Attempts` in the structure `trace.PoolGetDoneInfo`.
527-
This change reduces back compatibility, but there are no external uses of v3 sdk, so this change is
529+
This change reduces back compatibility, but there are no external uses of v3 sdk, so this change is
528530
fine. We are sorry if this change broke your code
529531

530532
## 3.1.0

internal/conn/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (c *conn) take(ctx context.Context) (cc *grpc.ClientConn, err error) {
221221
}
222222

223223
// prepend "ydb" scheme for grpc dns-xresolver to find the proper scheme
224-
/// ydb:///", three slashes is ok. It need for good parse scheme in grpc resolver.
224+
// ydb:///", three slashes is ok. It need for good parse scheme in grpc resolver.
225225
address := "ydb:///" + c.endpoint.Address()
226226

227227
cc, err = grpc.DialContext(ctx, address, c.config.GrpcDialOptions()...)

internal/dsn/dsn_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dsn
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76

87
"github.com/ydb-platform/ydb-go-sdk/v3/config"
@@ -31,34 +30,34 @@ func TestParseConnectionString(t *testing.T) {
3130
error error
3231
}{
3332
{
34-
"grpc://ydb-ru.yandex.xnet:2135/?" +
33+
"grpc://ydb-ru.yandex.net:2135/?" +
3534
"database=/ru/home/gvit/mydb&token=123",
3635
false,
37-
"ydb-ru.yandex.xnet:2135",
36+
"ydb-ru.yandex.net:2135",
3837
"/ru/home/gvit/mydb",
3938
"123",
4039
nil,
4140
},
4241
{
43-
"grpcs://ydb.serverless.yandexcloud.xnet:2135/?" +
42+
"grpcs://ydb.serverless.yandexcloud.net:2135/?" +
4443
"database=/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1&token=123",
4544
true,
46-
"ydb.serverless.yandexcloud.xnet:2135",
45+
"ydb.serverless.yandexcloud.net:2135",
4746
"/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1",
4847
"123",
4948
nil,
5049
},
5150
{
52-
"grpcs://lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.xnet:2135/?" +
51+
"grpcs://lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135/?" +
5352
"database=/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv&token=123",
5453
true,
55-
"lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.xnet:2135",
54+
"lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135",
5655
"/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv",
5756
"123",
5857
nil,
5958
},
6059
{
61-
"abcd://ydb-ru.yandex.xnet:2135/?database=/ru/home/gvit/mydb",
60+
"abcd://ydb-ru.yandex.net:2135/?database=/ru/home/gvit/mydb",
6261
true,
6362
"",
6463
"",
@@ -69,7 +68,7 @@ func TestParseConnectionString(t *testing.T) {
6968
t.Run(test.connectionString, func(t *testing.T) {
7069
options, err := Parse(test.connectionString)
7170
if !xerrors.Is(err, test.error) {
72-
t.Fatal(fmt.Sprintf("Received unexpected error:\n%+v", err))
71+
t.Fatalf("Received unexpected error:\n%+v", err)
7372
}
7473
config := config.New(options...)
7574
testutil.Equal(t, test.secure, config.Secure())
@@ -79,7 +78,7 @@ func TestParseConnectionString(t *testing.T) {
7978
if credentials := config.Credentials(); credentials != nil {
8079
token, err = credentials.Token(context.Background())
8180
if err != nil {
82-
t.Fatal(fmt.Sprintf("Received unexpected error:\n%+v", err))
81+
t.Fatalf("Received unexpected error:\n%+v", err)
8382
}
8483
} else {
8584
token = ""

options.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/x509"
66
"encoding/pem"
7+
"fmt"
78
"io/ioutil"
89
"os"
910
"path/filepath"
@@ -62,27 +63,16 @@ func WithRequestsType(requestsType string) Option {
6263
// (connection string will be required string param of ydb.Open)
6364
func WithConnectionString(connectionString string) Option {
6465
return func(ctx context.Context, c *connection) error {
65-
var (
66-
urls = []string{
67-
connectionString,
68-
"grpcs://" + connectionString,
69-
}
70-
issues = make([]error, 0, len(urls))
71-
)
72-
for _, url := range urls {
73-
options, err := dsn.Parse(url)
74-
if err == nil {
75-
c.options = append(c.options, options...)
76-
return nil
77-
}
78-
issues = append(issues, err)
66+
if connectionString == "" {
67+
return nil
7968
}
80-
if len(issues) > 0 {
81-
return xerrors.WithStackTrace(xerrors.NewWithIssues(
82-
"parse connection string '"+connectionString+"' failed:",
83-
issues...,
84-
))
69+
options, err := dsn.Parse(connectionString)
70+
if err != nil {
71+
return xerrors.WithStackTrace(
72+
fmt.Errorf("parse connection string '%s' failed: %w", connectionString, err),
73+
)
8574
}
75+
c.options = append(c.options, options...)
8676
return nil
8777
}
8878
}

sugar/dsn.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
package sugar
22

3+
import "net/url"
4+
35
// Usage of this package
46
//
57
// db, err := ydb.Open(
68
// ctx,
79
// sugar.DSN("endpoint", "database", false),
810
// )
911

10-
type connectionString struct {
11-
endpoint string
12-
database string
13-
secure bool
14-
}
12+
// DSN makes connection string (data source name) by endpoint, database and secure
13+
func DSN(endpoint, database string, secure bool) (s string) {
14+
qp := url.Values{}
15+
qp.Set("database", database)
1516

16-
func (cs connectionString) Build() (s string) {
17-
s = "grpc"
18-
if cs.secure {
19-
s += "s"
17+
dsn := url.URL{
18+
Scheme: "grpc",
19+
Host: endpoint,
20+
RawQuery: qp.Encode(),
2021
}
21-
return s + "://" + cs.endpoint + "/?database=" + cs.database
22-
}
2322

24-
// DSN makes connection string (data source name) by endpoint, database and secure
25-
func DSN(endpoint, database string, secure bool) (s string) {
26-
cs := connectionString{
27-
endpoint: endpoint,
28-
database: database,
29-
secure: secure,
23+
if secure {
24+
dsn.Scheme = "grpcs"
3025
}
31-
return cs.Build()
26+
27+
return dsn.String()
3228
}

0 commit comments

Comments
 (0)