]> 127.0.0.1 Git - part/.git/commitdiff
add v0.28.0+2023071821a6ae3
authorqydysky <qydysky@foxmail.com>
Tue, 18 Jul 2023 18:06:40 +0000 (02:06 +0800)
committerqydysky <qydysky@foxmail.com>
Tue, 18 Jul 2023 18:06:40 +0000 (02:06 +0800)
io/io.go
reqf/Reqf.go

index fe60b1aa9fb3554f8a3cdc0327efa2483574bfe4..96f5a9eae6a00af56684c0f1f3925bee6538c9f3 100644 (file)
--- a/io/io.go
+++ b/io/io.go
@@ -2,6 +2,7 @@ package part
 
 import (
        "context"
+       "errors"
        "fmt"
        "io"
        "sync/atomic"
@@ -141,3 +142,30 @@ func WithCtxTO(ctx context.Context, callTree string, to time.Duration, w []io.Wr
                },
        }
 }
+
+var (
+       ErrWrite = errors.New("ErrWrite")
+       ErrRead  = errors.New("ErrRead")
+)
+
+// close reader by yourself
+func WithCtxCopy(ctx context.Context, callTree string, to time.Duration, w []io.WriteCloser, r io.Reader, panicf ...func(s string)) error {
+       rwc := WithCtxTO(ctx, callTree, to, w, r)
+       defer rwc.Close()
+       for buf := make([]byte, 2048); true; {
+               if n, e := rwc.Read(buf); n != 0 {
+                       if n, e := rwc.Write(buf[:n]); n == 0 && e != nil {
+                               if !errors.Is(e, io.EOF) {
+                                       return errors.Join(ErrWrite, e)
+                               }
+                               break
+                       }
+               } else if e != nil {
+                       if !errors.Is(e, io.EOF) {
+                               return errors.Join(ErrRead, e)
+                       }
+                       break
+               }
+       }
+       return nil
+}
index 5326c1a26075710b7bbd69c5cf0c0155be8f638f..b6cde2fb13131a1a1adb732a8bb42ac2543c6481 100644 (file)
@@ -263,24 +263,8 @@ func (t *Req) Reqf_1(ctx context.Context, val Rval) (err error) {
                writeLoopTO = 1000
        }
 
-       rwc := pio.WithCtxTO(req.Context(), t.callTree, time.Duration(int(time.Millisecond)*writeLoopTO), ws, resReadCloser)
-       defer rwc.Close()
-
-       for buf := make([]byte, 2048); true; {
-               if n, e := rwc.Read(buf); n != 0 {
-                       if n, e := rwc.Write(buf[:n]); n == 0 && e != nil {
-                               if !errors.Is(e, io.EOF) {
-                                       err = errors.Join(err, ErrWriteRes, e)
-                               }
-                               break
-                       }
-               } else if e != nil {
-                       if !errors.Is(e, io.EOF) {
-                               err = errors.Join(err, ErrReadRes, e)
-                       }
-                       break
-               }
-       }
+       // io copy
+       err = errors.Join(err, pio.WithCtxCopy(req.Context(), t.callTree, time.Duration(int(time.Millisecond)*writeLoopTO), ws, resReadCloser))
 
        resp.Body.Close()