-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Askar Ibragimov opened SPR-15527 and commented
Jetty websocket server has two ways to send messages:
- blocking Write http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/RemoteEndpoint.html#sendBytes-java.nio.ByteBuffer-
- non-blocking Write http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/RemoteEndpoint.html#sendBytes-java.nio.ByteBuffer-org.eclipse.jetty.websocket.api.WriteCallback- and http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/RemoteEndpoint.html#sendBytesByFuture-java.nio.ByteBuffer-
Presently Spring Websocket uses blocking write in Jetty adapter. Which means that the message engine of Spring (ExecutorSubscribableChannel) has to have many threads and a thread remains parked till client acknowledges the reception of message.
There are few issues with this approach
- If client never acknowledges the reception on TCP level, the thread (from TaskExecutorChannel's threadpool) remains blocked. Actually due to a very funny decision of Jetty engineers to not set a timeout on these connections, so the block is never lifted (WebSocket hangs in blockingWrite jetty/jetty.project#272). In plain language, any Spring+Jetty app is exposed to a DDOS attack.
- This is a bad utilization of threads. I might want to write a logic that re-uses the thread for something else and just get notified via callback when web socket processed the message. This would allow me to have smaller thread pools and truly high-load-ready nonblocking architecture.
Suggested improvement
Permit in Spring usage of non-blockig websocket write with possibility to expose respective callback or future on upper level. Then custom or new ExecutorChannel implementation may use these callbacks to smartly utilize smaller number of threads and do not have blocked threads.
Useful contribution from Jetty people (caveats of using non blocking API of Jetty) http://stackoverflow.com/questions/43257736/make-websocket-sending-non-blocking
Issue Links:
- Jetty - use Async Send like Atmosphere does [SPR-13602] #18180 Jetty - use Async Send like Atmosphere does
- Expose asynchronous message sending in WebSocketSession [SPR-14099] #18671 Expose asynchronous message sending in WebSocketSession
Referenced from: commits 1b0e95d
1 votes, 3 watchers