]> 127.0.0.1 Git - front/.git/commitdiff
1
authorqydysky <qydysky@foxmail.com>
Sun, 3 Dec 2023 03:29:43 +0000 (11:29 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 3 Dec 2023 03:29:43 +0000 (11:29 +0800)
main.go

diff --git a/main.go b/main.go
index 17783dbe61f9ff0316196118503d0ed1f425b6f9..6598f989abbdc89c598db0f0efa9d61ae2c070de 100644 (file)
--- a/main.go
+++ b/main.go
@@ -330,25 +330,37 @@ func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, route
                        fin := make(chan error)
                        reqc := req.NetConn()
                        resc := res.NetConn()
+                       ctx, cancle := pctx.WithWait(ctx, 2, time.Minute)
                        defer func() {
-                               reqc.Close()
-                               resc.Close()
+                               if errors.Is(cancle(), pctx.ErrWaitTo) {
+                                       logger.L(`E:`, "退出超时")
+                               }
                        }()
                        go func() {
+                               ctx1, done1 := pctx.WaitCtx(ctx)
+                               defer done1()
                                _, e := io.Copy(reqc, resc)
-                               fin <- e
+                               select {
+                               case fin <- e:
+                               case <-ctx1.Done():
+                                       fin <- nil
+                               }
+                               reqc.Close()
                        }()
                        go func() {
+                               ctx1, done1 := pctx.WaitCtx(ctx)
+                               defer done1()
                                _, e := io.Copy(resc, reqc)
-                               fin <- e
-                       }()
-                       select {
-                       case e := <-fin:
-                               if e != nil {
-                                       logger.L(`E:`, fmt.Sprintf("%s=>%s %v", routePath, back.Name, e))
-                                       return ErrCopy
+                               select {
+                               case fin <- e:
+                               case <-ctx1.Done():
+                                       fin <- nil
                                }
-                       case <-ctx.Done():
+                               resc.Close()
+                       }()
+                       if e := <-fin; e != nil {
+                               logger.L(`E:`, fmt.Sprintf("%s=>%s %v", routePath, back.Name, e))
+                               return ErrCopy
                        }
                        return nil
                }