LongPolling

Last Updated: Apr 18, 2017

Context

Message Service enables message receiving in LongPolling mode. That is, WaitSecond is set to a value between 1 and 30 when messages are received. By LongPolling, the server holds the request sent by the client and returns the response only when there is a message for the client. This ensures message receiving in the first time, and prevents a large number of invalid requests from the client. LongPolling is recommended in Message Service.

LongPolling requires that the server holds the persistent connection at the HTTP layer. For the server, however, resources of the persistent connection at the HTTP layer are limited. To prevent malicious attacks, Message Service provides a limited number of LongPolling connections for a single user.

Problem description

Some users enable hundreds of threads in a single client to simultaneously access the Message Service server for messages. When no message is available in the queue, the client has hundreds of requests in LongPolling mode. If the users have multiple clients, the users may need to send thousands of LongPolling requests simultaneously.

In this scenario, when the users send LongPolling requests, the Message Service server may directly return an error “Message does not exist”, instead of holding the requests.

The users cannot obtain expected LongPolling results. Some users send consecutive LongPolling requests in a While loop but do not handle exceptions. After a period, the users find that a large number of requests have been sent.

Solution

LongPolling does not need to be attached to hundreds of threads simultaneously accessing the server, if there is no message in the queue. It can be attached only to threads between 1 and N. When threads attaching LongPolling find messages in the queue, they can wake other threads to receive the messages for fast response.

LongPolling sample code is a best practice where MessageReceiver is used to receive messages. A MessageReceiver instance is created for all threads receiving messages, and receiver.receiveMessage is used to receive messages.

The receiver provides an exclusive mechanism for LongPolling. If a thread is performing LongPolling, other threads can wait for messages.

Thank you! We've received your feedback.