]> 127.0.0.1 Git - part/.git/commitdiff
limit fix v0.5.18
authorqydysky <qydysky@foxmail.com>
Mon, 31 May 2021 14:58:04 +0000 (22:58 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 31 May 2021 14:58:04 +0000 (22:58 +0800)
reqf fix

limit/Limit.go
reqf/Reqf.go

index 2907bb64f2429d0439517e12d60542078e21d5d7..9d9a4e373af48342d39d31203afe5108f164be4a 100644 (file)
@@ -26,8 +26,7 @@ func New(maxNum_in_period,ms_in_period,ms_to_timeout int) (*Limit) {
 
        go func(object *Limit){
                for object.maxNum_in_period > 0 {
-                       object.bucket <- struct{}{}
-                       for i:=1;i<object.maxNum_in_period;i++{
+                       for i:=0;i<object.maxNum_in_period;i++{
                                select {
                                case object.bucket <- struct{}{}:;
                                default :i = object.maxNum_in_period
@@ -51,6 +50,11 @@ func (l *Limit) TO() bool {
        var AfterMS = func(ReadTimeout int) (c <-chan time.Time) {
                if ReadTimeout > 0 {
                        c = time.NewTimer(time.Millisecond*time.Duration(ReadTimeout)).C
+               } else if ReadTimeout < 0 {
+                       tc := make(chan time.Time,1)
+                       tc <- time.Now()
+                       close(tc)
+                       c = tc
                }
                return
        }
index a9763d1efd6e7ce74f9854e8e2c175cf7a3b2f5c..0a215c218e990c4a4e19448d97449b4894f6e5c4 100644 (file)
@@ -7,6 +7,7 @@ import (
     "context"
     "time"
     "strings"
+    "bytes"
     "net/http"
     "errors"
     "io/ioutil"
@@ -170,16 +171,19 @@ func (this *Req) Reqf_1(val Rval) (err error) {
     }
     
     var (
-        saveToFile func(io.Reader,string)error = func (Body io.Reader,filepath string) error {
+        saveToFile func(io.Reader,string)([]byte,error) = func (Body io.Reader,filepath string) (bodyB []byte,err error) {
             out, err := os.Create(filepath + ".dtmp")
-            if err != nil {out.Close();return err}
+            if err != nil {out.Close();return bodyB,err}
+            var buf bytes.Buffer
 
-            if _, err = io.Copy(out, Body); err != nil {out.Close();return err}
+            w := io.MultiWriter(out, &buf)
+            if _, err = io.Copy(w, Body); err != nil {out.Close();return bodyB,err}
             out.Close()
+            bodyB = buf.Bytes()
 
-            if err = os.RemoveAll(filepath); err != nil {return err}
-            if err = os.Rename(filepath+".dtmp", filepath); err != nil {return err}
-            return nil
+            if err = os.RemoveAll(filepath); err != nil {return bodyB,err}
+            if err = os.Rename(filepath+".dtmp", filepath); err != nil {return bodyB,err}
+            return bodyB,nil
         }
     )
     this.Response = resp
@@ -213,8 +217,27 @@ func (this *Req) Reqf_1(val Rval) (err error) {
             }
         } else {
             if SaveToPath != "" {
-                if err := saveToFile(resp.Body, SaveToPath); err != nil {
+                if bodyB,err := saveToFile(resp.Body, SaveToPath); err != nil {
                     return err
+                } else {
+                    if len(bodyB) != 0 {
+                        if SaveToChan != nil {
+                            SaveToChan <- bodyB
+                        } else if SaveToPipeWriter != nil {
+                            SaveToPipeWriter.Write(bodyB)
+                        } else {
+                            this.Respon = append(this.Respon,bodyB...)
+                        }
+                    } else {
+                        return io.EOF
+                    }
+
+                    if SaveToChan != nil {
+                        close(SaveToChan)
+                    }
+                    if SaveToPipeWriter != nil {
+                        SaveToPipeWriter.Close()
+                    }
                 }
             } else {
                 rc,_ := pio.RW2Chan(resp.Body,nil)