Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting HTTP connection closure in Rails

When using Nginx there are times then HTTP 499 is recorded in the error log when the client has prematurely closed their connection (for example, the connection timed out, the user reloaded their browser or pressed Stop or otherwise).

I understand that Nginx is able to detect this because they are the end which will receive the SIGPIPE signal indicating the socket has been closed, however I am wondering if it is possible to get this notice back to Rails at all?

Shopify recently used a very novel approach to detect this in Unicorn before sending the request on to Rails to be processed, however I would like to be able to detect this within the Rails app, such as during a long-running transaction.

like image 393
Matthew Savage Avatar asked Nov 13 '22 01:11

Matthew Savage


1 Answers

After Rails's dispatcher had already started processing the request - there is no connection to the http client anymore, only when the processing is done - the response is headed back to the client.

So basically, there is no way you can tell if the client (e.g: browser) had shutdown the connection while you are already processing it.

like image 83
Elad Meidar Avatar answered Nov 15 '22 06:11

Elad Meidar