mirror of
https://github.com/MetaCubeX/Clash.Meta.git
synced 2025-04-16 07:20:58 +00:00
fix: SetupContextForConn should return context error to user
This commit is contained in:
parent
bfd06ebad0
commit
dbb5b7db1c
3 changed files with 117 additions and 3 deletions
|
@ -7,7 +7,18 @@ import (
|
|||
"github.com/metacubex/mihomo/common/contextutils"
|
||||
)
|
||||
|
||||
// SetupContextForConn is a helper function that starts connection I/O interrupter goroutine.
|
||||
// SetupContextForConn is a helper function that starts connection I/O interrupter.
|
||||
// if ctx be canceled before done called, it will close the connection.
|
||||
// should use like this:
|
||||
//
|
||||
// func streamConn(ctx context.Context, conn net.Conn) (_ net.Conn, err error) {
|
||||
// if ctx.Done() != nil {
|
||||
// done := N.SetupContextForConn(ctx, conn)
|
||||
// defer done(&err)
|
||||
// }
|
||||
// conn, err := xxx
|
||||
// return conn, err
|
||||
// }
|
||||
func SetupContextForConn(ctx context.Context, conn net.Conn) (done func(*error)) {
|
||||
stopc := make(chan struct{})
|
||||
stop := contextutils.AfterFunc(ctx, func() {
|
||||
|
@ -21,7 +32,7 @@ func SetupContextForConn(ctx context.Context, conn net.Conn) (done func(*error))
|
|||
<-stopc
|
||||
if ctxErr := ctx.Err(); ctxErr != nil && inputErr != nil {
|
||||
// Return context error to user.
|
||||
inputErr = &ctxErr
|
||||
*inputErr = ctxErr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
103
common/net/context_test.go
Normal file
103
common/net/context_test.go
Normal file
|
@ -0,0 +1,103 @@
|
|||
package net_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
N "github.com/metacubex/mihomo/common/net"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func testRead(ctx context.Context, conn net.Conn) (err error) {
|
||||
if ctx.Done() != nil {
|
||||
done := N.SetupContextForConn(ctx, conn)
|
||||
defer done(&err)
|
||||
}
|
||||
_, err = conn.Read(make([]byte, 1))
|
||||
return err
|
||||
}
|
||||
|
||||
func TestSetupContextForConnWithCancel(t *testing.T) {
|
||||
t.Parallel()
|
||||
c1, c2 := N.Pipe()
|
||||
defer c1.Close()
|
||||
defer c2.Close()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
errc := make(chan error)
|
||||
go func() {
|
||||
errc <- testRead(ctx, c1)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-errc:
|
||||
t.Fatal("conn closed before cancel")
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
cancel()
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-errc:
|
||||
assert.ErrorIs(t, err, context.Canceled)
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
t.Fatal("conn not be canceled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupContextForConnWithTimeout1(t *testing.T) {
|
||||
t.Parallel()
|
||||
c1, c2 := N.Pipe()
|
||||
defer c1.Close()
|
||||
defer c2.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
errc := make(chan error)
|
||||
go func() {
|
||||
errc <- testRead(ctx, c1)
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-errc:
|
||||
if !errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
t.Fatal("conn closed before timeout")
|
||||
}
|
||||
assert.ErrorIs(t, err, context.DeadlineExceeded)
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
t.Fatal("conn not be canceled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupContextForConnWithTimeout2(t *testing.T) {
|
||||
t.Parallel()
|
||||
c1, c2 := N.Pipe()
|
||||
defer c1.Close()
|
||||
defer c2.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
errc := make(chan error)
|
||||
go func() {
|
||||
errc <- testRead(ctx, c1)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-errc:
|
||||
t.Fatal("conn closed before cancel")
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
c2.Write(make([]byte, 1))
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-errc:
|
||||
assert.Nil(t, ctx.Err())
|
||||
assert.Nil(t, err)
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
t.Fatal("conn not be canceled")
|
||||
}
|
||||
}
|
|
@ -326,7 +326,7 @@ func streamWebsocketWithEarlyDataConn(conn net.Conn, c *WebsocketConfig) (net.Co
|
|||
return N.NewDeadlineConn(conn), nil
|
||||
}
|
||||
|
||||
func streamWebsocketConn(ctx context.Context, conn net.Conn, c *WebsocketConfig, earlyData *bytes.Buffer) (net.Conn, error) {
|
||||
func streamWebsocketConn(ctx context.Context, conn net.Conn, c *WebsocketConfig, earlyData *bytes.Buffer) (_ net.Conn, err error) {
|
||||
u, err := url.Parse(c.Path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse url %s error: %w", c.Path, err)
|
||||
|
|
Loading…
Add table
Reference in a new issue