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

Why does this API so strictly enforce thread management? #155

Open
bw-work-mt opened this issue Jun 22, 2021 · 1 comment
Open

Why does this API so strictly enforce thread management? #155

bw-work-mt opened this issue Jun 22, 2021 · 1 comment

Comments

@bw-work-mt
Copy link

Does anyone know why this API enforces all messages must originate from the same thread the server was started on?

@hoytech
Copy link
Contributor

hoytech commented Jun 23, 2021

Seasocks is an asynchronous server, using the linux-specific epoll() API. There is one thread responsible for managing all events, like reading from the socket, buffering messages, writing to the socket, etc.

If multiple threads could all interact with the socket, then there would need to be locking to protect the data-structures this thread uses, which would increase complexity and decrease performance.

But this typically isn't a problem, since actually sending/receiving messages takes so little resources. Usually it's your own code that you want to run in a separate thread anyway. What you can do is create a worker thread pool that takes requests off a queue. The workers will then do their expensive processing, and push the responses onto another queue, and then wake up the seasocks thread to send the messages.

You can also have multiple processes all accepting connections from the same websocket if you use SO_REUSEPORT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants