Skip to content
Noah Andrews edited this page Sep 27, 2018 · 6 revisions

Overview

WebSocketServer

Both the websocket client and the server use nio although their architecture is quite different. While the client only uses one thread to perform the read the write operations and the event delivery the server performs those operations multithreaded:

There is the selector thread and one or more worker threads.

The Selectorthread performs all nio operations: It registers channels selection keys in order to read from and write to the channels efficently.

But is does not do any encoding/decoding or event delivery. Those operations will be executed by the worker threads.

The selector and the worker thread communicate over queues. There is the decoding queue, the write queue and the buffer queue.

The cycle decoding circle is: The selector thread takes a unused buffer from the "buffer queue" and put data from a read ready channel into it and puts it into the decoding queue. A workers thread takes the websocket out of that queue and consumes all of the assigned buffers content. After that it put the buffer back into the buffer queue so that it can be reused.

The behavior in terms of performance can be controlled by choosing the number of worker threads, the number and size of buffers in the buffer queue, and the size of the channels' internal buffers. The number of worker threads can be adjusted by using one of the forms of the WebSocketServer constructors that takes a decodercount parameter. In the future there will be ways you can set the other parameters as well.