generated from networkservicemesh/cmd-template
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add safe vpp connection + update vpphelper version
Signed-off-by: NikitaSkrynnik <[email protected]>
- Loading branch information
1 parent
f2bf8fd
commit 4216d3a
Showing
9 changed files
with
89 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2024 Cisco and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package vppinit | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/tools/extend" | ||
"github.com/networkservicemesh/sdk/pkg/tools/log" | ||
"go.fd.io/govpp/api" | ||
) | ||
|
||
type safeVPPConnection struct { | ||
api.Connection | ||
contextTimeout time.Duration | ||
} | ||
|
||
func NewSafeVPPConnection(vppConn api.Connection, contextTimeout time.Duration) api.Connection { | ||
return &safeVPPConnection{ | ||
Connection: vppConn, | ||
contextTimeout: contextTimeout, | ||
} | ||
} | ||
|
||
func (c *safeVPPConnection) Invoke(ctx context.Context, req api.Message, reply api.Message) error { | ||
ctx, cancel := c.ToSafeContext(ctx) | ||
err := c.Connection.Invoke(ctx, req, reply) | ||
cancel() | ||
return err | ||
} | ||
|
||
func (c *safeVPPConnection) ToSafeContext(ctx context.Context) (context.Context, func()) { | ||
deadline, ok := ctx.Deadline() | ||
if !ok { | ||
return ctx, func() {} | ||
} | ||
|
||
minDeadline := time.Now().Add(c.contextTimeout) | ||
if minDeadline.After(deadline) { | ||
deadline = minDeadline | ||
log.FromContext(ctx).Infof("Context deadline has been increased due to important request(s)") | ||
} | ||
postponedCtx, cancel := context.WithDeadline(context.Background(), deadline) | ||
return extend.WithValuesFromContext(postponedCtx, ctx), cancel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters