Skip to content

Commit

Permalink
fix discoverCandidatesServer.Close
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Uzlov <DanilUzlov@yandex.ru>
  • Loading branch information
d-uzlov committed Jun 20, 2023
1 parent 33db7f2 commit 6c2da84
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions pkg/networkservice/common/discover/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func (d *discoverCandidatesServer) Request(ctx context.Context, request *network
}

func (d *discoverCandidatesServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
// Unlike Request, Close method should always call next element in chain
// to make sure we clear resources in the current app.

logger := log.FromContext(ctx).WithField("discoverCandidatesServer", "Close")

if clienturlctx.ClientURL(ctx) != nil {
Expand All @@ -89,19 +92,20 @@ func (d *discoverCandidatesServer) Close(ctx context.Context, conn *networkservi

nseName := conn.GetNetworkServiceEndpointName()
if nseName == "" {
// If it's an existing connection, the NSE name should be set. Otherwise, it's probably an API misuse.
return nil, errors.Errorf("network_service_endpoint_name is not set")
logger.Error("network_service_endpoint_name is not set")
return next.Server(ctx).Close(ctx, conn)
}

var u *url.URL
nse, err := d.discoverNetworkServiceEndpoint(ctx, nseName)
if err != nil {
logger.Errorf("endpoint is not found: %v: %v", nseName, err)
return next.Server(ctx).Close(ctx, conn)
}

if nse, err := d.discoverNetworkServiceEndpoint(ctx, nseName); err == nil {
u, err = url.Parse(nse.Url)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse url %s", nse.Url)
}
} else {
logger.Errorf("no endpoint found for Close: %v", conn)
u, err := url.Parse(nse.Url)
if err != nil {
logger.Errorf("failed to parse url: %s: %v", nse.Url, err)
return next.Server(ctx).Close(ctx, conn)
}

return next.Server(ctx).Close(clienturlctx.WithClientURL(ctx, u), conn)
Expand Down

0 comments on commit 6c2da84

Please sign in to comment.