Skip to content

Commit

Permalink
sync upstream
Browse files Browse the repository at this point in the history
ref:f6ada2eb4b2d355828184bf7741739cd9af2c994
  • Loading branch information
arcadia-devtools committed Aug 6, 2020
1 parent 8ebba4e commit 9aa32e9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
7 changes: 4 additions & 3 deletions dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ func (d *Dialer) Dial(ctx context.Context, addr string) (Driver, error) {
timeout: d.Timeout,
config: config,
meta: &meta{
trace: config.Trace,
database: config.Database,
credentials: config.Credentials,
trace: config.Trace,
database: config.Database,
credentials: config.Credentials,
requestsType: config.RequestsType,
},
}).dial(ctx, addr)
}
Expand Down
4 changes: 4 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ type DriverConfig struct {
// is, currently this option may be called as experimental.
// You have been warned.
PreferLocalEndpoints bool

// RequestsType set an additional type hint to all requests.
// It is needed only for debug purposes and advanced cases.
RequestsType string
}

func (d *DriverConfig) withDefaults() (c DriverConfig) {
Expand Down
33 changes: 21 additions & 12 deletions meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import (
)

const (
metaDatabase = "x-ydb-database"
metaTicket = "x-ydb-auth-ticket"
metaVersion = "x-ydb-sdk-build-info"
metaDatabase = "x-ydb-database"
metaTicket = "x-ydb-auth-ticket"
metaVersion = "x-ydb-sdk-build-info"
metaRequestType = "x-ydb-request-type"
metaTeraceID = "x-ydb-trace-id"
)

type meta struct {
trace DriverTrace
credentials Credentials
database string
trace DriverTrace
credentials Credentials
database string
requestsType string

once sync.Once
mu sync.RWMutex
Expand All @@ -25,10 +28,17 @@ type meta struct {
}

func (m *meta) make() metadata.MD {
return metadata.New(map[string]string{
newMeta := metadata.New(map[string]string{
metaDatabase: m.database,
metaVersion: Version,
})
if m.requestsType != "" {
newMeta.Set(metaRequestType, m.requestsType)
}
if m.token != "" {
newMeta.Set(metaTicket, m.token)
}
return newMeta
}

func (m *meta) md(ctx context.Context) (md metadata.MD, _ error) {
Expand All @@ -52,6 +62,9 @@ func (m *meta) md(ctx context.Context) (md metadata.MD, _ error) {
// Continue.

case ErrCredentialsDropToken:
m.mu.Lock()
defer m.mu.Unlock()
m.token = ""
return m.make(), nil

case ErrCredentialsKeepToken:
Expand All @@ -77,10 +90,6 @@ func (m *meta) md(ctx context.Context) (md metadata.MD, _ error) {
}
m.token = token

m.curr = make(metadata.MD, 3)
m.curr.Set(metaDatabase, m.database)
m.curr.Set(metaTicket, m.token)
m.curr.Set(metaVersion, Version)

m.curr = m.make()
return m.curr, nil
}
17 changes: 14 additions & 3 deletions meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
func TestMetaErrDropToken(t *testing.T) {
var call int
m := &meta{
database: "database",
database: "database",
requestsType: "requestType",
credentials: CredentialsFunc(func(context.Context) (string, error) {
if call == 0 {
call++
Expand All @@ -26,19 +27,22 @@ func TestMetaErrDropToken(t *testing.T) {
}
assertMetaHasDatabase(t, md1)
assertMetaHasToken(t, md1)
assertMetaHasRequestType(t, md1)

md2, err := m.md(context.Background())
if err != nil {
t.Fatal(err)
}
assertMetaHasDatabase(t, md2)
assertMetaHasNoToken(t, md2)
assertMetaHasRequestType(t, md2)
}

func TestMetaErrKeepToken(t *testing.T) {
var call int
m := &meta{
database: "database",
database: "database",
requestsType: "requestType",
credentials: CredentialsFunc(func(context.Context) (string, error) {
if call == 0 {
call++
Expand All @@ -54,13 +58,15 @@ func TestMetaErrKeepToken(t *testing.T) {
}
assertMetaHasDatabase(t, md1)
assertMetaHasToken(t, md1)
assertMetaHasRequestType(t, md1)

md2, err := m.md(context.Background())
if err != nil {
t.Fatal(err)
}
assertMetaHasDatabase(t, md2)
assertMetaHasToken(t, md1)
assertMetaHasToken(t, md2)
assertMetaHasRequestType(t, md2)
}

func assertMetaHasDatabase(t *testing.T, md metadata.MD) {
Expand All @@ -78,3 +84,8 @@ func assertMetaHasNoToken(t *testing.T, md metadata.MD) {
t.Errorf("unexpected token info in meta")
}
}
func assertMetaHasRequestType(t *testing.T, md metadata.MD) {
if len(md.Get(metaRequestType)) == 0 {
t.Errorf("no request type info in meta")
}
}

0 comments on commit 9aa32e9

Please sign in to comment.