without cancel the context
This commit is contained in:
@@ -97,7 +97,7 @@ func (c *http2Connector) Connect(ctx context.Context, conn net.Conn, network, ad
|
|||||||
defer conn.SetDeadline(time.Time{})
|
defer conn.SetDeadline(time.Time{})
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Do(req.WithContext(ictx.Copy(ctx)))
|
resp, err := client.Do(req.WithContext(context.WithoutCancel(ctx)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import (
|
|||||||
"github.com/go-gost/core/logger"
|
"github.com/go-gost/core/logger"
|
||||||
md "github.com/go-gost/core/metadata"
|
md "github.com/go-gost/core/metadata"
|
||||||
xctx "github.com/go-gost/x/ctx"
|
xctx "github.com/go-gost/x/ctx"
|
||||||
ictx "github.com/go-gost/x/internal/ctx"
|
|
||||||
"github.com/go-gost/x/internal/net/proxyproto"
|
"github.com/go-gost/x/internal/net/proxyproto"
|
||||||
"github.com/go-gost/x/registry"
|
"github.com/go-gost/x/registry"
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
@@ -170,7 +169,7 @@ func (d *h2Dialer) Dial(ctx context.Context, address string, opts ...dialer.Dial
|
|||||||
d.logger.Trace(string(dump))
|
d.logger.Trace(string(dump))
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Do(req.WithContext(ictx.Copy(ctx)))
|
resp, err := client.Do(req.WithContext(context.WithoutCancel(ctx)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"github.com/go-gost/core/logger"
|
"github.com/go-gost/core/logger"
|
||||||
"github.com/go-gost/core/metadata"
|
"github.com/go-gost/core/metadata"
|
||||||
xctx "github.com/go-gost/x/ctx"
|
|
||||||
xrecorder "github.com/go-gost/x/recorder"
|
xrecorder "github.com/go-gost/x/recorder"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -53,28 +52,3 @@ func RecorderObjectFromContext(ctx context.Context) *xrecorder.HandlerRecorderOb
|
|||||||
v, _ := ctx.Value(recorderObjectCtxKey{}).(*xrecorder.HandlerRecorderObject)
|
v, _ := ctx.Value(recorderObjectCtxKey{}).(*xrecorder.HandlerRecorderObject)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
func Copy(ctx context.Context) context.Context {
|
|
||||||
if ctx == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx2 := context.Background()
|
|
||||||
if v := xctx.SrcAddrFromContext(ctx); v != nil {
|
|
||||||
ctx2 = xctx.ContextWithSrcAddr(ctx2, v)
|
|
||||||
}
|
|
||||||
if v := xctx.DstAddrFromContext(ctx); v != nil {
|
|
||||||
ctx2 = xctx.ContextWithDstAddr(ctx2, v)
|
|
||||||
}
|
|
||||||
if v := xctx.SidFromContext(ctx); v != "" {
|
|
||||||
ctx2 = xctx.ContextWithSid(ctx2, v)
|
|
||||||
}
|
|
||||||
if v := xctx.ClientIDFromContext(ctx); v != "" {
|
|
||||||
ctx2 = xctx.ContextWithClientID(ctx2, v)
|
|
||||||
}
|
|
||||||
if v := MetadataFromContext(ctx); v != nil {
|
|
||||||
ctx2 = ContextWithMetadata(ctx2, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctx2
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import (
|
|||||||
type conn struct {
|
type conn struct {
|
||||||
laddr net.Addr
|
laddr net.Addr
|
||||||
raddr net.Addr
|
raddr net.Addr
|
||||||
closed chan struct{}
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) Read(b []byte) (n int, err error) {
|
func (c *conn) Read(b []byte) (n int, err error) {
|
||||||
@@ -24,10 +24,8 @@ func (c *conn) Write(b []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) Close() error {
|
func (c *conn) Close() error {
|
||||||
select {
|
if c.cancel != nil {
|
||||||
case <-c.closed:
|
c.cancel()
|
||||||
default:
|
|
||||||
close(c.closed)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -52,10 +50,6 @@ func (c *conn) SetWriteDeadline(t time.Time) error {
|
|||||||
return &net.OpError{Op: "set", Net: "http2", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
return &net.OpError{Op: "set", Net: "http2", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) Done() <-chan struct{} {
|
|
||||||
return c.closed
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *conn) Context() context.Context {
|
func (c *conn) Context() context.Context {
|
||||||
return c.ctx
|
return c.ctx
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,11 +172,12 @@ func (l *http2Listener) handleFunc(w http.ResponseWriter, r *http.Request) {
|
|||||||
"w": w,
|
"w": w,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
conn := &conn{
|
conn := &conn{
|
||||||
laddr: l.addr,
|
laddr: l.addr,
|
||||||
raddr: remoteAddr,
|
raddr: remoteAddr,
|
||||||
closed: make(chan struct{}),
|
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -186,5 +187,5 @@ func (l *http2Listener) handleFunc(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
<-conn.Done()
|
<-ctx.Done()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ func (l *mwsListener) upgrade(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := r.Context()
|
||||||
if cc, ok := conn.NetConn().(xctx.Context); ok {
|
if cc, ok := conn.NetConn().(xctx.Context); ok {
|
||||||
if cv := cc.Context(); cv != nil {
|
if cv := cc.Context(); cv != nil {
|
||||||
ctx = cv
|
ctx = cv
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ func (l *wsListener) upgrade(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.WithoutCancel(r.Context())
|
||||||
if cc, ok := conn.NetConn().(xctx.Context); ok {
|
if cc, ok := conn.NetConn().(xctx.Context); ok {
|
||||||
if cv := cc.Context(); cv != nil {
|
if cv := cc.Context(); cv != nil {
|
||||||
ctx = cv
|
ctx = cv
|
||||||
|
|||||||
+1
-4
@@ -230,14 +230,11 @@ func (s *defaultService) Serve() error {
|
|||||||
ctx = xctx.ContextWithDstAddr(ctx, dstAddr)
|
ctx = xctx.ContextWithDstAddr(ctx, dstAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// clientAddr := srcAddr.String()
|
|
||||||
// ctx = xctx.ContextWithClientAddr(ctx, xctx.ClientAddr(clientAddr))
|
|
||||||
|
|
||||||
clientIP := srcAddr.String()
|
clientIP := srcAddr.String()
|
||||||
if h, _, _ := net.SplitHostPort(clientIP); h != "" {
|
if h, _, _ := net.SplitHostPort(clientIP); h != "" {
|
||||||
clientIP = h
|
clientIP = h
|
||||||
}
|
}
|
||||||
// ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: clientIP})
|
ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: clientIP})
|
||||||
|
|
||||||
for _, rec := range s.options.recorders {
|
for _, rec := range s.options.recorders {
|
||||||
if rec.Record == recorder.RecorderServiceClientAddress {
|
if rec.Record == recorder.RecorderServiceClientAddress {
|
||||||
|
|||||||
Reference in New Issue
Block a user