Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dedicaded close message for internal balancer closed #1614

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion internal/balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)

var ErrNoEndpoints = xerrors.Wrap(fmt.Errorf("no endpoints"))
var (
ErrNoEndpoints = xerrors.Wrap(fmt.Errorf("no endpoints"))
errBalancerClosed = xerrors.Wrap(fmt.Errorf("internal ydb sdk balancer closed"))
)

type Balancer struct {
driverConfig *config.Config
Expand All @@ -40,6 +43,7 @@ type Balancer struct {
localDCDetector func(ctx context.Context, endpoints []endpoint.Endpoint) (string, error)

connectionsState atomic.Pointer[connectionsState]
closed atomic.Bool
}

func (b *Balancer) clusterDiscovery(ctx context.Context) (err error) {
Expand Down Expand Up @@ -152,6 +156,8 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi
}

func (b *Balancer) Close(ctx context.Context) (err error) {
b.closed.Store(true)

onDone := trace.DriverOnBalancerClose(
b.driverConfig.Trace(), &ctx,
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer.(*Balancer).Close"),
Expand Down Expand Up @@ -281,6 +287,10 @@ func (b *Balancer) Invoke(
reply interface{},
opts ...grpc.CallOption,
) error {
if b.closed.Load() {
return xerrors.WithStackTrace(errBalancerClosed)
}

return b.wrapCall(ctx, func(ctx context.Context, cc conn.Conn) error {
return cc.Invoke(ctx, method, args, reply, opts...)
})
Expand All @@ -292,6 +302,10 @@ func (b *Balancer) NewStream(
method string,
opts ...grpc.CallOption,
) (_ grpc.ClientStream, err error) {
if b.closed.Load() {
return nil, xerrors.WithStackTrace(errBalancerClosed)
}

var client grpc.ClientStream
err = b.wrapCall(ctx, func(ctx context.Context, cc conn.Conn) error {
client, err = cc.NewStream(ctx, desc, method, opts...)
Expand Down Expand Up @@ -345,6 +359,10 @@ func (b *Balancer) connections() *connectionsState {
}

func (b *Balancer) getConn(ctx context.Context) (c conn.Conn, err error) {
if b.closed.Load() {
return nil, xerrors.WithStackTrace(errBalancerClosed)
}

onDone := trace.DriverOnBalancerChooseEndpoint(
b.driverConfig.Trace(), &ctx,
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer.(*Balancer).getConn"),
Expand Down
Loading