Skip to content

multi threaded server - #83

Merged
Davidiusdadi merged 8 commits into
TooTallNate:masterfrom
Davidiusdadi:master
May 15, 2012
Merged

Davidiusdadi merged 8 commits into
TooTallNate:masterfrom
Davidiusdadi:master

Conversation

@Davidiusdadi

Copy link
Copy Markdown
Collaborator

The server did not scale very well because it uses only one thread at all. ( #79 )
Therefore i made the it multi threaded.

I validated the changes using the autobahn test suit and the stress test utility of the example folder.

The server at its current state uses blocking writing.
The old behavior was to block only if the internal message queue was full and notify the selector thread otherwise who then flushed all connections unwritten data...

I am not yet sure whats the best way to deal with writing so i would not merge yet.
Besides multi threaded code should be reviewed and checked as intense as possible :)

@Davidiusdadi

Copy link
Copy Markdown
Collaborator Author

#79
well i just discovered that this pull request still has a performance issue.
I am going to fix that during the next few days...

…tructor

Test implementation of an only partially blocking writing in the WebSocketServer.java
@l3eta

l3eta commented Apr 16, 2012

Copy link
Copy Markdown

I was going to do this my self when I was writing a server for something with this API and then I dropped the project so I never started it. For performance wise. Just add in a check for if it's in use (Active) if not then sleep for a few then check again. Btw. I love this API saves my time although I wish the Client and Server were in different packages.

@Davidiusdadi

Copy link
Copy Markdown
Collaborator Author

How 95a615e works:

  • The selector thread selects the websocktes that are ready for io operations (using the selection keys) and puts them into the blocking queue.
  • The worker threads then takes the websockets form that queue and performs the message encoding and its delivery via the handlers.

The queue will block until its there is space free which happens when the selector puts new websockets into it.
The queue will also block a worker thread if there is currently nothing in the queue until the selector puts something into it.
That way none of the threads will ever be idling. They are all either blocking or doing something.

Currently messages will be sent in a blocking way unless the messages are sent form inside a message handler.
In the latter case sending the messages out over the wire will be performed directly after the messages delivery.

The websockets handleRead method must only be called once at the same time. During one select operation of the selector thread a websocket can only be put onto the queue once. But because a worker thread could still process a message of a previous selection i declared it synchronized. I also thought of using an an atomic boolean to abroad handleRead if there is still an handle read in progress but if that message would be the last message of a sequence of multiple ones there would be no way to detect that the channel of that websocket is ready to perform a read operation on.
Since i am not 100% sure when keys are put onto the selected key set i thought that would be the safer way.
Conclusion: The probability of 2 or more worker threads wanting to enter handleRead at the same time is unlikely an becomes ever more unlikely as more websocket connections there are.

separated WebSocket interface and implementation
introduced WebSocketFactory to allow users to better customize their websockets
Davidiusdadi added a commit that referenced this pull request May 15, 2012
Since the current head of this project is not working properly for more than a week now because i made a stupid mistake during merging stuff that should have belonged to this pull request i will now merge this pull request to be able focus on one branch instead of too.

@sippykup Thanks for revealing those problems!
@Davidiusdadi
Davidiusdadi merged commit 8780c85 into TooTallNate:master May 15, 2012
@Davidiusdadi

Copy link
Copy Markdown
Collaborator Author

Since the current head of this project is not working properly for more than a week now because i made a stupid mistake during merging stuff that should have belonged to this pull request i will now merge this pull request to be able focus on one branch instead of too.

@sippykup Thanks for revealing those problems!

maakolk pushed a commit to maakolk/Java-WebSocket that referenced this pull request Mar 5, 2017
Since the current head of this project is not working properly for more than a week now because i made a stupid mistake during merging stuff that should have belonged to this pull request i will now merge this pull request to be able focus on one branch instead of too.

@sippykup Thanks for revealing those problems!
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

Successfully merging this pull request may close these issues.

2 participants