I am trying to use ctx.Done
(gin context) to cancel processing request and stop go routine when a user refreshes or exits the page. The code below didn't work, what i do wrong?
respChan := make(chan types.Result)
defer close(respChan)
cCp := c.Copy()
go func() {
for {
fmt.Println("check")
time.Sleep(10000 * time.Millisecond) //long task
test := []types.ResultTXT{{Txt: "Test Resp"}}
select {
case <-cCp.Done():
fmt.Println("goroutine canceled")
return
case respChan <- types.Result{Response: test}:
fmt.Println("Work end")
return
}
}
}()
select {
case <-c.Done():
fmt.Println("canceled")
c.Abort()
return
case res := <-respChan:
result := res.Response
c.HTML(http.StatusOK, "response.html", gin.H{
"Resp": result,
})
return
}
I use gin and go in v. 1.8, but i also tried downgrade (to gin 1.7.7 and use c.Request.Context().Done()
) and result is same.