request for feedback: another websocket server implementation

175 views
Skip to first unread message

Jochen Voss

unread,
Sep 17, 2019, 6:29:08 PM9/17/19
to golang-nuts
Dear all,

Because I wanted to learn about the websocket protocol, I have implemented a websocket server in Go:


I would be happy to receive feedback about my code.

Specific questions:

- Does the API look reasonable?  Anything to improve?

- I use the function readMultiplexer (at the end of reader.go) to read all frame headers from the network connection, and to make sure that the readers of the frame bodies don't step on each other's feet.  Similarly, the function writeMultiplexer (at the end of writer.go) coordinates all writers and performs all writes to the network connection.  This system is held together by a tangle of go channels.  The code is much messier than I would like it to be.  Is there a way to structure such code so that it is easier to reason about, so that I can be more confident that it is correct?  The bits a struggled most with are the various ways the connection can be closed (closed by server explicitly or because of a protocol error, closed by client with/without close frame).

- I attempted to profile the code, to find whether there are any performance bottlenecks.  My code for this is in websocket_test.go (function BenchmarkEcho), but the profiles then only ever show the code waiting for data on go channels or for data from the network.  My feeling is that I was really benchmarking the network connection and not my code.  Is there a way to improve BenchmarkEcho to get more meaningful results?

Many thanks,
Jochen

T L

unread,
Sep 18, 2019, 9:27:17 AM9/18/19
to golang-nuts
Just for reference. Here are other Go websocket implementations:

Ilia Choly

unread,
Sep 18, 2019, 9:37:49 AM9/18/19
to golang-nuts
You should try to make Conn implement io.Reader and io.Writer


On Tuesday, September 17, 2019 at 6:29:08 PM UTC-4, Jochen Voss wrote:

Jochen Voss

unread,
Sep 18, 2019, 1:06:20 PM9/18/19
to golang-nuts
Dear T L,

Thanks for this.  I was aware of implementations number 1 and 4 (and I think I like my own better than these), but I didn't spot numbers 2 and 3.  I'll have a look at these, too.

Many thanks,
Jochen

Jochen Voss

unread,
Sep 18, 2019, 1:10:38 PM9/18/19
to golang-nuts
Dear Ilia,

Thank you for your comment.  In my mind, Read and Write are made for streaming data, whereas a websocket connection transports messages.  How would I handle message types and message boundaries when using the io.Reader and io.Writer interfaces?  (I do try to implement these interfaces for the contents of the messages themselves, see my SendMessage and ReceiveMessage functions.)

Many thanks,
Jochen

Ilia Choly

unread,
Sep 18, 2019, 4:43:22 PM9/18/19
to golang-nuts
You're absolutely right. Disregard my comment.
Reply all
Reply to author
Forward
0 new messages