From: qydysky Date: Tue, 18 Jul 2023 18:06:40 +0000 (+0800) Subject: add X-Git-Tag: v0.28.0+2023071821a6ae3 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=21a6ae354156092afa29daa584593d993989a66e;p=part%2F.git add --- diff --git a/io/io.go b/io/io.go index fe60b1a..96f5a9e 100644 --- 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 +} diff --git a/reqf/Reqf.go b/reqf/Reqf.go index 5326c1a..b6cde2f 100644 --- a/reqf/Reqf.go +++ b/reqf/Reqf.go @@ -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()