]> 127.0.0.1 Git - part/.git/commitdiff
81
authorqydysky <qydysky@foxmail.com>
Tue, 22 Sep 2020 14:08:25 +0000 (22:08 +0800)
committerqydysky <qydysky@foxmail.com>
Tue, 22 Sep 2020 14:08:25 +0000 (22:08 +0800)
Reqf.go

diff --git a/Reqf.go b/Reqf.go
index 5976c9324df2b6acd58ec79e9ef3b518a23d4b3d..1ce827befb767c42d52e2d600f9f334ee57ae260 100644 (file)
--- a/Reqf.go
+++ b/Reqf.go
@@ -35,7 +35,9 @@ type req struct {
     ResponseCode int
     Respon []byte
     UsedTime time.Duration
-    Cancel chan interface{}
+
+    cancelOpen bool
+    cancel chan interface{}
     sync.Mutex
 }
 
@@ -64,8 +66,13 @@ func (this *req) Reqf(val Rval) (error) {
     if _val.Timeout==0{_val.Timeout=3}
 
        for ;_val.Retry>=0;_val.Retry-- {
-               returnErr=this.Reqf_1(_val)
-        if returnErr==nil {break}
+        returnErr=this.Reqf_1(_val)
+        select {
+        case <- this.cancel://cancel
+            break
+        default:
+            if returnErr==nil {break}
+        }
         time.Sleep(time.Duration(_val.SleepTime)*time.Millisecond)
     }
 
@@ -122,8 +129,9 @@ func (this *req) Reqf_1(val Rval) (error) {
     req = req.WithContext(cx)
 
     go func(){
-        this.Cancel = make(chan interface{})
-        <- this.Cancel
+        this.cancel = make(chan interface{})
+        this.cancelOpen = true
+        <- this.cancel
         cancel()
     }()
 
@@ -174,4 +182,15 @@ func (this *req) Reqf_1(val Rval) (error) {
     this.UsedTime=time.Since(beginTime)
     
     return nil
+}
+
+func (t *req) Close(){
+    if !t.cancelOpen {return}
+    select {
+    case <- t.cancel://had close
+        return
+    default:
+        close(t.cancel)
+        t.cancelOpen = false
+    }
 }
\ No newline at end of file