Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what happens at the server when client closes the connection by using readTimeout

What happens at the server when client closes the connection to the API by using readTimeout.Will the execution for the request will be done or it will break as soon as timeout occurs OR the execution will complete and the response stream clogged with the response that server is supposed to send to the user

like image 450
niks Avatar asked Feb 10 '16 07:02

niks


1 Answers

Timeout is an untidy way to close the connection - when your side of a connection times out, there's good chance you won't be able to tell the other side you've timed out and are closing the connection. That is, the connection is not formally closed by coordinated action on both sides, it is just one side deciding it will consider it dead.

The way this is resolved is there are timeouts on both sides of the connection - if one side times out, the other side will timeout too, eventually.


As for what exactly happens on the server side: Since the server doesn't know the connection is dead until its own timeout expires, it will consider the connection good, and will normally process the request and attempt to write the response. There will probably be some buffering of the response, so attempting to write response may even seem to work to the server.

When and if the server attempts to write enough data to the response to fill the possible buffer, it will block, and then when timeout occurs an exception will be thrown, finally letting the server know that the connection timed out.

If the server doesn't fill the buffer with its response, the same (blocking, then exception) should happen when it tries to close the connection (but this may already happen outside your application, in the app server container code).

If by any chance you end up attempting to write the response after the timeout already occurred, you should get an exception right away.

So what exactly happens on server largely depends on your own code:

  • If you only write to response after performing all associated actions, they will be performed
  • If you intermix business logic and writing response, some of the logic might or might not be performed (depending on how much data you've already written to the response and at what time), and there's no good way to estimate what will happen

Either way, the server will eventually get to know the timeout occurred, but I'm not sure your application will always get this information.

like image 55
Jiri Tousek Avatar answered Sep 28 '22 05:09

Jiri Tousek