Skip to content

Commit

Permalink
Rework Close on failure to use closectx
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov committed Jul 26, 2021
1 parent 744390b commit 2c4f082
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,9 +26,12 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"google.golang.org/grpc"
"github.com/networkservicemesh/sdk/pkg/tools/closectx"

"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)
Expand Down Expand Up @@ -65,10 +70,18 @@ func (i *ipaddressClient) Request(ctx context.Context, request *networkservice.N
if err != nil {
return nil, err
}

if err := create(ctx, conn, metadata.IsClient(i)); err != nil {
_, _ = i.Close(ctx, conn, opts...)
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

return conn, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,8 +26,11 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/closectx"

"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)
Expand Down Expand Up @@ -65,10 +70,18 @@ func (i *ipaddressServer) Request(ctx context.Context, request *networkservice.N
if err != nil {
return nil, err
}

if err := create(ctx, conn, metadata.IsClient(i)); err != nil {
_, _ = i.Close(ctx, conn)
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := i.Close(closeCtx, conn); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

return conn, nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,9 +24,12 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"google.golang.org/grpc"
"github.com/networkservicemesh/sdk/pkg/tools/closectx"
)

type ipNeighborsClient struct{}
Expand All @@ -38,10 +43,18 @@ func (i *ipNeighborsClient) Request(ctx context.Context, request *networkservice
if err != nil {
return nil, err
}

if err := create(ctx, conn); err != nil {
_, _ = i.Close(ctx, conn, opts...)
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

return conn, nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,8 +24,11 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/closectx"
)

type ipNeighborsServer struct {
Expand All @@ -38,10 +43,18 @@ func (i *ipNeighborsServer) Request(ctx context.Context, request *networkservice
if err != nil {
return nil, err
}

if err := create(ctx, conn); err != nil {
_, _ = i.Close(ctx, conn)
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := i.Close(closeCtx, conn); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

return conn, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,11 +26,13 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/closectx"
)

type routesClient struct{}
Expand Down Expand Up @@ -64,10 +68,18 @@ func (i *routesClient) Request(ctx context.Context, request *networkservice.Netw
if err != nil {
return nil, err
}

if err := create(ctx, conn, metadata.IsClient(i)); err != nil {
_, _ = i.Close(ctx, conn, opts...)
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

return conn, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,10 +26,12 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"

"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/closectx"
)

type routesServer struct {
Expand Down Expand Up @@ -64,10 +68,18 @@ func (i *routesServer) Request(ctx context.Context, request *networkservice.Netw
if err != nil {
return nil, err
}

if err := create(ctx, conn, metadata.IsClient(i)); err != nil {
_, _ = i.Close(ctx, conn)
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := i.Close(closeCtx, conn); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

return conn, nil
}

Expand Down
22 changes: 19 additions & 3 deletions pkg/kernel/networkservice/connectioncontextkernel/mtu/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,10 +26,13 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/closectx"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"google.golang.org/grpc"
)

type mtuClient struct{}
Expand Down Expand Up @@ -60,15 +65,26 @@ func NewClient() networkservice.NetworkServiceClient {
}

func (m *mtuClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
logger := log.FromContext(ctx).WithField("mtuClient", "Request")

conn, err := next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
return nil, err
}

if err := setMTU(ctx, conn); err != nil {
log.FromContext(ctx).Debugf("about to Close due to error: %+v", err)
_, _ = m.Close(ctx, conn, opts...)
logger.Debugf("about to Close due to error: %s", err.Error())

closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := m.Close(closeCtx, conn, opts...); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

return conn, nil
}

Expand Down
19 changes: 17 additions & 2 deletions pkg/kernel/networkservice/connectioncontextkernel/mtu/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,8 +26,11 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/closectx"
"github.com/networkservicemesh/sdk/pkg/tools/log"
)

Expand Down Expand Up @@ -60,13 +65,23 @@ func NewServer() networkservice.NetworkServiceServer {
}

func (m *mtuServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
logger := log.FromContext(ctx).WithField("mutServer", "Request")

conn, err := next.Server(ctx).Request(ctx, request)
if err != nil {
return nil, err
}

if err := setMTU(ctx, conn); err != nil {
log.FromContext(ctx).Debugf("about to Close due to error: %+v", err)
_, _ = m.Close(ctx, conn)
logger.Debugf("about to Close due to error: %s", err.Error())

closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := m.Close(closeCtx, conn); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}
return conn, nil
Expand Down
19 changes: 16 additions & 3 deletions pkg/kernel/networkservice/ethernetcontext/vf_client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,9 +22,12 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"google.golang.org/grpc"
"github.com/networkservicemesh/sdk/pkg/tools/closectx"

"github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/vfconfig"
)
Expand All @@ -39,12 +44,20 @@ func (i *vfEthernetClient) Request(ctx context.Context, request *networkservice.
if err != nil {
return nil, err
}

if vfConfig := vfconfig.Config(ctx); vfConfig != nil {
err := vfCreate(vfConfig, conn, true)
if err != nil {
if err := vfCreate(vfConfig, conn, true); err != nil {
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}
}

return conn, nil
}

Expand Down

0 comments on commit 2c4f082

Please sign in to comment.