forked from yandex-cloud/ydb-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
63 lines (52 loc) · 1014 Bytes
/
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
59
60
61
62
63
package ydb
import (
"context"
"sync"
"google.golang.org/grpc/metadata"
)
const (
metaDatabase = "x-ydb-database"
metaTicket = "x-ydb-auth-ticket"
)
type meta struct {
credentials Credentials
mu sync.RWMutex
database string
token string
curr metadata.MD
}
func newMeta(database string, c Credentials) *meta {
md := make(metadata.MD, 1)
md.Set(metaDatabase, database)
return &meta{
credentials: c,
database: database,
curr: md,
}
}
func (m *meta) md(ctx context.Context) (md metadata.MD, err error) {
if m.credentials == nil {
return m.curr, nil
}
token, err := m.credentials.Token(ctx)
if err != nil {
return nil, err
}
m.mu.RLock()
changed := m.token != token
md = m.curr
m.mu.RUnlock()
if !changed {
return md, nil
}
m.mu.Lock()
defer m.mu.Unlock()
if m.token == token {
return m.curr, nil
}
m.token = token
m.curr = make(metadata.MD, 2)
m.curr.Set(metaDatabase, m.database)
m.curr.Set(metaTicket, m.token)
return m.curr, nil
}