1

I am using jetty 9.4.26 with --module=gzip to enable compression on all my server apps. I am sending a lot of byte messages (not strings) to websocket clients (small, but tens of messages per second per client) and I am not sure if the compression is applied to each websocket message, as I noticed an almost double increase in CPU usage (I have not eliminated every possibility but it is the only thing I changed that might have impacted the cpu).

And if it does, is there a way to disable gzip for websockets only?

Andrei F
  • 4,205
  • 9
  • 35
  • 66

2 Answers2

0

Compression of websocket messages is done via the permessage-deflate extension for the websocket conversation itself.

See: https://stackoverflow.com/a/19300336/775715

HTTP gzip compression (which is what --module=gzip enables) is completely unrelated.

Keep in mind that websocket is an HTTP upgrade to websocket, once you are upgraded to WebSocket, the HTTP HTTP behaviors are no longer in play.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • So if I understand correctly, that extension is not enabled by default, that means that no additional CPU should be needed when sending the webscoket messages to clients? – Andrei F Feb 26 '20 at 07:47
  • Jetty has the `permessage-deflate` extension in its list of registered extensions by default, if a client chooses to use it and sends it on its WebSocket upgrade offer, then Jetty will response in kind and use it. Which will use CPU to compress websocket messages to clients and to uncompress websocket messages from clients. – Joakim Erdfelt Feb 26 '20 at 12:19
  • So if i wanted to use this extension i would send "permessage-deflate" header value when making the upgrade request? – Andrei F Feb 26 '20 at 14:26
0

If you use Jetty project this link helps.

WebSocketUpgradeFilter wsuf = WebSocketUpgradeFilter.configureContext(context);
wsuf.getFactory().getExtensionFactory().unregister("permessage-deflate");

If you use Spring framework, then refer to this link:

@Bean
public JettyServletWebServerFactory jettyServletWebServerFactory() {
    JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
    factory.addServerCustomizers(server -> {
        GzipHandler gzipHandler = new GzipHandler();
        gzipHandler.setInflateBufferSize(1);
        gzipHandler.setHandler(server.getHandler());

        HandlerCollection handlerCollection = new HandlerCollection(gzipHandler);
        server.setHandler(handlerCollection);
    });
    return factory;
}
kissLife
  • 307
  • 1
  • 2
  • 9