jetty / jetty.project

Eclipse Jetty® - Web Container & Clients - supports HTTP/2, HTTP/1.1, HTTP/1.0, websocket, servlets, and more
https://eclipse.dev/jetty
Other
3.86k stars 1.91k forks source link

OOM error on jetty websocket async write #1410

Closed neeraj20gupta closed 7 years ago

neeraj20gupta commented 7 years ago

I have written a websocket application using jetty in which the server send Json frames on the connected websocket clients.

For better performance I am using async Send string method.

session.getRemote().sendString(message, writecallback) For my connected 200 clients I see OOM error after sending data(approximately 500 records per second) for 20-25 mins.

Following is the trace

    java.lang.OutOfMemoryError: GC overhead limit exceeded
            at java.lang.StringCoding.encode(StringCoding.java:350)
            at java.lang.String.getBytes(String.java:929)
            at org.eclipse.jetty.util.StringUtil.getUtf8Bytes(StringUtil.java:561)
            at org.eclipse.jetty.websocket.common.frames.TextFrame.setPayload(TextFrame.java:44)
            at org.eclipse.jetty.websocket.common.WebSocketRemoteEndpoint.sendString(WebSocketRemoteEndpoint.java:426)

Jetty Version :- 9.4.0.v20161208. Heap Size :- 16 GB(both min and max). Default GC setting of JDK 1.8 The OOM is not happening when I use blocking SendString() Is this a bug with jetty or incorrect usage of async write? Thanks in advance :)

joakime commented 7 years ago

Incorrect usage of async write.

Async write will write when it can, you can't keep shoving content at the endpoint, having Jetty queue messages infinitely.

You have to pay attention to return on the Future, or use the Callback to identify when a message has been send (and more importantly, if it hasn't been sent after a certain amount of time).

Don't send messages to slow endpoints, drop messages if you can, enqueue messages on your application for when the prior write has completed.

This is can get rather complicated, and you'll probably want to use one of the many libraries built on top of WebSockets to handle this kind of messaging. (cometd is our go-to)