Skip to content

Commit af0160d

Browse files
committed
switched internal/balancer.Balancer.connections_state from pointer to atomic.Pointer
1 parent 98a04d2 commit af0160d

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

internal/balancer/balancer.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"sync/atomic"
78

89
"google.golang.org/grpc"
910

@@ -41,9 +42,9 @@ type Balancer struct {
4142
discoveryRepeater repeater.Repeater
4243
localDCDetector func(ctx context.Context, endpoints []endpoint.Endpoint) (string, error)
4344

44-
mu xsync.RWMutex
45-
connectionsState *connectionsState
45+
connectionsState atomic.Pointer[connectionsState]
4646

47+
mu xsync.RWMutex
4748
onApplyDiscoveredEndpoints []func(ctx context.Context, endpoints []endpoint.Info)
4849
}
4950

@@ -158,8 +159,9 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi
158159
endpointsInfo[i] = e
159160
}
160161

162+
b.connectionsState.Store(state)
163+
161164
b.mu.WithLock(func() {
162-
b.connectionsState = state
163165
for _, onApplyDiscoveredEndpoints := range b.onApplyDiscoveredEndpoints {
164166
onApplyDiscoveredEndpoints(ctx, endpointsInfo)
165167
}
@@ -216,8 +218,7 @@ func New(
216218
discoveryClient: internalDiscovery.New(ctx, pool.Get(
217219
endpoint.New(driverConfig.Endpoint()),
218220
), discoveryConfig),
219-
connectionsState: &connectionsState{},
220-
localDCDetector: detectLocalDC,
221+
localDCDetector: detectLocalDC,
221222
}
222223

223224
if config := driverConfig.Balancer(); config == nil {
@@ -319,10 +320,7 @@ func (b *Balancer) wrapCall(ctx context.Context, f func(ctx context.Context, cc
319320
}
320321

321322
func (b *Balancer) connections() *connectionsState {
322-
b.mu.RLock()
323-
defer b.mu.RUnlock()
324-
325-
return b.connectionsState
323+
return b.connectionsState.Load()
326324
}
327325

328326
func (b *Balancer) getConn(ctx context.Context) (c conn.Conn, err error) {

0 commit comments

Comments
 (0)