Java Servlets: how to repeat an HTTP request?

I want to retry the HTTP request automatically if a database stall occurs; however FilterChain.doFilter () is defined as a unidirectional chain (so I cannot reset its state).

In cases where this is safe, you can retry the HTTP request without resubmitting the request .

UPDATE . I just discovered a problem with this approach. Even if you repeat the request, you will need to buffer the InputStream request. This means that if the user downloads 100 MB of data, you will be forced to buffer this data regardless of whether a deadlock has occurred.

I am exploring the idea of ​​making the client repeat the request here: Is it appropriate to return HTTP 503 in response to a database lock?

0
source share
2 answers

Answering my own question:

Do not try to retry the HTTP request. To do this, you will be forced to buffer an InputStream for all requests, even if a deadlock never occurs. This will allow you to refuse denial of service attacks if you are forced to accept large downloads.

Instead, I recommend this approach: Is it appropriate to return HTTP 503 in response to a database lock?

You can then break large downloads into multiple requests stitched together using AJAX. Not really, but it works, and overall your design should be easier to implement.

UPDATE . According to Brett Wooldridge :

You need a small pool of several dozen connections in most cases, and you want the rest of the application threads to be blocked in the pool waiting for the connection.

Just as Hikari recommends a small number of threads with a long queue of requests, I believe this is true for the web server. By limiting the number of active streams, we limit the number of input streams that we need to buffer (other requests are blocked before sending the HTTP body).

To further strengthen this point, Craig Ringer recommends recovering from server-side failures where possible.

+1
source

You can "forward" the original request, as shown below.

RequestDispatcher rd= request.getRequestDispatcher("/originalUrl"); rd.forward(request, response); 

Here, the request and response represent HttpServletRequest / HttpServletResponse respectively. http://docs.oracle.com/javaee/5/api/index.html?javax/servlet/RequestDispatcher.html

Alternatively, you can redirect the response. This, however, will send a response to the browser asking for a new request for the provided URL. This is shown below.

 response.sendRedirect("originalUrl?neededParam=abc"); 
0
source

Source: https://habr.com/ru/post/1481560/


All Articles