Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gin.Context Done() function does not work as expected #1452

Closed
bonjefir opened this issue Aug 1, 2018 · 5 comments
Closed

gin.Context Done() function does not work as expected #1452

bonjefir opened this issue Aug 1, 2018 · 5 comments

Comments

@bonjefir
Copy link

bonjefir commented Aug 1, 2018

I need to determine(in server-side) a request is cancelled from client. In the following code it take 5min even if we cancel the request from client to return handleFunc. But same code using the net/http package works properly.

func handleFunc(c *gin.Context) {
    ch := make(chan error, 1)
    go func() {
        time.Sleep(5 * time.Minute)
        ch <- nil
    }()

    select {
    case err = <-ch:
        fmt.Println(err)
    case <-c.Done():
        err = c.Err()
        fmt.Println(err)
    }

    c.HTML(200, "results", {})
}

PS:

  1. I used go version go1.10.1 linux/amd64

  2. And gin commit: a712f77

@bonjefir
Copy link
Author

bonjefir commented Aug 1, 2018

Using case <-c.Request.Context().Done(): instead of case <-c.Done(): solved the problem

@syssam
Copy link
Contributor

syssam commented Aug 4, 2018

if solved the problem, please close the issue

@bonjefir bonjefir closed this as completed Aug 4, 2018
@azaviyalov
Copy link

The issue should be reopened.

Currently the function is like:

// Done returns a channel that's closed when work done on behalf of this
// context should be canceled. Done may return nil if this context can
// never be canceled. Successive calls to Done return the same value.
func (c *Context) Done() <-chan struct{} {
	return nil
}

return nil make it useless and non-intuitive.

@thinkerou thinkerou reopened this Dec 11, 2018
@thinkerou
Copy link
Member

@lorencio1 please see #1690

@thinkerou
Copy link
Member

merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants