forked from ydb-platform/ydb-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwith.go
57 lines (46 loc) · 1.03 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
package ydb
import (
"context"
"sync/atomic"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
)
var nextID = uint64(0)
func (c *connection) with(ctx context.Context, opts ...Option) (*connection, uint64, error) {
id := atomic.AddUint64(&nextID, 1)
child, err := newConnectionFromOptions(
ctx,
append(
append(
c.opts,
WithBalancer(
c.config.Balancer(),
),
withOnClose(func(child *connection) {
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
}
func (c *connection) With(ctx context.Context, opts ...Option) (Connection, 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
}