On Sat, Nov 3, 2012 at 4:59 AM, funback
<expe...@gmail.com> wrote:
hi, just discovered sockjs-tornado by recommendation, looks very nice and would like to use it more seriously. But first, a few questions I'd like to clarify (some may seem obvious for a current user of the library).
1. is there a easy way to measure how much time it takes to handle a request to a sockjs-tornado method (e.g. on_message)? In tornado, it gives you the time in milliseconds for handling each request, can this be easily done for sockjs-tornado methods?
It is quite complicated, as there's no usual request-reply pattern and sockjs-tornado can't figure out if you finished processing incoming message or just started asynchronous operation.
If you don't use asynchronous code or use @gen.engine, you can measure execution time by getting time stamp in beginning of on_message and calculating delta in end of the function.
2. i suspect the send/broadcast method in sockjs-tornado is implemented asynchronously (is it?), then how should i handle this situation: imagine i'm broadcasting a set of 5 clients, and when sending the msg to the 3rd one, some exception/error happens (for example, this client may have already disconnected), now what will happen to the 4th and 5th clients? Will the msg be sent to them at all?
3. i notice that there is is_closed method on the SockJSConnection class. Can this method be used to detect disconnections in real-time, while executing on_message of some connection instance?
This is just a flag and it only represents if logical session was closed. In most of the cases it also represents if connection was closed, but if client is disconnected while your code was running (ioloop was blocked), it will report wrong value.
4. most importantly, how should I handle connection-related exceptions in on_message method? For examples, network errors may occur when trying to send msg; connections may already dropped when sending msgs asynchronously; how do we handle these exceptions/errors?
I'm not sure if you have to do something special about it. If your code raises exception, sockjs-tornado will kill offending connection and will continue working like nothing happened.
So, if you expect your code might raise exception, wrap it in try/except or ExceptionStackContext and handle errors yourself.
If you see any kind of unexpected behavior, open a ticket.
Serge.