forked from ydb-platform/ydb-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwith.go
58 lines (47 loc) · 1.11 KB
/
with.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"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xatomic"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
)
var nextID xatomic.Uint64
func (c *Driver) with(ctx context.Context, opts ...Option) (*Driver, uint64, error) {
id := nextID.Add(1)
child, err := newConnectionFromOptions(
ctx,
append(
append(
c.opts,
WithBalancer(
c.config.Balancer(),
),
withOnClose(func(child *Driver) {
c.childrenMtx.Lock()
defer c.childrenMtx.Unlock()
delete(c.children, id)
}),
withConnPool(c.pool),
),
opts...,
)...,
)
if err != nil {
return nil, 0, xerrors.WithStackTrace(err)
}
return child, id, nil
}
// With makes child Driver with the same options and another options
func (c *Driver) With(ctx context.Context, opts ...Option) (*Driver, error) {
child, id, err := c.with(ctx, opts...)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
err = connect(ctx, child)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
c.childrenMtx.Lock()
defer c.childrenMtx.Unlock()
c.children[id] = child
return child, nil
}