Skip to content

Commit 11e1336

Browse files
committed
fix: simplify pool synchronization by replacing RWMutex with Mutex
Replaced RWMutex with Mutex to streamline lock handling and simplify the synchronization logic in the connection pool implementation. The change eliminates redundant lock/unlock calls, ensuring thread safety while reducing complexity.
1 parent 3eb330c commit 11e1336

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

pkg/clients/database/pool.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func NewPoolWithConnectionFactory(
6363
}
6464

6565
type poolImpl struct {
66-
sync.RWMutex
66+
sync.Mutex
6767
isClosed bool
6868
logger *slog.Logger
6969
closers []func()
@@ -75,6 +75,9 @@ type poolImpl struct {
7575

7676
func (p *poolImpl) GetConnection(params PoolParams) (Connection, error) {
7777

78+
p.Lock()
79+
defer p.Unlock()
80+
7881
connectionParams := ConnectionParams{
7982
ClientID: p.clientID,
8083
ClientSecret: p.clientSecret,
@@ -84,29 +87,23 @@ func (p *poolImpl) GetConnection(params PoolParams) (Connection, error) {
8487
}
8588
hash := connectionParams.Hash()
8689

87-
// First, try to get an existing connection with a read lock
88-
p.RLock()
90+
// First, try to get an existing connection
8991
if p.isClosed {
90-
p.RUnlock()
9192
return nil, ErrPoolClosed
9293
}
9394
if conn, ok := p.connections[hash]; ok {
94-
p.RUnlock()
9595
return conn, nil
9696
}
97-
p.RUnlock()
9897

9998
// Create a new connection if one doesn't exist
10099
conn, closer, err := p.newConnectionFunc(p.logger, connectionParams)
101100
if err != nil {
102101
return nil, fmt.Errorf("failed to create connection: %w", err)
103102
}
104103

105-
// Store the new connection in the pool with a write lock
106-
p.Lock()
104+
// Store the new connection in the pool
107105
p.connections[hash] = conn
108106
p.closers = append(p.closers, closer)
109-
p.Unlock()
110107

111108
return conn, nil
112109
}

0 commit comments

Comments
 (0)