Skip to content

Commit

Permalink
Rework closectx to use context.Background()
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 6e677ac commit cfcfbe4
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 13 deletions.
6 changes: 2 additions & 4 deletions pkg/networkservice/common/authorize/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ import (
)

type authorizeClient struct {
ctx context.Context
policies policiesList
serverPeer atomic.Value
}

// NewClient - returns a new authorization networkservicemesh.NetworkServiceClient
func NewClient(ctx context.Context, opts ...Option) networkservice.NetworkServiceClient {
func NewClient(opts ...Option) networkservice.NetworkServiceClient {
var result = &authorizeClient{
ctx: ctx,
policies: defaultPolicies(),
}

Expand All @@ -68,7 +66,7 @@ func (a *authorizeClient) Request(ctx context.Context, request *networkservice.N
}

if err = a.policies.check(ctx, conn); err != nil {
closeCtx, cancelClose := closectx.New(a.ctx, ctx)
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := next.Client(ctx).Close(closeCtx, conn, opts...); closeErr != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/common/connect/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *connectServer) Request(ctx context.Context, request *networkservice.Net
conn, err = next.Server(ctx).Request(ctx, request)
// Close connection if next.Server Request finished with error
if err != nil && !refreshRequest {
closeCtx, cancelClose := closectx.New(s.ctx, ctx)
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

_, closeErr := s.Close(closeCtx, request.Connection.Clone())
Expand Down Expand Up @@ -158,7 +158,7 @@ func (s *connectServer) client(ctx context.Context, conn *networkservice.Connect
return connInfo.client
}

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

// For some reason we have changed the clientURL, so we need to close and delete the existing client.
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/heal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (u *healClient) Request(ctx context.Context, request *networkservice.Networ
err = errors.Errorf("timeout waiting for connection monitor: %s", conn.GetId())
}
if err != nil {
closeCtx, cancelClose := closectx.New(u.ctx, ctx)
closeCtx, cancelClose := closectx.New(ctx)
defer cancelClose()

if _, closeErr := next.Client(ctx).Close(closeCtx, conn); closeErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/common/refresh/nse_registry_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *refreshNSEClient) Register(ctx context.Context, nse *registry.NetworkSe

cancel, err = c.startRefresh(ctx, refreshNSE, expirationDuration)
if err != nil {
unregisterCtx, cancelUnregister := closectx.New(c.ctx, ctx)
unregisterCtx, cancelUnregister := closectx.New(ctx)
defer cancelUnregister()

if _, unregisterErr := next.NetworkServiceEndpointRegistryServer(ctx).Unregister(unregisterCtx, reg); unregisterErr != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/tools/closectx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ import (
const closeTimeout = 15 * time.Second

// New creates a new context for Close/Unregister in case of Request/Register failure
func New(parent, valuesCtx context.Context) (ctx context.Context, cancel context.CancelFunc) {
clockTime := clock.FromContext(valuesCtx)
func New(ctx context.Context) (context.Context, context.CancelFunc) {
clockTime := clock.FromContext(ctx)

var timeout time.Duration
if deadline, ok := valuesCtx.Deadline(); ok {
if deadline, ok := ctx.Deadline(); ok {
timeout = clockTime.Until(deadline)
}
if timeout < closeTimeout {
timeout = closeTimeout
}

ctx = extend.WithValuesFromContext(parent, valuesCtx)
ctx = extend.WithValuesFromContext(context.Background(), ctx)

return clockTime.WithTimeout(ctx, timeout)
}
2 changes: 1 addition & 1 deletion pkg/tools/sandbox/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (n *Node) NewClient(
CloneURL(n.NSMgr.URL),
client.WithDialOptions(DefaultDialOptions(generatorFunc)...),
client.WithDialTimeout(DialTimeout),
client.WithAuthorizeClient(authorize.NewClient(ctx, authorize.Any())),
client.WithAuthorizeClient(authorize.NewClient(authorize.Any())),
client.WithAdditionalFunctionality(additionalFunctionality...),
)
}

0 comments on commit cfcfbe4

Please sign in to comment.