"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"
)
Response *http.Response
UsedTime time.Duration
- cancelOpen bool
- cancel chan interface{}
+ id *idpool.Id
+ cancel *signal.Signal
sync.Mutex
}
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}
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:
}
}()
}
return context.DeadlineExceeded
}
+ if !this.cancel.Islive() {
+ if SaveToChan != nil {
+ close(SaveToChan)
+ }
+ if SaveToPipeWriter != nil {
+ SaveToPipeWriter.Close()
+ }
+ return context.Canceled
+ }
}
}
}
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) {
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