Comments (4)
The websocket connection code is "synchronous" in that writes to the websocket connection block until the message data is written to the operating system network connection.
Thanks that helps me understand the problem better. So I guess the OS happily fills its buffers with hundreds of messages until that buffer is full. Yet the deadline starts "counting" and after 5 seconds (in my case) where the application can't pass data to the OS, it "fails". I am still wondering why it takes 5 seconds (or longer) to pass the message to the OS. Maybe some weird OS logic in how buffers are handled?
Without the write deadline, a client can keep the connection alive without ever reading data.
Yeah, that's what I thought. I might want to change the behavior to actively send out pings from the server side.
Then I noticed that my broadcastHandler processed hundreds of messages per second (1636 messages) while the actual websocket client only received 107 messages in the same time. This seems wrong but might be explained by the writeBuffer used in the websocket library.
In this code:
select { case c.broadcastChan <- data: default: // Default case is executed if the client's broadcast channel is full. c.skippedCerts++ } }
data
is dropped when the client's broadcast channel is full.
Maybe I did not explain that part well enough. Yes, that is expected. data
should get dropped. To further clarify: The "skippedCerts" is not the same metric I used for measuring the "1636 messages" I mentioned in my original message. After playing around a little more I can confirm that it's actually a buffer of the websocket (or operating system as you said) that's holding all these messages. When I set a breakpoint at the right position I can see that messages are still being sent, even though there are no new conn.Write()
calls.
If the client is slow reading the data, but not so slow as to trip the per message deadline set in broadcastHandler, then messages will be discarded without any errors reported.
That's my goal. I want them to be discarded in my code without tripping the deadline. Do you know the best approach for that?
from websocket.
If the goal is to discard messages sent faster than the client can receive the messages, then the broadcaster's select with default accomplishes that goal.
Well yes, but with the exception that still the writeDeadline is tripping. Each message is about 1500 bytes in size. I thought that within the 5 seconds margin there will be enough space in the buffers for the OS to accept new ones.
The five second deadline may be too early
Yeah, that's probably the case. I tried with longer deadlines. For 60 seconds I did not have any issues so far, but with 10 seconds I also ran into problems with the deadline tripping.
Do you have evidence that the write deadline is tripped? The broadcast handler ignores errors.
Yes. Locally I have error checking in place (actually I just pushed the changes).
2023/12/16 21:04:24 client.go:62: Error while closing: write tcp 127.0.0.1:8080->127.0.0.1:56117: i/o timeout
2023/12/16 21:04:24 client.go:40: Closing broadcast handler for client: 127.0.0.1:56117
2023/12/16 21:04:24 client.go:116: Disconnecting client 127.0.0.1:56117!
the broadcast handler should handle errors by closing the network connection
I noticed that when I dived down the rabbit hole of weird issues with clients not receiving messages any more and changed that already, but thanks for noticing :)
from websocket.
Even with a 30 seconds deadline the deadline is tripped after about 10 minutes or so. When using a 60 second timeout, i did still did not come across any issues so far.
from websocket.
Other than the issue with the ping and the read deadline, the code looks good. Is there anything else to resolve?
No thank you very much for your time and the details. It really helped. I will set the writeDeadline to something like 60, which seemed to work flawlessly.
from websocket.
Related Issues (20)
- [ISSUE] Handling SIGINT and SIGTERM for Graceful Shutdown in Go Websocket App HOT 2
- [BUG] If `deadline` argument to `WriteControl` is `deadline.IsZero()` then 1000 hours (~41 days) are used as unexpected and undocumented fallback HOT 2
- [BUG] 不能删除超时的连接 HOT 1
- [QUESTION] Regarding pongWait Time in the Examples HOT 2
- [BUG] <SRV send msg to client and close the conn right no, client can't receive the msg> HOT 1
- Address CVE-2023-45288 HOT 1
- [FEATURE] Support Unix Domain Sockets HOT 4
- [BUG] FormatMessageType returns empty string for unknown type HOT 3
- [BUG] ssh: tcpChan: deadline not supported HOT 1
- [BUG] panic: concurrent write to websocket connection HOT 4
- [FEATURE] expose newConn function for custom Upgrader HOT 2
- [BUG] WebSocket server does not send 426 Upgrade Required
- [BUG] <title>panic: concurrent write to websocket connection HOT 4
- After connecting to websocket, the server prints the following log each time it receives a message: read/write on closed pipe. HOT 4
- Restore trust in the project HOT 5
- [BUG] go test @ 1.5.2 fails HOT 1
- [BUG] v1.5.2 checksum mismatch HOT 5
- [Question] Why are username and password not allowed in websocket URIs? HOT 2
- what's up with closing? HOT 2
- [BUG] panic: concurrent read to websocket connection bug HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from websocket.