Skip to content

Commit

Permalink
pinhole: fix behavior in case of creation error (#762)
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <[email protected]>
  • Loading branch information
glazychev-art authored Oct 24, 2023
1 parent e728499 commit af5c232
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
18 changes: 13 additions & 5 deletions pkg/networkservice/pinhole/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,28 @@ func (v *pinholeClient) Request(ctx context.Context, request *networkservice.Net
if key == nil {
continue
}
if _, ok := v.ipPortMap.LoadOrStore(*key, struct{}{}); !ok {
// Check if this ACL rule has been added
if _, ok := v.ipPortMap.Load(*key); !ok {
var err error

v.mutex.Lock()
if err := create(ctx, v.vppConn, key.IP(), key.Port(), fmt.Sprintf("%s port %d", aclTag, key.port)); err != nil {
// Double check after mutex
if _, ok := v.ipPortMap.Load(*key); !ok {
if err = create(ctx, v.vppConn, key.IP(), key.Port(), fmt.Sprintf("%s port %d", aclTag, key.port)); err == nil {
v.ipPortMap.Store(*key, struct{}{})
}
}
v.mutex.Unlock()

if err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

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

v.mutex.Unlock()
return nil, err
}
v.mutex.Unlock()
}
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/networkservice/pinhole/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,28 @@ func (v *pinholeServer) Request(ctx context.Context, request *networkservice.Net
if key == nil {
continue
}
if _, ok := v.ipPortMap.LoadOrStore(*key, struct{}{}); !ok {
// Check if this ACL rule has been added
if _, ok := v.ipPortMap.Load(*key); !ok {
var err error

v.mutex.Lock()
if err := create(ctx, v.vppConn, key.IP(), key.Port(), fmt.Sprintf("%s port %d", aclTag, key.port)); err != nil {
// Double check after mutex
if _, ok := v.ipPortMap.Load(*key); !ok {
if err = create(ctx, v.vppConn, key.IP(), key.Port(), fmt.Sprintf("%s port %d", aclTag, key.port)); err == nil {
v.ipPortMap.Store(*key, struct{}{})
}
}
v.mutex.Unlock()

if err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

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

v.mutex.Unlock()
return nil, err
}
v.mutex.Unlock()
}
}

Expand Down

0 comments on commit af5c232

Please sign in to comment.