Skip to content

Commit 9f2b47d

Browse files
committed
add xsync
rewrite critical section with more safe code
1 parent 1ef9790 commit 9f2b47d

File tree

11 files changed

+342
-195
lines changed

11 files changed

+342
-195
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Added to query stats plan and AST
33
* Changed behaviour of `result.Stats()` (if query result have no stats - returns `nil`)
44
* Added context cancel with specific error
5+
* Added mutex wrapper for mutex, rwmutex for guarantee unlock and better show critical section
56

67
## v3.26.10
78
* Fixed syntax mistake in `trace.TablePooStateChangeInfo` to `trace.TablePoolStateChangeInfo`

connection.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"google.golang.org/grpc"
1010

11+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
12+
1113
"github.com/ydb-platform/ydb-go-sdk/v3/config"
1214
"github.com/ydb-platform/ydb-go-sdk/v3/coordination"
1315
"github.com/ydb-platform/ydb-go-sdk/v3/discovery"
@@ -108,7 +110,7 @@ type connection struct {
108110
balancer balancer.Connection
109111

110112
children map[uint64]Connection
111-
childrenMtx sync.Mutex
113+
childrenMtx xsync.Mutex
112114
onClose []func(c *connection)
113115

114116
panicCallback func(e interface{})
@@ -124,13 +126,13 @@ func (c *connection) Close(ctx context.Context) error {
124126
}
125127
}()
126128

127-
c.childrenMtx.Lock()
128129
closers := make([]func(context.Context) error, 0)
129-
for _, child := range c.children {
130-
closers = append(closers, child.Close)
131-
}
132-
c.children = nil
133-
c.childrenMtx.Unlock()
130+
c.childrenMtx.WithLock(func() {
131+
for _, child := range c.children {
132+
closers = append(closers, child.Close)
133+
}
134+
c.children = nil
135+
})
134136

135137
closers = append(
136138
closers,

internal/conn/pool.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package conn
22

33
import (
44
"context"
5-
"sync"
65
"sync/atomic"
76
"time"
87

98
"google.golang.org/grpc"
109

10+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
11+
1112
"github.com/ydb-platform/ydb-go-sdk/v3/internal/closer"
1213
"github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint"
1314
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
@@ -17,7 +18,7 @@ import (
1718
type Pool struct {
1819
usages int64
1920
config Config
20-
mtx sync.RWMutex
21+
mtx xsync.RWMutex
2122
opts []grpc.DialOption
2223
conns map[string]*conn
2324
done chan struct{}
@@ -100,12 +101,13 @@ func (p *Pool) Release(ctx context.Context) error {
100101

101102
close(p.done)
102103

103-
p.mtx.RLock()
104-
conns := make([]closer.Closer, 0, len(p.conns))
105-
for _, c := range p.conns {
106-
conns = append(conns, c)
107-
}
108-
p.mtx.RUnlock()
104+
var conns []closer.Closer
105+
p.mtx.WithRLock(func() {
106+
conns = make([]closer.Closer, 0, len(p.conns))
107+
for _, c := range p.conns {
108+
conns = append(conns, c)
109+
}
110+
})
109111

110112
var issues []error
111113
for _, c := range conns {

0 commit comments

Comments
 (0)