-
Notifications
You must be signed in to change notification settings - Fork 58
golang-http does not allow graceful request cancelling #40
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
Comments
Thanks for this example, please can you now show us how it would work differently with the cancellation in place? |
I was just about to add that, I would expect this function to stop as soon as the package function
import (
"fmt"
"net/http"
handler "github.com/openfaas-incubator/go-function-sdk"
)
// Handle a function invocation
func Handle(req handler.Request) (handler.Response, error) {
var err error
for i := 0; i < 10000; i++ {
if req.Context().Err() != nil {
return handler.Response{}, fmt.Errorf("request cancelled")
}
fmt.Printf("count %d\n", i)
}
message := fmt.Sprintf("Hello world, input was: %s", string(req.Body))
return handler.Response{
Body: []byte(message),
StatusCode: http.StatusOK,
}, err
} |
This would make a good addition to the README.md as an example: I.e. "How to cancel a long running operation" or similar |
Is this issue resloved now? |
I will PR the updated docs against this, but i think it is fixed once the of-watchdog is updated in the templates as well |
Because the go-function-sdk does not propagate the original request context in the
Request
object, the handler can not correctly respond to the request being cancelled by the user. In the Go stdlib this information is generally conveyed by the checking ifrequest.Context()
is done.If the handler has an expensive operation that should be canceled and reverted, e.g. a DB transaction, this would be impossible because the handler can not check for the early cancellation.
We should be able to see this by creating a handler that prints/counts to a large number, issuing a request and then immediately cancelling. The handler should continue to count even after the user aborts the request.
For example, using this
Build and run the function
Then in a new terminal
You will see the function start and then continue even after you
ctrl-c
the curl request. This is because the handler has not even attempted to check for the abort. But it is not currently possible to do this because the context is not available.The text was updated successfully, but these errors were encountered: