lxzan / gws

simple, fast, reliable websocket server & client, supports running over tcp/kcp/unix domain socket. keywords: ws, proxy, chat, go, golang...
https://pkg.go.dev/github.com/lxzan/gws
Apache License 2.0
1.36k stars 87 forks source link

Use cases of ReadAsyncGoLimit and ReadAsyncCap configurations #24

Closed WeiquanWa closed 1 year ago

WeiquanWa commented 1 year ago

There are two similar configs of ReadAsyncGoLimit and ReadAsyncCap. What's the differencies between them in terms of use cases?

lxzan commented 1 year ago

ReadAsyncGoLimit sets the maximum number of OnMessage calls in parallel on a single connection. ReadAsyncCap sets the maximum number of messages to be stacked, overflowing will return an error and close the connection.

WeiquanWa commented 1 year ago

Thank you for your prompt reply.

lxzan commented 1 year ago

Generally speaking, ReadAsyncCap does not need to be modified, it defaults to 256 and normal connections do not pile up so many messages. The default value of ReadAsyncGoLimit is 8, which can handle up to 8 requests in parallel, modify it if necessary.

lxzan commented 1 year ago

gws read and write is thread-safe, reads serially, and processes business logic in parallel.

WeiquanWa commented 1 year ago

okay. if ReadAsyncEnabled is false, gws creates two read and write-goroutines of each new connection?

WeiquanWa commented 1 year ago

is it possible to read and write messages concurrently for single connection using gws?

lxzan commented 1 year ago

is it possible to read and write messages concurrently using gws?

Unlike gorilla/websocket, gws' WriteMessage ensures thread safety

lxzan commented 1 year ago

okay. if ReadAsyncEnabled is false, gws creates two read and write-goroutines of each new connection?

One connection in gws will only create one goroutine for reading messages

WeiquanWa commented 1 year ago

to read and write concurrently, we will have to enable ReadAsyncEnabled. please confirm

lxzan commented 1 year ago

Not required, gws also provides the WriteAsync method, which starts a new goroutine to write the message.

If you want OnMessage to be processed in parallel, you need to turn it on, or implement your own concurrency control logic.

lxzan commented 1 year ago

is it possible to read and write messages concurrently for single connection using gws?

This possibility exists.

WeiquanWa commented 1 year ago

okay cool

Looking at the source code, reader and writer are using a same buffer pool named myBufferPool across all connections. Is it realistic if there are a lot of many concurrent connections and the packet size is large?

lxzan commented 1 year ago

How big is it exactly? gws is not optimized for large packets (e.g. writing temporary files), and large packets will block the connection, I think this scenario should use HTTP, WebSocket messages with URLs in them.

WeiquanWa commented 1 year ago

I see, seems like the approach to manage memory in gws is a manner to reduce memory usage