-
-
Notifications
You must be signed in to change notification settings - Fork 254
Best Practice for context canceled. #1008
Copy link
Copy link
Open
Labels
questionFurther information is requestedFurther information is requested
Description
Is there a good pattern for handling client disconnects? When a get a client cancelation (specifically client timeout or client disconnect) I typically end up having the server return a 500. This makes adding monitoring more difficult because we have alarms setup around 500.
For example, consider the following handler.
// Handler function that does many things
func GetWidget(ctx context.Context, input *Input) (*Output, error) {
// validate user makes an external http api call and can have a context canceled error
userInfo, err := validateUser(ctx)
if err != nil {
// return a 500 here
}
greeting, err := db.getWidget(input.id)
if err != nil {
// return 500 or other status code
}
// ...
return resp, nil
}The client could disconnect causing an err to be returned from either the validateUser function or the getWidget function. I could check the err issue against the client disconnect error, but overall this doesn't seem like an easy to abstract pattern.
My questions are:
- What should the server do when the client disconnects (is there a way to effectively "cancel" the request within the huma handler?)
- If the server should return a status code which one should it be?
- What is the best abstraction to use to add this to a large number of handlers which may not have complicated logic to handle if the client disconnects mid request.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested