Stopping short

Stopping short is the process of discontinuing the processing request (before it would otherwise finish) based on an external signal.

In our case, that external signal will be the cancelation of the user's HTTP request. In Go, the http.Request object includes a Context() method; the following is an extract of the documentation for that method:

For incoming server requests, the context is canceled when the client's connection closes, the request is canceled (with HTTP/2), or when the ServeHTTP method returns

What does it mean when the request is canceled? Most importantly for us, it means that no one is waiting for the response.

If the user has given up listening to the response, it is likely they will consider the request failed and will hopefully try again.

How we should react to this situation depends on the feature we are implementing, but in many cases, mainly features related to loading or fetching data, the most effective response is to stop processing the request.

For the register endpoint of our service, this is the option we have chosen. We are going to pass Context from the request through all of the layers of our code using method injection. If the user cancels their request, we will immediately stop processing the request.

Now that we are clear on what we are trying to achieve, let's apply method injection to the layers of our service from the inside out. We need to start from the inside to ensure that our code and tests stay running during the refactor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.137.213