-
Notifications
You must be signed in to change notification settings - Fork 0
Add graceful connection draining on tunnel close #350
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,110 @@ | ||
| package vpn | ||
|
|
||
| import ( | ||
| "net" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/sagernet/sing-box/common/conntrack" | ||
|
|
||
| "github.com/getlantern/radiance/vpn/ipc" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDrainConnectionsNoConnections(t *testing.T) { | ||
| // With no active connections, drainConnections should return immediately. | ||
| origTimeout := DrainTimeout | ||
| DrainTimeout = 1 * time.Second | ||
| defer func() { DrainTimeout = origTimeout }() | ||
|
|
||
| start := time.Now() | ||
| drainConnections() | ||
| elapsed := time.Since(start) | ||
|
|
||
| assert.Less(t, elapsed, 100*time.Millisecond, "should return immediately with no connections") | ||
| } | ||
|
|
||
| func TestDrainConnectionsWaitsForConnections(t *testing.T) { | ||
| if !conntrack.Enabled { | ||
| t.Skip("conntrack not enabled (need build tag with_conntrack)") | ||
| } | ||
|
|
||
| origTimeout := DrainTimeout | ||
| origPoll := DrainPollInterval | ||
| DrainTimeout = 5 * time.Second | ||
| DrainPollInterval = 10 * time.Millisecond | ||
| defer func() { | ||
| DrainTimeout = origTimeout | ||
| DrainPollInterval = origPoll | ||
| }() | ||
|
|
||
| // Create a tracked connection using conntrack. | ||
| server, client := net.Pipe() | ||
| defer server.Close() | ||
|
|
||
| tracked, err := conntrack.NewConn(client) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 1, conntrack.Count(), "should have 1 tracked connection") | ||
|
|
||
| // Close the connection after a short delay to simulate graceful drain. | ||
| drainDelay := 200 * time.Millisecond | ||
| go func() { | ||
| time.Sleep(drainDelay) | ||
| tracked.Close() | ||
| }() | ||
|
|
||
| start := time.Now() | ||
| drainConnections() | ||
| elapsed := time.Since(start) | ||
|
|
||
| assert.Equal(t, 0, conntrack.Count(), "all connections should be drained") | ||
| assert.GreaterOrEqual(t, elapsed, drainDelay, "should have waited for connection to close") | ||
| assert.Less(t, elapsed, DrainTimeout, "should not have waited the full timeout") | ||
| } | ||
|
|
||
| func TestDrainConnectionsTimeout(t *testing.T) { | ||
| if !conntrack.Enabled { | ||
| t.Skip("conntrack not enabled (need build tag with_conntrack)") | ||
| } | ||
|
|
||
| origTimeout := DrainTimeout | ||
| origPoll := DrainPollInterval | ||
| DrainTimeout = 200 * time.Millisecond | ||
| DrainPollInterval = 10 * time.Millisecond | ||
| defer func() { | ||
| DrainTimeout = origTimeout | ||
| DrainPollInterval = origPoll | ||
| }() | ||
|
|
||
| // Create a tracked connection that never closes. | ||
| server, client := net.Pipe() | ||
| defer server.Close() | ||
|
|
||
| tracked, err := conntrack.NewConn(client) | ||
| require.NoError(t, err) | ||
| defer tracked.Close() | ||
| require.Equal(t, 1, conntrack.Count(), "should have 1 tracked connection") | ||
|
|
||
| start := time.Now() | ||
| drainConnections() | ||
| elapsed := time.Since(start) | ||
|
|
||
| assert.GreaterOrEqual(t, elapsed, DrainTimeout, "should have waited the full timeout") | ||
| assert.Less(t, elapsed, DrainTimeout+100*time.Millisecond, "should not overshoot timeout significantly") | ||
| assert.Equal(t, 1, conntrack.Count(), "connection should still be open after timeout") | ||
| } | ||
|
|
||
| func TestTunnelCloseCallsDrain(t *testing.T) { | ||
| // Verify that tunnel.close() invokes the drain phase by checking that closing a tunnel | ||
| // with no closers and no connections completes quickly. | ||
| tun := &tunnel{} | ||
| tun.status.Store(ipc.Disconnected) | ||
|
|
||
| start := time.Now() | ||
| err := tun.close() | ||
| elapsed := time.Since(start) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Less(t, elapsed, 500*time.Millisecond, "close with no connections should be fast") | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.