]> 127.0.0.1 Git - part/.git/commitdiff
req id v0.5.14
authorqydysky <qydysky@foxmail.com>
Tue, 11 May 2021 00:22:38 +0000 (08:22 +0800)
committerqydysky <qydysky@foxmail.com>
Tue, 11 May 2021 00:22:38 +0000 (08:22 +0800)
io/io.go
reqf/Reqf.go
reqf/Reqf_test.go
signal/Signal.go

index fb0be3302b69707eb0d9f0ac12eef2a657fec03e..1572fdf0b426ce1d458b7774f980b136c0f3f0ba 100644 (file)
--- a/io/io.go
+++ b/io/io.go
@@ -12,10 +12,10 @@ func RW2Chan(r io.ReadCloser,w io.WriteCloser) (rc,wc chan[]byte) {
                go func(rc chan[]byte,r io.ReadCloser){
                        for {
                                buf := make([]byte, 1<<16)
-                               n,e := r.Read(buf)
+                               n,_ := r.Read(buf)
                                if n != 0 {
                                        rc <- buf[:n]
-                               } else if e != nil {
+                               } else {
                                        close(rc)
                                        break
                                }
index f9d271ac3140d0e3b377a38aa003dcdd1f0c35b2..2b3b9d2c84467075ab5d1abe714c1a49c63f4eaa 100644 (file)
@@ -13,6 +13,8 @@ import (
     "net/url"
     compress "github.com/qydysky/part/compress"
     pio "github.com/qydysky/part/io"
+    signal "github.com/qydysky/part/signal"
+    idpool "github.com/qydysky/part/idpool"
     // "encoding/binary"
 )
 
@@ -38,8 +40,8 @@ type Req struct {
     Response  *http.Response
     UsedTime time.Duration
 
-    cancelOpen bool
-    cancel chan interface{}
+    id *idpool.Id
+    cancel *signal.Signal
     sync.Mutex
 }
 
@@ -67,10 +69,21 @@ func (this *Req) Reqf(val Rval) (error) {
 
     if _val.Timeout==0{_val.Timeout=3}
 
+    {
+        idp := idpool.New()
+        this.id = idp.Get()
+        defer func(){
+            idp.Put(this.id)
+            this.id = nil
+        }()
+    }
+
+    this.cancel = signal.Init()
+
        for ;_val.Retry>=0;_val.Retry-- {
         returnErr=this.Reqf_1(_val)
         select {
-        case <- this.cancel://cancel
+        case <- this.cancel.WaitC()://cancel
             return returnErr
         default:
             if returnErr==nil {return nil}
@@ -132,10 +145,8 @@ func (this *Req) Reqf_1(val Rval) (error) {
     var done = make(chan struct{})
     defer close(done)
     go func(){
-        this.cancel = make(chan interface{})
-        this.cancelOpen = true
         select {
-        case <- this.cancel:cancel()
+        case <- this.cancel.WaitC():cancel()
         case <- done:
         }
     }()
@@ -241,6 +252,15 @@ func (this *Req) Reqf_1(val Rval) (error) {
                         }
                         return context.DeadlineExceeded
                     }
+                    if !this.cancel.Islive() {
+                        if SaveToChan != nil {
+                            close(SaveToChan)
+                        }
+                        if SaveToPipeWriter != nil {
+                            SaveToPipeWriter.Close()
+                        }
+                        return context.Canceled
+                    }
                 }
             }
         }
@@ -254,14 +274,13 @@ func (this *Req) Reqf_1(val Rval) (error) {
 func (t *Req) Cancel(){t.Close()}
 
 func (t *Req) Close(){
-    if !t.cancelOpen {return}
-    select {
-    case <- t.cancel://had close
-        return
-    default:
-        close(t.cancel)
-        t.cancelOpen = false
-    }
+    if !t.cancel.Islive() {return}
+    t.cancel.Done()
+}
+
+func (t *Req) Id() uintptr {
+    if t.id == nil {return 0}
+    return t.id.Id
 }
 
 func Cookies_String_2_Map(Cookies string) (o map[string]string) {
index 5d506f0ecd2d6d74dd259c40b5b740ece6364889..fb70b490f82b1b945b34e55cf8923e8f9abe3405 100644 (file)
@@ -36,4 +36,33 @@ func Test_Cancel(t *testing.T) {
                return
        }
        t.Error(`no error`)
+}
+
+func Test_Cancel_chan(t *testing.T) {
+       r := New()
+
+       c := make(chan[]byte,1<<16)
+
+       go func(){
+               for{
+                       <-c
+               }
+       }()
+
+       go func(){
+               time.Sleep(time.Second*7)
+               r.Cancel()
+       }()
+
+       if e := r.Reqf(Rval{
+               Url:`https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-10.9.0-amd64-netinst.iso`,
+               SaveToChan:c,
+               Timeout:10,
+       });e != nil {
+               if !IsCancel(e) {
+                       t.Error(`type error`,e)
+               }
+               return
+       }
+       t.Error(`no error`)
 }
\ No newline at end of file
index 8180acb44ee90980a5335a2658b4d2b9c7231ac9..2721d99d4bc103ee2d67fd99d31aee836a2e39c8 100644 (file)
@@ -12,6 +12,11 @@ func (i *Signal) Wait() {
        if i.Islive() {<-i.Chan}
 }
 
+func (i *Signal) WaitC() (<-chan struct{}) {
+       if i.Islive() {return i.Chan}
+       return nil
+}
+
 func (i *Signal) Done() {
        if i.Islive() {close(i.Chan)}
 }