Barbosik / MultiOgar

An open source Ogar server implementation, written with Node.js.
Other
61 stars 44 forks source link

Multithreading #54

Open Influxx opened 8 years ago

Influxx commented 8 years ago

Hi, would it be possible to make MultiOgar multithreaded? Being able to use multiple cpu cores for one game instance would result in even better performance.

Socket.io for example uses Redis as a message broker between the connected clients to achieve multi-threading: https://github.com/socketio/socket.io-redis/blob/master/index.js

Gist example for ws module: https://gist.github.com/bricecarpentier/5599799

Node.js has cluster abilities: https://nodejs.org/api/cluster.html

And with the PM2 module it's even easier: pm2 start index.js -i max

Also, https://github.com/alexhultman/uWebSockets/ claims to be the fastest websocket implementation for Node...

Barbosik commented 8 years ago

thanks for info :)

Luka967 commented 8 years ago

You can use jxcore, however I didn't look for it a lot.

Influxx commented 8 years ago

Tested jxcore. Can launch multiple instances with: jx mt:<#threads> index.js. However, memory is not shared programmatically so players will connect to different game instances. It essentially launches a game instance for however many threads you define, which is not really desired.

To achieve true multi-threading, it would need to be implemented in MultiOgar itself, by using (for example) Node's clustering abilities combined with Redis / RabbitMQ to share state (connections) in memory - see first post.

I've tried implementing https://github.com/alexhultman/uWebSockets/ in MultiOgar as they claim it is one of the fastest, however because they only povide a server API and not a client API, I was getting errors on sendPacket() function (which extends WebSocket client). Anyone know a solution for this?

Barbosik commented 8 years ago

there is just one thing that can be executed on separate threads - websocket send. I'm not sure if it's possible with node.js

chuushi commented 8 years ago

The chat part and player pre-join codes can be run/compiled on a separate thread and brought over to the packet sender/gameserver. It would be for good if issue #164 goes through.

Barbosik commented 8 years ago

multithreading requires to use thread synchronization objects and features, such as critical sections, semaphores, mutexes, memory bariers, spin locks and other things. Without these things, you cannot utilize multithreading effectively. I'm not sure, but as I know node.js doesn't support it. Also, multithreading makes code very complicated and it's easy to add bugs.