Skip to content

Best Practice for context canceled. #1008

@benkawecki

Description

@benkawecki

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:

  1. What should the server do when the client disconnects (is there a way to effectively "cancel" the request within the huma handler?)
  2. If the server should return a status code which one should it be?
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions