开发者

What will common web servers do when user canceled a request?

For a not-so-short-period request, and the user canceled the connection (i.e., he/she closed the browser before the request is completed), what will happen in server-side then?

It should be server dependent开发者_JAVA技巧, though. But what would be the common way?

Some requests take rather long time to complete, for example, a distributed DBMS transaction, which will commit data to several different servers in different countries. (I know that's very common in bank applications)

Some requests send a large chunk of data to the client, for example, when user download a large attachment.

Should it cancel the request thread after a specific timeout?

Should my web application take care of user activity, and cancel the transaction myself like this:

service(request, response):
    tx_start();
    do {
        do_some_work()
        if (user_canceled) break;
        do_some_work()
        if (user_canceled) break;
        do_some_work()
        if (user_canceled) break;
        do_some_work()
        if (user_canceled) break;
        do_some_work()
        if (user_canceled) break;

        tx_commit();
        return;
    } while (false);
    tx_rollback();
    throw new CanceledException();
}

should I?


Problem:

  • If do_some_work() takes longer than expected and the client connection died, do_some_work() may continue when the transaction is no longer needed.

Solutions:

  • Separate server transaction work and client input using another thread.
  • Let the client interrupt do_all_work() which then throws CanceledException.
service(request, response) {  
  try {  
    client_start(request);  
    tx_start();
    do_all_work();
    tx_commit();  
  } catch(CanceledException ce) {  
    tx_rollback();  
    throw ce;
  } finally { 
    client_join();  
  }
}

client_start(request) {
  client = new Client(request);
  client_thread = new Thread(client).start();
}

client_join() {
  client_thread.join(1000); // wait 1s for the client thread to end  
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜