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

fix: stop resource leaking in client connections on find queries #1228

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
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
28 changes: 15 additions & 13 deletions pkg/registry/common/dial/ns_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func (c *dialNSClient) Unregister(ctx context.Context, in *registry.NetworkServi

type dialNSFindClient struct {
registry.NetworkServiceRegistry_FindClient
closeFn func()
cleanupFn func()
}

func (c *dialNSFindClient) Recv() (*registry.NetworkServiceResponse, error) {
resp, err := c.NetworkServiceRegistry_FindClient.Recv()
if err != nil {
c.closeFn()
c.cleanupFn()
}
return resp, err
}
Expand All @@ -130,31 +130,33 @@ func (c *dialNSClient) Find(ctx context.Context, in *registry.NetworkServiceQuer

di := newDialer(c.chainCtx, c.dialTimeout, c.dialOptions...)

findCtx, cancel := context.WithCancel(ctx)

err := di.Dial(findCtx, clientURL)
err := di.Dial(ctx, clientURL)
if err != nil {
log.FromContext(ctx).Errorf("can not dial to %v, err %v. Deleting clientconn...", grpcutils.URLToTarget(clientURL), err)
cancel()
return nil, err
}

clientconn.Store(ctx, di)
defer clientconn.Delete(ctx)

cleanupFn := func() {
clientconn.Delete(ctx)
_ = di.Close()
}

resp, err := next.NetworkServiceRegistryClient(ctx).Find(ctx, in, opts...)
if err != nil {
_ = di.Close()
cancel()
cleanupFn()
return nil, err
}

go func() {
<-resp.Context().Done()
cleanupFn()
}()

return &dialNSFindClient{
NetworkServiceRegistry_FindClient: resp,
closeFn: func() {
cancel()
_ = di.Close()
},
cleanupFn: cleanupFn,
}, nil
}

Expand Down
28 changes: 15 additions & 13 deletions pkg/registry/common/dial/nse_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func (c *dialNSEClient) Unregister(ctx context.Context, in *registry.NetworkServ

type dialNSEFindClient struct {
registry.NetworkServiceEndpointRegistry_FindClient
closeFn func()
cleanupFn func()
}

func (c *dialNSEFindClient) Recv() (*registry.NetworkServiceEndpointResponse, error) {
resp, err := c.NetworkServiceEndpointRegistry_FindClient.Recv()
if err != nil {
c.closeFn()
c.cleanupFn()
}
return resp, err
}
Expand All @@ -130,31 +130,33 @@ func (c *dialNSEClient) Find(ctx context.Context, in *registry.NetworkServiceEnd

di := newDialer(c.chainCtx, c.dialTimeout, c.dialOptions...)

findCtx, cancel := context.WithCancel(ctx)

err := di.Dial(findCtx, clientURL)
err := di.Dial(ctx, clientURL)
if err != nil {
log.FromContext(ctx).Errorf("can not dial to %v, err %v. Deleting clientconn...", grpcutils.URLToTarget(clientURL), err)
cancel()
return nil, err
}

clientconn.Store(ctx, di)
defer clientconn.Delete(ctx)

cleanupFn := func() {
clientconn.Delete(ctx)
_ = di.Close()
}

resp, err := next.NetworkServiceEndpointRegistryClient(ctx).Find(ctx, in, opts...)
if err != nil {
_ = di.Close()
cancel()
cleanupFn()
return nil, err
}

go func() {
<-resp.Context().Done()
cleanupFn()
}()

return &dialNSEFindClient{
NetworkServiceEndpointRegistry_FindClient: resp,
closeFn: func() {
cancel()
_ = di.Close()
},
cleanupFn: cleanupFn,
}, nil
}

Expand Down