Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] concurrent safe write function #826

Open
0x-3FFF opened this issue Nov 3, 2022 · 5 comments
Open

[feature] concurrent safe write function #826

0x-3FFF opened this issue Nov 3, 2022 · 5 comments

Comments

@0x-3FFF
Copy link

0x-3FFF commented Nov 3, 2022

currently, the implementation of the write series function is concurrency-unsafe, it is recommended to introduce a concurrency-safe write function

@ghost
Copy link

ghost commented Jun 15, 2024

Concurrent writes are the top support issue for this package.

It's not useful to make the current methods threadsafe given the overall design of the API, but it might be worth doing to reduce the support burden.

@jaitaiwan
Copy link
Member

I've been thinking about this for awhile. Do we support thread-safeness into the future or do we just say "too bad" and ask folks to find a better library. I'm not entirely sure.

@ghost
Copy link

ghost commented Jun 15, 2024

Drive the concurrency-related issues elsewhere by making the individual methods threadsafe. Support thread safety into the future.

Developers of robust applications will want to handle concurrency within their application code. The current API is no hindrance to those developers and may be preferable to other APIs.

I read through the examples in this repository and don't see how additional thread safety in the package will benefit them. Perhaps I need to spend more time looking at them. As an interesting side note, none of the examples use sync.Mutex.

@hulkingshtick
Copy link

Given that deadline, compression and compression level are set by separate methods, it does not make sense to support concurrent calls to NextWriter, WriteMessage and WriteJSON.

A previous comment suggests that the support burden can be reduced by making the individual methods thread-safe. That's a lot of work for something that does not make sense.

That said, there is some low-hanging fruit. Based on a scan through the issues tracker, it looks like most of the concurrent writes are through WriteMessage and WriteJSON. Adding a mutex to protect those two calls will probably eliminate most of the reported concurrent write to websocket connection panics and mostly do the right thing for the application.

@hulkingshtick
Copy link

do we just say "too bad" and ask folks to find a better library.

No, tell them to fix their code by writing from a single goroutine or by using a mutex.

You can also create a new higher-level package that encapsulates a common use pattern for the package, namely the read and write pump from the chat example (but not the chat example's hub). This package can address concurrent write and other common needs such as liveness detection. The package would not add protocol above the websocket layer or be applicable to all use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

5 participants