Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monitoring for original context in extendedConnection struct #10

Merged
merged 3 commits into from
Dec 6, 2024

Conversation

NikitaSkrynnik
Copy link

@NikitaSkrynnik NikitaSkrynnik commented Dec 5, 2024

Description

Add monitoring of original context canceling in extendedConnection

Issue

networkservicemesh/cmd-forwarder-vpp#1133

Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com>
Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com>
Copy link
Member

@denis-tingaikin denis-tingaikin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify it. Thoughts?

diff --git a/extendtimeout/connection.go b/extendtimeout/connection.go
index 288a872..e34a6f4 100644
--- a/extendtimeout/connection.go
+++ b/extendtimeout/connection.go
@@ -21,7 +21,6 @@ import (
 	"context"
 	"time"
 
-	"github.com/edwarnicke/log"
 	"go.fd.io/govpp/api"
 )
 
@@ -48,27 +47,26 @@ func NewConnection(vppConn api.Connection, contextTimeout time.Duration) api.Con
 }
 
 func (c *extendedConnection) Invoke(ctx context.Context, req, reply api.Message) error {
-	ctx, cancel := c.withExtendedTimeoutCtx(ctx)
+	ctx, cancel := c.withExtenedTimeoutContext(ctx)
 	err := c.Connection.Invoke(ctx, req, reply)
 	cancel()
 	return err
 }
 
-func (c *extendedConnection) withExtendedTimeoutCtx(ctx context.Context) (extendedCtx context.Context, cancel func()) {
-	deadline, ok := ctx.Deadline()
-	if !ok {
-		return ctx, func() {}
-	}
+func(c *extendedConnection) withExtenedTimeoutContext(ctx context.Context) (context.Context, context.CancelFunc) {
+	var cancelContext, cancel = context.WithCancel(context.Background())
+	var timeoutContext, timeoutCancel = context.WithTimeout(cancelContext, c.contextTimeout)
+	go func() {
+		<-timeoutContext.Done()
+		timeoutCancel()
+		select {
+		case <-cancelContext.Done():
+			return
+		case <-ctx.Done():
+			cancel()
+			return
+		}
+	}()
 
-	minDeadline := time.Now().Add(c.contextTimeout)
-	if minDeadline.Before(deadline) {
-		return ctx, func() {}
-	}
-	log.Entry(ctx).Warnf("Context deadline has been extended by extendtimeout from %v to %v", deadline, minDeadline)
-	deadline = minDeadline
-	postponedCtx, cancel := context.WithDeadline(context.Background(), deadline)
-	return &extendedContext{
-		Context:       postponedCtx,
-		valuesContext: ctx,
-	}, cancel
+	return cancelContext, cancel
 }
diff --git a/extendtimeout/connection_test.go b/extendtimeout/connection_test.go
index 4d0025e..7af4f30 100644
--- a/extendtimeout/connection_test.go
+++ b/extendtimeout/connection_test.go
@@ -39,44 +39,6 @@ func (c *testConn) Invoke(ctx context.Context, req, reply api.Message) error {
 	return nil
 }
 
-type key struct{}
-
-const value = "value"
-
-func TestSmallTimeout(t *testing.T) {
-	testConn := &testConn{invokeBody: func(ctx context.Context) {
-		deadline, ok := ctx.Deadline()
-		require.True(t, ok)
-		timeout := time.Until(deadline)
-		require.Greater(t, timeout, time.Second)
-		require.Equal(t, ctx.Value(&key{}), value)
-	}}
-
-	smallCtx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10)
-	smallCtx = context.WithValue(smallCtx, &key{}, value)
-	defer cancel()
-
-	err := extendtimeout.NewConnection(testConn, 2*time.Second).Invoke(smallCtx, nil, nil)
-	require.NoError(t, err)
-}
-
-func TestBigTimeout(t *testing.T) {
-	testConn := &testConn{invokeBody: func(ctx context.Context) {
-		deadline, ok := ctx.Deadline()
-		require.True(t, ok)
-		timeout := time.Until(deadline)
-		require.Greater(t, timeout, 7*time.Second)
-		require.Equal(t, ctx.Value(&key{}), value)
-	}}
-
-	bigCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
-	bigCtx = context.WithValue(bigCtx, &key{}, value)
-	defer cancel()
-
-	err := extendtimeout.NewConnection(testConn, 2*time.Second).Invoke(bigCtx, nil, nil)
-	require.NoError(t, err)
-}
-
 func TestNoTimeout(t *testing.T) {
 	testConn := &testConn{invokeBody: func(ctx context.Context) {
 		_, ok := ctx.Deadline()

Signed-off-by: NikitaSkrynnik <nikita.skrynnik@xored.com>
@denis-tingaikin denis-tingaikin merged commit 6f75d67 into networkservicemesh:main Dec 6, 2024
8 checks passed
nsmbot pushed a commit to networkservicemesh/cmd-nsc-vpp that referenced this pull request Dec 6, 2024
…phelper@main

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-forwarder-vpp that referenced this pull request Dec 6, 2024
…phelper@main

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nse-simple-vl3-docker that referenced this pull request Dec 6, 2024
…phelper@main

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nse-icmp-responder-vpp that referenced this pull request Dec 6, 2024
…phelper@main

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nse-firewall-vpp that referenced this pull request Dec 6, 2024
…phelper@main

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nsc-simple-docker that referenced this pull request Dec 6, 2024
…phelper@main

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nse-vl3-vpp that referenced this pull request Dec 6, 2024
…phelper@main

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nse-vlan-vpp that referenced this pull request Dec 6, 2024
…phelper@main

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot added a commit to networkservicemesh/cmd-nsc-vpp that referenced this pull request Dec 6, 2024
…phelper@main (#738)

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
Co-authored-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/deployments-k8s that referenced this pull request Dec 6, 2024
…d-nsc-vpp@main

PR link: networkservicemesh/cmd-nsc-vpp#738

Commit: 25125e9
Author: Network Service Mesh Bot
Date: 2024-12-06 04:21:23 -0600
Message:
  - Update go.mod and go.sum to latest version from networkservicemesh/vpphelper@main (#738)
PR link: networkservicemesh/vpphelper#10
Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
    - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot added a commit to networkservicemesh/cmd-nse-icmp-responder-vpp that referenced this pull request Dec 6, 2024
…phelper@main (#721)

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
Co-authored-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot added a commit to networkservicemesh/cmd-nse-firewall-vpp that referenced this pull request Dec 6, 2024
…phelper@main (#556)

PR link: networkservicemesh/vpphelper#10

Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
  - Merge pull request #10 from NikitaSkrynnik/extended

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
Co-authored-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/deployments-k8s that referenced this pull request Dec 6, 2024
…d-nse-icmp-responder-vpp@main

PR link: networkservicemesh/cmd-nse-icmp-responder-vpp#721

Commit: 932b2b0
Author: Network Service Mesh Bot
Date: 2024-12-06 04:21:40 -0600
Message:
  - Update go.mod and go.sum to latest version from networkservicemesh/vpphelper@main (#721)
PR link: networkservicemesh/vpphelper#10
Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
    - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/deployments-k8s that referenced this pull request Dec 6, 2024
…d-nse-firewall-vpp@main

PR link: networkservicemesh/cmd-nse-firewall-vpp#556

Commit: 4880cdd
Author: Network Service Mesh Bot
Date: 2024-12-06 04:21:48 -0600
Message:
  - Update go.mod and go.sum to latest version from networkservicemesh/vpphelper@main (#556)
PR link: networkservicemesh/vpphelper#10
Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
    - Merge pull request #10 from NikitaSkrynnik/extended
Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot added a commit to networkservicemesh/deployments-k8s that referenced this pull request Dec 6, 2024
…d-nsc-vpp@main (#12584)

PR link: networkservicemesh/cmd-nsc-vpp#738

Commit: 25125e9
Author: Network Service Mesh Bot
Date: 2024-12-06 04:21:23 -0600
Message:
  - Update go.mod and go.sum to latest version from networkservicemesh/vpphelper@main (#738)
PR link: networkservicemesh/vpphelper#10
Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
    - Merge pull request #10 from NikitaSkrynnik/extended

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
Co-authored-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot added a commit to networkservicemesh/deployments-k8s that referenced this pull request Dec 6, 2024
…d-nse-icmp-responder-vpp@main (#12562)

PR link: networkservicemesh/cmd-nse-icmp-responder-vpp#721

Commit: 932b2b0
Author: Network Service Mesh Bot
Date: 2024-12-06 04:21:40 -0600
Message:
  - Update go.mod and go.sum to latest version from networkservicemesh/vpphelper@main (#721)
PR link: networkservicemesh/vpphelper#10
Commit: 6f75d67
Author: Denis Tingaikin
Date: 2024-12-06 05:18:38 -0500
Message:
    - Merge pull request #10 from NikitaSkrynnik/extended

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
Co-authored-by: NSMBot <nsmbot@networkservicmesh.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants