Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upExplain how filewatch and other examples ensure at most one concurrent writer #87
Comments
Why? The concurrency of WriteControl is not needed. |
Best practice? We used this as an example of how two add a pinger using websocket and it caused the issue as per: #83 |
Best practice in this case is to use WriteMessage. |
I am looking for someone to help on this issue. |
Hi, garyburd, we are use this library for more than 1000 users chartroom program, concurrent writing is needed, 1000 users * 20 messsages per seconds, we must put this worker into goroutine, please tell me ,how to best practice. Now I fork it and add writerlocker and readerlocker, but it's not the best way I think. |
@hetao29 Use a sync.Mutex in your application to ensure that only one goroutine calls the write methods concurrently or use a single goroutine for all writes as is done in the chat example. |
Hi, @GaryBud, In your chat example, high concurrent broadcast is problematic, a network delay of w.Write (message) (line 102, file client.go) will block all subsequent w.Write (message) If you want to broadcast to 1000+ people at the same time: "a single goroutine for all writes", if a person's network delay, will lead to all other people delay or block like your chat example; "one goroutine calls the write methods concurrently", but will lead to a large number The goroutine generation, in the stress test, the memory and cpu will continue to rise. |
@hetao29 Are you observing that? Note that the chat example spins up a readPump and writePump per connection and then pushes messages into each clients' send channel - https://github.com/gorilla/websocket/blob/master/examples/chat/hub.go#L45 This channel is buffered - and the loop over all clients does not block based on a receiver. |
@hetao29 Because hub uses select with default when sending the channel, the hub does not block in theory or in practice. |
The package documentation covers everything that a user of the package needs to know regarding concurrency. Is more documentation required? |
While its not broken filewatch should really use WriteControl to send Ping messages.
https://github.com/gorilla/websocket/blob/master/examples/filewatch/main.go#L106