-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmeta.go
58 lines (52 loc) · 1.3 KB
/
meta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package ydb
import (
"context"
"fmt"
"google.golang.org/grpc/metadata"
)
const (
metaDatabase = "x-ydb-database"
metaTicket = "x-ydb-auth-ticket"
metaVersion = "x-ydb-sdk-build-info"
metaRequestType = "x-ydb-request-type"
metaTraceID = "x-ydb-trace-id"
metaUserAgent = "x-ydb-user-agent"
)
type meta struct {
trace DriverTrace
credentials Credentials
database string
requestsType string
}
type MetaDatabaseCtxKey struct{}
func (m *meta) md(ctx context.Context) (md metadata.MD, err error) {
var has bool
if md, has = metadata.FromOutgoingContext(ctx); !has {
md = metadata.MD{}
}
database := m.database
if v, ok := ctx.Value(MetaDatabaseCtxKey{}).(string); ok {
database = v
}
md.Set(metaDatabase, database)
md.Set(metaVersion, Version)
if m.requestsType != "" {
md.Set(metaRequestType, m.requestsType)
}
if m.credentials != nil {
var token string
driverTraceGetCredentialsDone := driverTraceOnGetCredentials(ctx, m.trace, ctx)
defer func() {
driverTraceGetCredentialsDone(ctx, token != "", err)
}()
token, err = m.credentials.Token(ctx)
if err != nil {
if stringer, ok := m.credentials.(fmt.Stringer); ok {
return nil, fmt.Errorf("%s: %w", stringer.String(), err)
}
return nil, err
}
md.Set(metaTicket, token)
}
return md, nil
}