views:

100

answers:

5

If an http request is made and the caller abandons the request does it get completed anyway? For example an asynchronous JavaScript GET request to log a banner click in the DB then redirect. Does the script need to wait for the response?

A: 

You should wait for it to be safe. You never know when the server is going to get around to processing your request (though it is usually within a couple hundred milliseconds or less), so you won't know if something timed out, failed, or if you were going to receive a different response than expected unless you wait.

Cory Larson
+1  A: 

The client doesn't send any notification to the server that it is canceling the request.

PHP doesn't know if the client has disconnected until it tries to send the client some data (eg., an unbuffered echo() call), so if your script doesn't return any data to the user, it will fully execute. If it does return data it may abort partway through, but this can be changed with ignore_user_abort()) If you're using a different environment, you will have to explore the documentation.

GApple
A: 

For most cases, once the request is received by the server, it will not stop processing if the client stops listening.

However, the server can always fail while servicing the request, so it's probably not a good idea to assume it completed.

HackedByChinese
+2  A: 

How critical is your request? What if the database is not available at that time? What if the server side code throws an exception?

For very critical requests, you may need to implement some sort of message queueing that is able to hold the request data until it can be fully processed. This gets more complicated if you are dealing with grids and clouds (you can't just queue the message on a single node, since the node can potentially have a hardware failure). But this is an extreme case, where you end up with dedicated queue servers.

A: 

You don't have to wait for the response in order for the request to reach the server. The server can check if someone is still listening while processing the request, but processing the request will start even if noone is listening for the response (unless there was an error on the way, of course).

If you want to be sure that the request really was processed, you should wait for the response, but it's not required for the request to go through to the server.

Guffa