Skip to content

Commit 5d47eed

Browse files
committed
close dropped connection on balancer.Update()
1 parent 3074ad5 commit 5d47eed

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

Diff for: internal/balancer/balancer.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"sync"
78
"sync/atomic"
89

910
"github.com/ydb-platform/ydb-go-genproto/Ydb_Discovery_V1"
@@ -127,10 +128,12 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi
127128
)
128129
previous = b.connections().All()
129130
)
131+
132+
_, added, dropped := xslices.Diff(previous, newest, func(lhs, rhs endpoint.Endpoint) int {
133+
return strings.Compare(lhs.Address(), rhs.Address())
134+
})
135+
130136
defer func() {
131-
_, added, dropped := xslices.Diff(previous, newest, func(lhs, rhs endpoint.Endpoint) int {
132-
return strings.Compare(lhs.Address(), rhs.Address())
133-
})
134137
onDone(
135138
xslices.Transform(newest, func(t endpoint.Endpoint) trace.EndpointInfo { return t }),
136139
xslices.Transform(added, func(t endpoint.Endpoint) trace.EndpointInfo { return t }),
@@ -145,6 +148,14 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi
145148
c.Endpoint().Touch()
146149
}
147150

151+
droppedConnections := endpointsToConnections(b.pool, dropped)
152+
for _, c := range droppedConnections {
153+
sync.OnceFunc(func() {
154+
b.pool.Ban(ctx, c, errEndpointNotDiscovered)
155+
_ = c.Close(ctx)
156+
})()
157+
}
158+
148159
info := balancerConfig.Info{SelfLocation: localDC}
149160
state := newConnectionsState(connections, b.balancerConfig.Filter, info, b.balancerConfig.AllowFallback)
150161

Diff for: internal/balancer/errors.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package balancer
2+
3+
import (
4+
"errors"
5+
)
6+
7+
var (
8+
errEndpointNotDiscovered = errors.New("endpoint is no longer discovered")
9+
)

Diff for: internal/conn/conn.go

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package conn
33
import (
44
"context"
55
"fmt"
6+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/closer"
67
"sync"
78
"sync/atomic"
89
"time"
@@ -36,6 +37,7 @@ var (
3637

3738
type Conn interface {
3839
grpc.ClientConnInterface
40+
closer.Closer
3941

4042
Endpoint() endpoint.Endpoint
4143

0 commit comments

Comments
 (0)