-
Notifications
You must be signed in to change notification settings - Fork 71
/
with.go
69 lines (56 loc) · 1.42 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
59
60
61
62
63
64
65
66
67
68
69
package ydb
import (
"context"
"sync/atomic"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)
var nextID atomic.Uint64 //nolint:gochecknoglobals
func (d *Driver) with(ctx context.Context, opts ...Option) (*Driver, uint64, error) {
id := nextID.Add(1)
child, err := newConnectionFromOptions(
ctx,
append(
append(
d.opts,
WithBalancer(
d.config.Balancer(),
),
withOnClose(func(child *Driver) {
d.childrenMtx.Lock()
defer d.childrenMtx.Unlock()
delete(d.children, id)
}),
withConnPool(d.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 (d *Driver) With(ctx context.Context, opts ...Option) (*Driver, error) {
child, id, err := d.with(ctx, opts...)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
onDone := trace.DriverOnWith(
d.trace(), &ctx,
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/ydb.(*Driver).With"),
d.config.Endpoint(), d.config.Database(), d.config.Secure(),
)
defer func() {
onDone(err)
}()
if err = child.connect(ctx); err != nil {
return nil, xerrors.WithStackTrace(err)
}
d.childrenMtx.Lock()
defer d.childrenMtx.Unlock()
d.children[id] = child
return child, nil
}